forked from sunmi-OS/gocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorsa.go
executable file
·66 lines (46 loc) · 1.2 KB
/
gorsa.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
package gorsa
import (
"encoding/base64"
)
// 公钥加密
func PublicEncrypt(data, publicKey string) (string, error) {
grsa := RSASecurity{}
grsa.SetPublicKey(publicKey)
rsadata, err := grsa.PubKeyENCTYPT([]byte(data))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(rsadata), nil
}
// 私钥加密
func PriKeyEncrypt(data, privateKey string) (string, error) {
grsa := RSASecurity{}
grsa.SetPrivateKey(privateKey)
rsadata, err := grsa.PriKeyENCTYPT([]byte(data))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(rsadata), nil
}
// 公钥解密
func PublicDecrypt(data, publicKey string) (string, error) {
databs, _ := base64.StdEncoding.DecodeString(data)
grsa := RSASecurity{}
grsa.SetPublicKey(publicKey)
rsadata, err := grsa.PubKeyDECRYPT([]byte(databs))
if err != nil {
return "", err
}
return string(rsadata), nil
}
// 私钥解密
func PriKeyDecrypt(data, privateKey string) (string, error) {
databs, _ := base64.StdEncoding.DecodeString(data)
grsa := RSASecurity{}
grsa.SetPrivateKey(privateKey)
rsadata, err := grsa.PriKeyDECRYPT([]byte(databs))
if err != nil {
return "", err
}
return string(rsadata), nil
}