-
Notifications
You must be signed in to change notification settings - Fork 14
/
aes-cbc-pkcs7.hex.go
80 lines (72 loc) · 2.01 KB
/
aes-cbc-pkcs7.hex.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
// golang版aes-cbc-pkcs7加密解密hex字符串输入输出
type AesHex struct {
key []byte // 允许16,24,32字节长度
iv []byte // 只允许16字节长度
}
func (s AesHex) Encrypt(text []byte) (string, error) {
if len(text) == 0 {
return "", nil
}
//生成cipher.Block 数据块
block, err := aes.NewCipher(s.key)
if err != nil {
return "", err
}
//填充内容,如果不足16位字符
blockSize := block.BlockSize()
originData := s.pad(text, blockSize)
//加密方式
blockMode := cipher.NewCBCEncrypter(block, s.iv)
//加密,输出到[]byte数组
crypted := make([]byte, len(originData))
blockMode.CryptBlocks(crypted, originData)
return hex.EncodeToString(crypted), nil
}
func (s AesHex) Decrypt(text string) ([]byte, error) {
if len(text) == 0 {
return []byte(text), nil
}
decodeData, err := hex.DecodeString(text)
if err != nil {
return []byte(text), err
}
if len(decodeData) == 0 {
return []byte(text), nil
}
//生成密码数据块cipher.Block
block, _ := aes.NewCipher(s.key)
//解密模式
blockMode := cipher.NewCBCDecrypter(block, s.iv)
//输出到[]byte数组
originData := make([]byte, len(decodeData))
blockMode.CryptBlocks(originData, decodeData)
//去除填充,并返回
return s.unPad(originData), nil
}
func (s AesHex) pad(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padText...)
}
func (s AesHex) unPad(ciphertext []byte) []byte {
length := len(ciphertext)
// 去掉最后一次的padding
unPadding := int(ciphertext[length-1])
return ciphertext[:(length - unPadding)]
}
func main() {
var text = "this is Aes encryption with pkcs7"
aes := AesHex{key: []byte("1111111111111111"), iv: []byte("2222222222222222")}
enc, err := aes.Encrypt([]byte(text))
fmt.Println("enc", enc, err)
dec, err := aes.Decrypt(enc)
fmt.Println("dec", string(dec), err)
}