-
Notifications
You must be signed in to change notification settings - Fork 6
/
cbc.go
41 lines (38 loc) · 1 KB
/
cbc.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
package aescrypto
import (
"crypto/aes"
"crypto/cipher"
)
// aes、cbc、pkcs7 加密
func AesCbcPkcs7Encrypt(plantText, key, ikey []byte) ([]byte, error) {
block, err := aes.NewCipher(key) //选择加密算法
if err != nil {
return nil, err
}
plantText = PKCS7Padding(plantText, block.BlockSize())
if ikey == nil {
ikey = key
}
blockModel := cipher.NewCBCEncrypter(block, ikey[:block.BlockSize()])
ciphertext := make([]byte, len(plantText))
blockModel.CryptBlocks(ciphertext, plantText)
return ciphertext, nil
}
// aes、cbc、pkcs7 解密
func AesCbcPkcs7Decrypt(ciphertext, key, ikey []byte) ([]byte, error) {
block, err := aes.NewCipher(key) //选择解密算法
if err != nil {
return nil, err
}
if ikey == nil {
ikey = key
}
blockModel := cipher.NewCBCDecrypter(block, ikey[:block.BlockSize()])
plantText := make([]byte, len(ciphertext))
blockModel.CryptBlocks(plantText, ciphertext)
plantText,err = PKCS7UnPadding(plantText)
if err != nil {
return nil, err
}
return plantText, nil
}