-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.go
58 lines (51 loc) · 1.29 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
package utils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
)
// 加密实例
type Aes struct {
key [16]byte
}
// 新建实例
func NewAes(passwd string) *Aes {
return &Aes{key: md5.Sum([]byte(passwd))}
}
// 加密数据
func (a *Aes) Enc(src []byte) (data []byte, err error) {
var block cipher.Block
if block, err = aes.NewCipher(a.key[:]); err != nil {
return
}
blockSize := block.BlockSize()
src = pkcs5Padding(src, blockSize)
blockMode := cipher.NewCBCEncrypter(block, a.key[:blockSize])
data = make([]byte, len(src))
blockMode.CryptBlocks(data, src)
return
}
// 解密数据
func (a *Aes) Dec(src []byte) (data []byte, err error) {
var block cipher.Block
if block, err = aes.NewCipher(a.key[:]); err != nil {
return
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, a.key[:blockSize])
data = make([]byte, len(src))
blockMode.CryptBlocks(data, src)
data = pkcs5UnPadding(data)
return
}
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func pkcs5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}