-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsa_encrypt.go
196 lines (177 loc) · 4.85 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
package secret
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"os"
)
type RsaBitsType int
const (
RsaBits512 RsaBitsType = 512
RsaBits1024 RsaBitsType = 1024
RsaBits2048 RsaBitsType = 2048
RsaBits4096 RsaBitsType = 4096
RsaDefaultBits = RsaBits1024
)
const (
RsaDefaultPublishKeyName = "publishKey"
RsaDefaultPrivateKeyName = "privateKey"
PublishKey = "PUBLIC KEY"
PrivateKey = "RSA PRIVATE KEY"
)
type RsaEncrypt struct {
Bits RsaBitsType // 定位大小的 RSA 密钥对
PublishKeyName string // 公钥名字
PrivateKeyName string // 私钥名字
PublishKeyPath string // 公钥的输出路径
PrivateKeyPath string // 私钥的输出路径
}
var RsaBitsMap = map[RsaBitsType]int{
RsaBits512: 512,
RsaBits1024: 1024,
RsaBits2048: 2048,
RsaBits4096: 4096,
}
func formatPubAndPriKeyName(name string) string {
return fmt.Sprintf("\\%s.pem", name)
}
func NewDefaultRsaEncrypt() *RsaEncrypt {
defaultPath, _ := os.Getwd()
return &RsaEncrypt{
Bits: RsaDefaultBits,
PublishKeyName: formatPubAndPriKeyName(RsaDefaultPublishKeyName),
PrivateKeyName: formatPubAndPriKeyName(RsaDefaultPrivateKeyName),
PublishKeyPath: defaultPath + formatPubAndPriKeyName(RsaDefaultPublishKeyName),
PrivateKeyPath: defaultPath + formatPubAndPriKeyName(RsaDefaultPrivateKeyName),
}
}
func NewRsaEncrypt(bits RsaBitsType, publishKeyName, publishKeyPath, privateKeyName, privateKeyPath string) *RsaEncrypt {
obj := NewDefaultRsaEncrypt()
if bits != 0 {
obj.Bits = bits
}
if publishKeyName != "" {
obj.PublishKeyName = formatPubAndPriKeyName(publishKeyName)
}
if publishKeyPath != "" {
obj.PublishKeyPath = publishKeyPath + formatPubAndPriKeyName(publishKeyName)
}
if privateKeyName != "" {
obj.PrivateKeyName = formatPubAndPriKeyName(privateKeyName)
}
if privateKeyPath != "" {
obj.PrivateKeyPath = privateKeyPath + formatPubAndPriKeyName(privateKeyName)
}
return obj
}
// SaveRsaKey 保存生成的公钥和密钥
func (r *RsaEncrypt) SaveRsaKey() error {
privateKey, err := rsa.GenerateKey(rand.Reader, RsaBitsMap[r.Bits])
if err != nil {
fmt.Println(err)
return err
}
publicKey := privateKey.PublicKey
// 使用x509标准对私钥进行编码,AsN.1编码字符串
x509Privete := x509.MarshalPKCS1PrivateKey(privateKey)
// 使用x509标准对公钥进行编码,AsN.1编码字符串
x509Public := x509.MarshalPKCS1PublicKey(&publicKey)
// 对私钥封装block 结构数据
blockPrivate := pem.Block{Type: PrivateKey, Bytes: x509Privete}
// 对公钥封装block 结构数据
blockPublic := pem.Block{Type: PublishKey, Bytes: x509Public}
// 创建存放私钥的文件
privateFile, errPri := os.Create(r.PrivateKeyPath)
if errPri != nil {
return errPri
}
defer func(privateFile *os.File) {
errClose := privateFile.Close()
if errClose != nil {
panic(errClose)
}
}(privateFile)
err = pem.Encode(privateFile, &blockPrivate)
if err != nil {
return err
}
// 创建存放公钥的文件
publicFile, errPub := os.Create(r.PublishKeyPath)
if errPub != nil {
return errPub
}
defer publicFile.Close()
err = pem.Encode(publicFile, &blockPublic)
if err != nil {
return err
}
return nil
}
// RsaEncrypt 加密
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)
// pem解码
block, _ := pem.Decode(keyBytes)
// x509解码
publicKey, errPb := x509.ParsePKCS1PublicKey(block.Bytes)
if errPb != nil {
return nil, errPb
}
// 使用公钥对明文进行加密
retByte, errRet := rsa.EncryptPKCS1v15(rand.Reader, publicKey, srcByte)
if errRet != nil {
return nil, errRet
}
return retByte, nil
}
// RsaDecrypt 解密
func (r *RsaEncrypt) RsaDecrypt(srcByte []byte, filePath string) ([]byte, error) {
// 打开文件
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)
// pem解码
block, _ := pem.Decode(keyBytes)
// x509解码
privateKey, errPb := x509.ParsePKCS1PrivateKey(block.Bytes)
if errPb != nil {
return nil, errPb
}
// 进行解密
retByte, errRet := rsa.DecryptPKCS1v15(rand.Reader, privateKey, srcByte)
if errRet != nil {
return nil, errRet
}
return retByte, nil
}
// EncryptString String输出加密后的东西
func (r *RsaEncrypt) EncryptString(retByte []byte) string {
return base64.StdEncoding.EncodeToString(retByte)
}