-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
88 lines (71 loc) · 2.05 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
/*
* Author: fasion
* Created time: 2023-01-11 10:36:39
* Last Modified by: fasion
* Last Modified time: 2023-01-12 15:13:46
*/
package baseutils
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/base64"
"math/rand"
)
type AesCipher struct {
block cipher.Block
blockBytes int
}
func NewAesCipher(key []byte, bits int) (*AesCipher, error) {
// 对密钥算哈希,这样密钥就不用固定长度
hash := sha256.Sum256(key)
key = hash[:bits>>3]
// 以哈希值为密钥,初始化加密器
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return &AesCipher{
block: block,
blockBytes: block.BlockSize(),
}, nil
}
func (a *AesCipher) Encrypt(plaintext []byte, b64 bool) ([]byte, error) {
// 对明文进行填充
plaintext = Pkcs5Padding(plaintext, a.blockBytes)
// 随机生成初始向量
iv := make([]byte, a.blockBytes)
if _, err := rand.Read(iv); err != nil {
return nil, err
}
// 为密文分配空间(为拼接IV预留空间),并完成加密
ciphertext := make([]byte, len(plaintext), len(plaintext)+len(iv))
cipher.NewCBCEncrypter(a.block, iv).CryptBlocks(ciphertext, plaintext)
// 将IV拼接在密文后面,两者合在一起作为加密结果
result := append(ciphertext, iv...)
// 由于结果是无序的二进制字节序列,可以做BASE64编码(可选)
if b64 {
result = Base64Encode(base64.StdEncoding, result)
}
return result, nil
}
func (a *AesCipher) Decrypt(cipherdata []byte, b64 bool) ([]byte, error) {
// 如果做了BASE64编码,需要先解密
if b64 {
var err error
cipherdata, err = Base64Decode(base64.StdEncoding, cipherdata)
if err != nil {
return nil, err
}
}
// 计算密文长度
textBytes := len(cipherdata) - a.blockBytes
// 切出密文和IV
iv := cipherdata[textBytes:]
ciphertext := cipherdata[:textBytes]
// 为明文分配空间并解密
plaintext := make([]byte, len(ciphertext))
cipher.NewCBCDecrypter(a.block, iv).CryptBlocks(plaintext, ciphertext)
// 取出填充字符
return Pkcs5Unpadding(plaintext), nil
}