/
ut_rsa.go
179 lines (158 loc) · 3.98 KB
/
ut_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
)
// Read RSA key file
func RsaReadKey(keyName string) []byte {
key, err := ioutil.ReadFile(keyName)
if err != nil {
return nil
}
return key
}
// Gen RSA key pair
func RsaGenKey(filePath string, bits int) error {
if !IsDirExist(filePath) {
os.Mkdir(filePath, 0700)
}
privPath := filepath.Join(filePath, "private.pem")
pubfPath := filepath.Join(filePath, "public.pem")
if IsFileExist(privPath) || IsFileExist(pubfPath) {
log.Println("Error: files already exist at:", filePath)
return errors.New("RSA key files already exist")
}
// Gen private key
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(privPath)
if err != nil {
log.Println("Error: create ", privPath, " failed")
return err
}
defer file.Close()
err = pem.Encode(file, block)
if err != nil {
return err
}
file.Chmod(0400)
// Gen public key
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(pubfPath)
if err != nil {
log.Println("Error: create ", pubfPath, " failed")
return err
}
defer file.Close()
err = pem.Encode(file, block)
if err != nil {
return err
}
file.Chmod(0400)
return nil
}
// RSA encrypt
func RsaEncrypt(publicKey []byte, origData []byte) ([]byte, error) {
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
}
pub := pubInterface.(*rsa.PublicKey)
k := (pub.N.BitLen() + 7) / 8
if len(origData) > k-11 {
o1, e1 := rsa.EncryptPKCS1v15(rand.Reader, pub, origData[:k-19])
o2, e2 := rsa.EncryptPKCS1v15(rand.Reader, pub, origData[k-19:])
if e1 != nil || e2 != nil {
return nil, errors.New("RSA encrypt error")
}
return append(o1, o2...), nil
} else {
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
}
// RSA decrypt
func RsaDecrypt(privateKey []byte, ciphertext []byte) ([]byte, error) {
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
}
k := (priv.N.BitLen() + 7) / 8
if len(ciphertext) > k {
o1, e1 := rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext[:k])
o2, e2 := rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext[k:])
if e1 != nil || e2 != nil {
return nil, errors.New("RSA decrypt error")
}
return append(o1, o2...), nil
} else {
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
}
func RsaAllTest(bits int) {
fmt.Println("==================== RsaAllTest ====================")
relFile, _ := exec.LookPath(os.Args[0])
absFile, _ := filepath.Abs(relFile)
absPath, _ := filepath.Abs(filepath.Dir(absFile))
keyPath := filepath.Join(absPath, "keys")
err := RsaGenKey(keyPath, bits)
if err != nil {
fmt.Println("RsaGenKey failed")
return
}
publicKey := RsaReadKey(filepath.Join(keyPath, "public.pem"))
if publicKey == nil {
fmt.Println("RsaReadKey public.pem failed")
return
}
privateKey := RsaReadKey(filepath.Join(keyPath, "private.pem"))
if privateKey == nil {
fmt.Println("RsaReadKey private.pem failed")
return
}
plaintext := []byte("some plain text for test")
ciphertext, _ := RsaEncrypt(publicKey, plaintext)
if ciphertext == nil {
fmt.Println("RsaEncrypt plaintext failed")
return
}
fmt.Println("RSA ENC", bits, ":", hex.EncodeToString(ciphertext))
bDec, _ := RsaDecrypt(privateKey, ciphertext)
if bDec == nil {
fmt.Println("RsaDecrypt ciphertext failed")
return
}
fmt.Println("RSA DEC", bits, ":", string(bDec))
}