-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa.go
67 lines (62 loc) · 1.72 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
package function
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
)
// RsaEncrypt 使用公钥加密数据[只有私钥才能解开]
func RsaEncrypt(str, publicKey string) (string, error) {
b, err := RsaEncryptByte([]byte(str), publicKey)
if err != nil {
return "", err
}
return string(b), nil
}
// RsaDecrypt 使用私钥解密公钥加密的数据
func RsaDecrypt(str, privateKey string) (string, error) {
b, err := RsaDecryptByte([]byte(str), privateKey)
if err != nil {
return "", err
}
return string(b), nil
}
// RsaEncryptByte 使用公钥加密数据[只有私钥才能解开]
func RsaEncryptByte(plain []byte, publicKey string) (cipherByte []byte, err error) {
var (
pubKeyValue interface{}
encryptOAEP []byte
)
// 解码公钥
pubBlock, _ := pem.Decode([]byte(publicKey))
// 读取公钥
pubKeyValue, err = x509.ParsePKIXPublicKey(pubBlock.Bytes)
if err != nil {
return
}
pub := pubKeyValue.(*rsa.PublicKey)
// 加密数据方法:不用使用EncryptPKCS1v15方法加密,源码里面推荐使用EncryptOAEP,因此这里使用安全的方法加密
encryptOAEP, err = rsa.EncryptOAEP(sha1.New(), rand.Reader, pub, plain, nil)
if err != nil {
return
}
cipherByte = encryptOAEP
return
}
// RsaDecryptByte 使用私钥解密公钥加密的数据
func RsaDecryptByte(cipherByte []byte, privateKey string) (decryptOAEP []byte, err error) {
var priKey *rsa.PrivateKey
// 解析出私钥
priBlock, _ := pem.Decode([]byte(privateKey))
priKey, err = x509.ParsePKCS1PrivateKey(priBlock.Bytes)
if err != nil {
return
}
// 解密RSA-OAEP方式加密后的内容
decryptOAEP, err = rsa.DecryptOAEP(sha1.New(), rand.Reader, priKey, cipherByte, nil)
if err != nil {
return
}
return
}