-
Notifications
You must be signed in to change notification settings - Fork 222
/
encrypt_rsa.go
491 lines (449 loc) · 9.74 KB
/
encrypt_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package xorm
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"io"
"io/ioutil"
"math/big"
)
const (
RSA_PUBKEY_ENCRYPT_MODE = iota //公钥加密
RSA_PUBKEY_DECRYPT_MODE //公钥解密
RSA_PRIKEY_ENCRYPT_MODE //私钥加密
RSA_PRIKEY_DECRYPT_MODE //私钥解密
)
type RsaEncrypt struct {
PubKey string
PriKey string
pubkey *rsa.PublicKey
prikey *rsa.PrivateKey
EncryptMode int
DecryptMode int
}
func (this *RsaEncrypt) Encrypt(strMesg string) ([]byte, error) {
var inByte []byte
var err error
if this.EncryptMode == RSA_PUBKEY_ENCRYPT_MODE {
this.pubkey, err = getPubKey([]byte(this.PubKey))
if err != nil {
return nil, err
}
}
if this.EncryptMode == RSA_PRIKEY_ENCRYPT_MODE {
this.prikey, err = getPriKey([]byte(this.PriKey))
if err != nil {
return nil, err
}
}
inByte = []byte(strMesg)
inByte, err = this.Byte(inByte, this.EncryptMode)
if err != nil {
return nil, err
}
return inByte, nil
}
func (this *RsaEncrypt) Decrypt(crypted []byte) (decrypted []byte, err error) {
if this.DecryptMode == RSA_PUBKEY_DECRYPT_MODE {
this.pubkey, err = getPubKey([]byte(this.PubKey))
if err != nil {
return nil, err
}
}
if this.DecryptMode == RSA_PRIKEY_DECRYPT_MODE {
this.prikey, err = getPriKey([]byte(this.PriKey))
if err != nil {
return nil, err
}
}
decrypted, err = base64.StdEncoding.DecodeString(string(crypted))
if err != nil {
return nil, err
}
decrypted, err = this.Byte(decrypted, this.DecryptMode)
if err != nil {
return nil, err
}
return decrypted, nil
}
func (this *RsaEncrypt) Byte(in []byte, mode int) ([]byte, error) {
out := bytes.NewBuffer(nil)
err := this.IO(bytes.NewReader(in), out, mode)
if err != nil {
return nil, err
}
return ioutil.ReadAll(out)
}
func (this *RsaEncrypt) IO(in io.Reader, out io.Writer, mode int) error {
switch mode {
case RSA_PUBKEY_ENCRYPT_MODE:
if key, err := this.getPubKey(); err != nil {
return err
} else {
return pubKeyIO(key, in, out, true)
}
case RSA_PUBKEY_DECRYPT_MODE:
if key, err := this.getPubKey(); err != nil {
return err
} else {
return pubKeyIO(key, in, out, false)
}
case RSA_PRIKEY_ENCRYPT_MODE:
if key, err := this.getPriKey(); err != nil {
return err
} else {
return priKeyIO(key, in, out, true)
}
case RSA_PRIKEY_DECRYPT_MODE:
if key, err := this.getPriKey(); err != nil {
return err
} else {
return priKeyIO(key, in, out, false)
}
default:
return errors.New("mode not found")
}
}
func (this *RsaEncrypt) getPubKey() (*rsa.PublicKey, error) {
if this.pubkey == nil {
return nil, ErrPublicKey
}
return this.pubkey, nil
}
func (this *RsaEncrypt) getPriKey() (*rsa.PrivateKey, error) {
if this.prikey == nil {
return nil, ErrPrivateKey
}
return this.prikey, nil
}
//-----------------------------------------
var (
ErrDataToLarge = errors.New("message too long for RSA public key size")
ErrDataLen = errors.New("data length error")
ErrDataBroken = errors.New("data broken, first byte is not zero")
ErrKeyPairDismatch = errors.New("data is not encrypted by the private key")
ErrDecryption = errors.New("decryption error")
ErrPublicKey = errors.New("get public key error")
ErrPrivateKey = errors.New("get private key error")
)
/*公钥解密*/
func pubKeyDecrypt(pub *rsa.PublicKey, data []byte) ([]byte, error) {
k := (pub.N.BitLen() + 7) / 8
if k != len(data) {
return nil, ErrDataLen
}
m := new(big.Int).SetBytes(data)
if m.Cmp(pub.N) > 0 {
return nil, ErrDataToLarge
}
m.Exp(m, big.NewInt(int64(pub.E)), pub.N)
d := leftPad(m.Bytes(), k)
if d[0] != 0 {
return nil, ErrDataBroken
}
if d[1] != 0 && d[1] != 1 {
return nil, ErrKeyPairDismatch
}
var i = 2
for ; i < len(d); i++ {
if d[i] == 0 {
break
}
}
i++
if i == len(d) {
return nil, nil
}
return d[i:], nil
}
/*私钥加密*/
func priKeyEncrypt(rand io.Reader, priv *rsa.PrivateKey, hashed []byte) ([]byte, error) {
tLen := len(hashed)
k := (priv.N.BitLen() + 7) / 8
if k < tLen+11 {
return nil, ErrDataLen
}
em := make([]byte, k)
em[1] = 1
for i := 2; i < k-tLen-1; i++ {
em[i] = 0xff
}
copy(em[k-tLen:k], hashed)
m := new(big.Int).SetBytes(em)
c, err := decrypt(rand, priv, m)
if err != nil {
return nil, err
}
copyWithLeftPad(em, c.Bytes())
return em, nil
}
/*公钥加密或解密Reader*/
func pubKeyIO(pub *rsa.PublicKey, in io.Reader, out io.Writer, isEncrytp bool) error {
k := (pub.N.BitLen() + 7) / 8
if isEncrytp {
k = k - 11
}
buf := make([]byte, k)
var b []byte
var err error
size := 0
for {
size, err = in.Read(buf)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if size < k {
b = buf[:size]
} else {
b = buf
}
if isEncrytp {
b, err = rsa.EncryptPKCS1v15(rand.Reader, pub, b)
} else {
b, err = pubKeyDecrypt(pub, b)
}
if err != nil {
return err
}
if _, err = out.Write(b); err != nil {
return err
}
}
return nil
}
/*私钥加密或解密Reader*/
func priKeyIO(pri *rsa.PrivateKey, r io.Reader, w io.Writer, isEncrytp bool) error {
k := (pri.N.BitLen() + 7) / 8
if isEncrytp {
k = k - 11
}
buf := make([]byte, k)
var err error
var b []byte
size := 0
for {
size, err = r.Read(buf)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if size < k {
b = buf[:size]
} else {
b = buf
}
if isEncrytp {
b, err = priKeyEncrypt(rand.Reader, pri, b)
} else {
b, err = rsa.DecryptPKCS1v15(rand.Reader, pri, b)
}
if err != nil {
return err
}
if _, err = w.Write(b); err != nil {
return err
}
}
return nil
}
/*公钥加密或解密byte*/
func pubKeyByte(pub *rsa.PublicKey, in []byte, isEncrytp bool) ([]byte, error) {
k := (pub.N.BitLen() + 7) / 8
if isEncrytp {
k = k - 11
}
if len(in) <= k {
if isEncrytp {
return rsa.EncryptPKCS1v15(rand.Reader, pub, in)
} else {
return pubKeyDecrypt(pub, in)
}
} else {
iv := make([]byte, k)
out := bytes.NewBuffer(iv)
if err := pubKeyIO(pub, bytes.NewReader(in), out, isEncrytp); err != nil {
return nil, err
}
return ioutil.ReadAll(out)
}
}
/*私钥加密或解密byte*/
func priKeyByte(pri *rsa.PrivateKey, in []byte, isEncrytp bool) ([]byte, error) {
k := (pri.N.BitLen() + 7) / 8
if isEncrytp {
k = k - 11
}
if len(in) <= k {
if isEncrytp {
return priKeyEncrypt(rand.Reader, pri, in)
} else {
return rsa.DecryptPKCS1v15(rand.Reader, pri, in)
}
} else {
iv := make([]byte, k)
out := bytes.NewBuffer(iv)
if err := priKeyIO(pri, bytes.NewReader(in), out, isEncrytp); err != nil {
return nil, err
}
return ioutil.ReadAll(out)
}
}
/*读取公钥*/
func getPubKey(in []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(in)
if block == nil {
return nil, ErrPublicKey
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
} else {
return pub.(*rsa.PublicKey), err
}
}
/*读取私钥*/
func getPriKey(in []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(in)
if block == nil {
return nil, ErrPrivateKey
}
pri, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err == nil {
return pri, nil
}
pri2, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
} else {
return pri2.(*rsa.PrivateKey), nil
}
}
/*从crypto/rsa复制 */
var bigZero = big.NewInt(0)
var bigOne = big.NewInt(1)
/*从crypto/rsa复制 */
func encrypt(c *big.Int, pub *rsa.PublicKey, m *big.Int) *big.Int {
e := big.NewInt(int64(pub.E))
c.Exp(m, e, pub.N)
return c
}
/*从crypto/rsa复制 */
func decrypt(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, err error) {
if c.Cmp(priv.N) > 0 {
err = ErrDecryption
return
}
var ir *big.Int
if random != nil {
var r *big.Int
for {
r, err = rand.Int(random, priv.N)
if err != nil {
return
}
if r.Cmp(bigZero) == 0 {
r = bigOne
}
var ok bool
ir, ok = modInverse(r, priv.N)
if ok {
break
}
}
bigE := big.NewInt(int64(priv.E))
rpowe := new(big.Int).Exp(r, bigE, priv.N)
cCopy := new(big.Int).Set(c)
cCopy.Mul(cCopy, rpowe)
cCopy.Mod(cCopy, priv.N)
c = cCopy
}
if priv.Precomputed.Dp == nil {
m = new(big.Int).Exp(c, priv.D, priv.N)
} else {
m = new(big.Int).Exp(c, priv.Precomputed.Dp, priv.Primes[0])
m2 := new(big.Int).Exp(c, priv.Precomputed.Dq, priv.Primes[1])
m.Sub(m, m2)
if m.Sign() < 0 {
m.Add(m, priv.Primes[0])
}
m.Mul(m, priv.Precomputed.Qinv)
m.Mod(m, priv.Primes[0])
m.Mul(m, priv.Primes[1])
m.Add(m, m2)
for i, values := range priv.Precomputed.CRTValues {
prime := priv.Primes[2+i]
m2.Exp(c, values.Exp, prime)
m2.Sub(m2, m)
m2.Mul(m2, values.Coeff)
m2.Mod(m2, prime)
if m2.Sign() < 0 {
m2.Add(m2, prime)
}
m2.Mul(m2, values.R)
m.Add(m, m2)
}
}
if ir != nil {
m.Mul(m, ir)
m.Mod(m, priv.N)
}
return
}
/*从crypto/rsa复制 */
func copyWithLeftPad(dest, src []byte) {
numPaddingBytes := len(dest) - len(src)
for i := 0; i < numPaddingBytes; i++ {
dest[i] = 0
}
copy(dest[numPaddingBytes:], src)
}
/*从crypto/rsa复制 */
func nonZeroRandomBytes(s []byte, rand io.Reader) (err error) {
_, err = io.ReadFull(rand, s)
if err != nil {
return
}
for i := 0; i < len(s); i++ {
for s[i] == 0 {
_, err = io.ReadFull(rand, s[i:i+1])
if err != nil {
return
}
s[i] ^= 0x42
}
}
return
}
/*从crypto/rsa复制 */
func leftPad(input []byte, size int) (out []byte) {
n := len(input)
if n > size {
n = size
}
out = make([]byte, size)
copy(out[len(out)-n:], input)
return
}
/*从crypto/rsa复制 */
func modInverse(a, n *big.Int) (ia *big.Int, ok bool) {
g := new(big.Int)
x := new(big.Int)
y := new(big.Int)
g.GCD(x, y, a, n)
if g.Cmp(bigOne) != 0 {
return
}
if x.Cmp(bigOne) < 0 {
x.Add(x, n)
}
return x, true
}