-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
rsa.go
69 lines (62 loc) · 1.93 KB
/
rsa.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
package tools
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
)
// RSA加密
func RSAEncrypt(data, publicBytes []byte) ([]byte, error) {
var res []byte
// 解析公钥
block, _ := pem.Decode(publicBytes)
if block == nil {
return res, fmt.Errorf("无法加密, 公钥可能不正确")
}
// 使用X509将解码之后的数据 解析出来
// x509.MarshalPKCS1PublicKey(block):解析之后无法用,所以采用以下方法:ParsePKIXPublicKey
keyInit, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return res, fmt.Errorf("无法加密, 公钥可能不正确, %v", err)
}
// 使用公钥加密数据
pubKey := keyInit.(*rsa.PublicKey)
res, err = rsa.EncryptPKCS1v15(rand.Reader, pubKey, data)
if err != nil {
return res, fmt.Errorf("无法加密, 公钥可能不正确, %v", err)
}
// 将数据加密为base64格式
return []byte(EncodeStr2Base64(string(res))), nil
}
// 对数据进行解密操作
func RSADecrypt(base64Data, privateBytes []byte) ([]byte, error) {
var res []byte
// 将base64数据解析
data := []byte(DecodeStrFromBase64(string(base64Data)))
// 解析私钥
block, _ := pem.Decode(privateBytes)
if block == nil {
return res, fmt.Errorf("无法解密, 私钥可能不正确,解析私钥失败")
}
// 还原数据
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return res, fmt.Errorf("无法解密, 私钥可能不正确,解析PKCS失败 %v", err)
}
res, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, data)
if err != nil {
return res, fmt.Errorf("无法解密, 私钥可能不正确,解密PKCS1v15失败 %v", err)
}
return res, nil
}
// 加密base64字符串
func EncodeStr2Base64(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}
// 解密base64字符串
func DecodeStrFromBase64(str string) string {
decodeBytes, _ := base64.StdEncoding.DecodeString(str)
return string(decodeBytes)
}