-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.go
130 lines (116 loc) · 3.15 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
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
package crab
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"fmt"
)
// GenerateRSAKey 创建RSA 常用位数 1024 2048 4096
func GenerateRSAKey(bits int) (priKey, pubKey []byte, err error) {
priWriter := bytes.NewBuffer([]byte{})
pubWriter := bytes.NewBuffer([]byte{})
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
privateBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: X509PrivateKey,
}
if err = pem.Encode(priWriter, privateBlock); err != nil {
return
}
publicKey := privateKey.PublicKey
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
panic(err)
}
publicBlock := &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: X509PublicKey,
}
if err = pem.Encode(pubWriter, publicBlock); err != nil {
return
}
priKey = priWriter.Bytes()
pubKey = pubWriter.Bytes()
return
}
// GenerateRSAKeyWithPwd 创建带密码的RSA
func GenerateRSAKeyWithPwd(passwd []byte, bits int) (priKey, pubKey []byte, err error) {
priWriter := bytes.NewBuffer([]byte{})
pubWriter := bytes.NewBuffer([]byte{})
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
x509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
privateBlock, err := x509.EncryptPEMBlock(rand.Reader, "RSA PRIVATE KEY", x509PrivateKey, passwd, x509.PEMCipherAES256)
if err != nil {
return
}
err = pem.Encode(priWriter, privateBlock)
if err != nil {
return
}
X509PublicKey, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
if err != nil {
return
}
publicBlock := &pem.Block{Type: "RSA PUBLIC KEY", Bytes: X509PublicKey}
err = pem.Encode(pubWriter, publicBlock)
if err != nil {
return
}
priKey = priWriter.Bytes()
pubKey = pubWriter.Bytes()
return
}
// RSAEncryptOAEP 公钥加密
func RSAEncryptOAEP(plainText, pubCipherKey []byte) (cipherText []byte, err error) {
block, _ := pem.Decode(pubCipherKey)
if block == nil {
err = fmt.Errorf("failed to parse certificate PEM")
return
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return
}
publickey := pub.(*rsa.PublicKey)
return rsa.EncryptOAEP(sha256.New(), rand.Reader, publickey, plainText, nil)
}
// RSADecryptOAEP 私钥解密
func RSADecryptOAEP(cipherText, privCipherKey []byte) (plainText []byte, err error) {
block, _ := pem.Decode(privCipherKey)
if block == nil {
err = fmt.Errorf("failed to parse certificate PEM")
return
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return
}
return rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, cipherText, nil)
}
// RSADecryptOAEPPwd 私钥解密,带密码
func RSADecryptOAEPPwd(cipherText, privCipherKey, passwd []byte) (plainText []byte, err error) {
block, _ := pem.Decode(privCipherKey)
if block == nil {
err = fmt.Errorf("failed to parse certificate PEM")
return
}
data, err := x509.DecryptPEMBlock(block, passwd)
if err != nil {
return
}
privateKey, err := x509.ParsePKCS1PrivateKey(data)
if err != nil {
return
}
return rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, cipherText, nil)
}