-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaes.go
92 lines (72 loc) · 2.14 KB
/
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
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
package fun
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"errors"
"fmt"
)
// AesCBCEncrypt Aes CBC 对称加密, key 的长度决定 AES-128, AES-192, or AES-256
func AesCBCEncrypt(text string, key string, iv string) (string, error) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
textBytes := Bytes(text)
keyBytes := Bytes(key)
ivBytes := Bytes(iv)
block, err := aes.NewCipher(keyBytes)
if err != nil {
return "", errors.New(err.Error())
}
// 对数据进行填充,使其满足加密块大小,加密块大小为 16 字节
blockSize := block.BlockSize()
paddingText := pKCS7Padding(textBytes, blockSize)
// 创建加密块链,使用 CBC 加密模式,iv 的长度需要和 block.BlockSize() 一致
mode := cipher.NewCBCEncrypter(block, ivBytes)
// 加密数据
cipherText := make([]byte, len(paddingText))
mode.CryptBlocks(cipherText, paddingText)
cipherHex := hex.EncodeToString(cipherText)
return cipherHex, nil
}
// AesCBCDecrypt Aes CBC 对称加密
func AesCBCDecrypt(cipherStr string, key string, iv string) (string, error) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
cipherBytes, err := hex.DecodeString(cipherStr)
if err != nil {
return "", errors.New(err.Error())
}
keyBytes := Bytes(key)
ivBytes := Bytes(iv)
// 创建解密器
block, err := aes.NewCipher(keyBytes)
if err != nil {
return "", errors.New(err.Error())
}
// 创建解密块链
mode := cipher.NewCBCDecrypter(block, ivBytes)
// 解密数据
textBytes := make([]byte, len(cipherBytes))
mode.CryptBlocks(textBytes, cipherBytes)
textBytes = pKCS7UnPadding(textBytes)
return String(textBytes), nil
}
// pKCS7Padding 对数据进行填充,满足加密块大小
func pKCS7Padding(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padText...)
}
// pKCS7UnPadding 去除填充的数据
func pKCS7UnPadding(data []byte) []byte {
length := len(data)
unPadding := int(data[length-1])
return data[:(length - unPadding)]
}