forked from nanjishidu/gomini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
executable file
·97 lines (92 loc) · 2.06 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
// rsa.go
package gocrypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"os"
)
//openssl genrsa -out private.pem 1024
//openssl rsa -in private.pem -pubout -out public.pem
func GenRsaKey(bits int, pemFile ...string) error {
var privatePem, publicPem = "private.pem","public.pem"
if len(pemFile)>=2{
privatePem = pemFile[0]
publicPem = pemFile[1]
}
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derStream,
}
file, err := os.Create(privatePem)
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type: "PUBLIC KEY",
Bytes: derPkix,
}
file, err = os.Create(publicPem)
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
return nil
}
func RSAEncrypt(data []byte, publicPem ...string) ([]byte, error) {
var publicPemPath = "public.pem"
if len(publicPem)>= 1 {
publicPemPath = publicPem[0]
}
publicKey, err := readFileBytes(publicPemPath)
if err != nil {
return nil, err
}
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.EncryptPKCS1v15(rand.Reader, pubInterface.(*rsa.PublicKey), data)
}
func RSADecrypt(data []byte, privatePem ...string) ([]byte, error) {
var privatePemPath = "private.pem"
if len(privatePem) >= 1 {
privatePemPath = privatePem[0]
}
privateKey, err := readFileBytes(privatePemPath)
if err != nil {
return nil, err
}
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, data)
}