-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaesencrypt.go
198 lines (176 loc) · 4.58 KB
/
aesencrypt.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
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"errors"
"bytes"
)
/**
* AES加密
* @plainText 明文
* @key 密钥
* @返回base64加密文本
*/
func AesEncrypt(plainText, key string) (string, error) {
src := []byte(plainText)
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
bs := block.BlockSize()
// src = ZeroPadding(src, bs)
src = PKCS5Padding(src, bs)
if len(src)%bs != 0 {
return "", errors.New("need a multiple of the blocksize")
}
out := make([]byte, len(src))
dst := out
for len(src) > 0 {
block.Encrypt(dst, src[:bs])
src = src[bs:]
dst = dst[bs:]
}
return base64.StdEncoding.EncodeToString(out), nil
}
/**
* AES解密
* @ciphertext 解密数据,base64格式加密文本
* @key 密钥
* 返回解密文本
*/
func AesDecrypt(cipherText, key string) (string, error) {
src, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return "", err
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
out := make([]byte, len(src))
dst := out
bs := block.BlockSize()
if len(src)%bs != 0 {
return "", errors.New("crypto/cipher: input not full blocks")
}
for len(src) > 0 {
block.Decrypt(dst, src[:bs])
src = src[bs:]
dst = dst[bs:]
}
// out = ZeroUnPadding(out)
out = PKCS5UnPadding(out)
return string(out), nil
}
/**
* AES加密 ECB模式
* @plainText 明文
* @key 密钥
* @返回hex加密文本
*/
func AesEncryptECB(plainText, key string) (string, error) {
keyBytes, err := hex.DecodeString(key)
if err != nil {
return "", errors.New("key error, please input hex type aes key")
}
block, err := aes.NewCipher(keyBytes)
if err != nil {
return "", err
}
if plainText == "" {
return "", errors.New("plainText content empty")
}
ecb := NewECBEncrypter(block)
content := []byte(plainText)
content = PKCS5Padding(content, block.BlockSize())
crypted := make([]byte, len(content))
ecb.CryptBlocks(crypted, content)
return hex.EncodeToString(crypted), nil
}
/**
* AES解密 ECB模式
* @cipherText 解密数据,hex格式加密文本
* @key 密钥
* @返回解密文本
*/
func AesDecryptECB(cipherText string, key string) (string, error) {
data, err := hex.DecodeString(cipherText)
if err != nil {
return "", errors.New("cipherText error, please input hex type aes cipherText")
}
keyBytes, err := hex.DecodeString(key)
if err != nil {
return "", errors.New("key error, please input hex type aes key")
}
cipher, _ := aes.NewCipher([]byte(keyBytes))
decrypted := make([]byte, len(data))
size := 16
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
cipher.Decrypt(decrypted[bs:be], data[bs:be])
}
// remove the padding. The last character in the byte array is the number of padding chars
paddingSize := int(decrypted[len(decrypted)-1])
return string(decrypted[0 : len(decrypted)-paddingSize]), nil
}
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncrypter ecb
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
// mode, using the given Block.
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
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
// mode, using the given Block.
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:]
}
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}