forked from foxundermoon/WechatHelper708
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_aes.go
64 lines (57 loc) · 1.7 KB
/
utils_aes.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
package wxxx
import (
"bytes"
"crypto/aes"
"crypto/cipher"
)
func PKCS7Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
paddingText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, paddingText...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unPadding := int(origData[length-1])
return origData[:(length - unPadding)]
}
//noinspection ALL
func AESEcbDecrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
decrypted := make([]byte, len(data))
size := block.BlockSize()
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
block.Decrypt(decrypted[bs:be], data[bs:be])
}
return PKCS7UnPadding(decrypted)
}
func AESCbcDecrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
decrypted := make([]byte, len(data))
iv := key
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(decrypted, data)
return PKCS7UnPadding(decrypted)
}
func AESEcbEncrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
data = PKCS7Padding(data, block.BlockSize())
encrypted := make([]byte, len(data))
size := block.BlockSize()
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
block.Encrypt(encrypted[bs:be], data[bs:be])
}
return encrypted
}
func AESCbcEncrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
blockSize := block.BlockSize()
data = PKCS7Padding(data, blockSize)
encrypted := make([]byte, len(data))
iv := key
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted, data)
//for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
// block.Encrypt(encrypted[bs:be], data[bs:be])
//}
return encrypted
}