-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
120 lines (102 loc) · 2.96 KB
/
crypto.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
package cryptography
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"fmt"
"math/big"
)
func AESCBCEncrypt(plainText, key, iv []byte) []byte {
block, _ := aes.NewCipher(key)
plainText = pkcs5Padding(plainText, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, iv)
cipherText := make([]byte, len(plainText))
blockMode.CryptBlocks(cipherText, plainText)
return cipherText
}
func AESCBCDecrypt(cipherText, key, iv []byte) []byte {
block, _ := aes.NewCipher(key)
blockMode := cipher.NewCBCDecrypter(block, iv)
plainText := make([]byte, len(cipherText))
blockMode.CryptBlocks(plainText, cipherText)
plainText = pkcs5UnPadding(plainText)
return plainText
}
func AESECBEncrypt(plainText, key []byte) []byte {
block, _ := aes.NewCipher(key)
plainText = pkcs5Padding(plainText, block.BlockSize())
blockMode := NewECBEncrypter(block)
cipherText := make([]byte, len(plainText))
blockMode.CryptBlocks(cipherText, plainText)
return cipherText
}
func AESECBDecrypt(cipherText, key []byte) []byte {
block, _ := aes.NewCipher(key)
blockMode := NewECBDecrypter(block)
plainText := make([]byte, len(cipherText))
blockMode.CryptBlocks(plainText, cipherText)
plainText = pkcs5UnPadding(plainText)
return plainText
}
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
paddingText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, paddingText...)
}
func pkcs5UnPadding(src []byte) []byte {
n := len(src)
unPadding := int(src[n-1])
return src[:n-unPadding]
}
func RSAEncrypt(origData []byte, modulus string, exponent int64) string {
bigOrigData := big.NewInt(0).SetBytes(origData)
bigModulus, _ := big.NewInt(0).SetString(modulus, 16)
bigRs := bigOrigData.Exp(bigOrigData, big.NewInt(exponent), bigModulus)
return fmt.Sprintf("%0256x", bigRs)
}
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncrypter ecb
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Encrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
type ecbDecrypter ecb
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Decrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}