-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
94 lines (76 loc) · 1.85 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
93
94
package aesecb
import (
"bytes"
"crypto/aes"
"encoding/hex"
"strings"
)
/**
* 输入输出都是string类型,简化使用
*/
func EncryptString(plantText, key string) (string, error) {
ret, err := Encrypt([]byte(plantText), []byte(key))
if err != nil {
return plantText, err
}
return strings.ToUpper(hex.EncodeToString(ret)), nil
}
func Encrypt(plantText, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key) //选择加密算法
if err != nil {
return nil, err
}
plantText = ECBZeroPadding(plantText)
blockModel := NewECBEncrypter(block)
ciphertext := make([]byte, len(plantText))
blockModel.CryptBlocks(ciphertext, plantText)
return ciphertext, nil
}
/**
* ECB/NoPadding
*
*/
func ECBZeroPadding(plantText []byte) []byte {
bit16 := len(plantText) % 16
if bit16 == 0 {
return plantText
} else {
// 计算补足的位数,填补16的位数,例如 10 = 16, 17 = 32, 33 = 48
return append(plantText, bytes.Repeat([]byte{byte(0)}, 16-bit16)...)
}
}
func DecryptString(ciphertext, key string) (string, error) {
ciphertext1, err := hex.DecodeString(ciphertext)
if err != nil {
return ciphertext, err
}
ret, err1 := Decrypt(ciphertext1, []byte(key))
if err1 != nil {
return ciphertext, err1
}
return string(ret), nil
}
func Decrypt(ciphertext, key []byte) ([]byte, error) {
keyBytes := []byte(key)
block, err := aes.NewCipher(keyBytes)
if err != nil {
return nil, err
}
blockModel := NewECBDecrypter(block)
plantText := make([]byte, len(ciphertext))
blockModel.CryptBlocks(plantText, ciphertext)
plantText = ECBUnZeroPadding(plantText)
return plantText, nil
}
func ECBUnZeroPadding(plantText []byte) []byte {
l := len(plantText)
//大于16,需要把结尾是0的截取掉
if l >= 16 {
for i := l; i > l-16; i-- {
if plantText[i-1] != byte(0) {
return plantText[0:i]
}
}
}
return plantText
}