-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_encrypt.go
213 lines (170 loc) · 4.43 KB
/
rsa_encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package criptus
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"os"
"runtime"
)
const (
RsaDefaultPublishKeyName = "publishKey"
RsaDefaultPrivateKeyName = "privateKey"
PublishKey = "PUBLIC KEY"
PrivateKey = "RSA PRIVATE KEY"
)
type RsaEncrypt struct {
PublishKeyPath string
PrivateKeyPath string
OneTimeGenerate bool
Kind RsaBitsType
}
func formatName(name string) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("\\%s.pem", name)
}
return fmt.Sprintf("/%s.pem", name)
}
func NewRsaEncrypt(opts ...RSAOptions) (*RsaEncrypt, error) {
params, err := newRSAParams(opts...)
if err != nil {
return nil, err
}
defaultPath, _ := os.Getwd()
if len(params.GetPublishKeyPath()) == 0 {
params.SetPublishKeyPath(defaultPath +
formatName(RsaDefaultPublishKeyName))
}
if len(params.GetPrivateKeyPath()) == 0 {
params.SetPrivateKeyPath(defaultPath +
formatName(RsaDefaultPrivateKeyName))
}
if params.GetBits() == 0 {
params.SetBits(RsaBits1024)
}
return &RsaEncrypt{
PublishKeyPath: params.GetPublishKeyPath(),
PrivateKeyPath: params.GetPrivateKeyPath(),
Kind: params.GetBits(),
}, nil
}
func (r *RsaEncrypt) SaveRsaKey() error {
var privateKey *rsa.PrivateKey
var publicKey *rsa.PublicKey
if _, err := os.Stat(r.PublishKeyPath); err == nil {
// Load the existing public key
publicKeyBytes, err := os.ReadFile(r.PublishKeyPath)
if err != nil {
return err
}
block, _ := pem.Decode(publicKeyBytes)
if block == nil || block.Type != PublishKey {
return fmt.Errorf("failed to decode PEM block containing public key")
}
pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return err
}
publicKey = pubKey
}
if _, err := os.Stat(r.PrivateKeyPath); err == nil {
// Load the existing private key
privateKeyBytes, err := os.ReadFile(r.PrivateKeyPath)
if err != nil {
return err
}
block, _ := pem.Decode(privateKeyBytes)
if block == nil || block.Type != PrivateKey {
return fmt.Errorf("failed to decode PEM block containing private key")
}
priKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
privateKey = priKey
}
// If keys are not loaded, generate new ones
if privateKey == nil || publicKey == nil {
privateKey, err := rsa.GenerateKey(rand.Reader, r.Kind.Bits())
if err != nil {
return err
}
publicKey = &privateKey.PublicKey
x509PrivateBytes := x509.MarshalPKCS1PrivateKey(privateKey)
x509PublicBytes := x509.MarshalPKCS1PublicKey(publicKey)
blockPrivate := pem.Block{Type: PrivateKey, Bytes: x509PrivateBytes}
blockPublic := pem.Block{Type: PublishKey, Bytes: x509PublicBytes}
privateFile, err := os.Create(r.PrivateKeyPath)
if err != nil {
return err
}
defer privateFile.Close()
err = pem.Encode(privateFile, &blockPrivate)
if err != nil {
return err
}
publicFile, err := os.Create(r.PublishKeyPath)
if err != nil {
return err
}
defer publicFile.Close()
if err := pem.Encode(publicFile, &blockPublic); err != nil {
return err
}
}
return nil
}
func (r *RsaEncrypt) RsaEncrypt(src, filePath string) ([]byte, error) {
srcByte := []byte(src)
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
fileInfo, errInfo := file.Stat()
if errInfo != nil {
return nil, errInfo
}
keyBytes := make([]byte, fileInfo.Size())
file.Read(keyBytes)
block, _ := pem.Decode(keyBytes)
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, err
}
retByte, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, srcByte)
if err != nil {
return nil, err
}
return retByte, nil
}
func (r *RsaEncrypt) RsaDecrypt(srcByte []byte, filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
fileInfo, errInfo := file.Stat()
if errInfo != nil {
return "", errInfo
}
keyBytes := make([]byte, fileInfo.Size())
_, _ = file.Read(keyBytes)
block, _ := pem.Decode(keyBytes)
privateKey, errPb := x509.ParsePKCS1PrivateKey(block.Bytes)
if errPb != nil {
return "", errPb
}
retByte, errRet := rsa.DecryptPKCS1v15(rand.Reader, privateKey, srcByte)
if errRet != nil {
return "", errRet
}
return string(retByte), nil
}
func (r *RsaEncrypt) ToString(retByte []byte) string {
return base64.StdEncoding.EncodeToString(retByte)
}
func (r *RsaEncrypt) ToByte(src string) []byte {
b, _ := base64.StdEncoding.DecodeString(src)
return b
}