-
Notifications
You must be signed in to change notification settings - Fork 2
/
aes_encrypt.go
137 lines (121 loc) · 3.45 KB
/
aes_encrypt.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package secret
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"errors"
"fmt"
"log"
"github.com/spf13/cast"
)
const AesKeyLength = 16
var AesBaseSpecialSign = "!@a%$bc.de,l%$fgqweruriskn&#@xl784zm321apgiw"
var AesBaseSpecialSignLength = len(AesBaseSpecialSign)
type AesEncrypt struct {
SpecialSign string // 加解密都会基于这一串字符,如果没有会基于 AesBaseSpecialSign.
Key string // 密钥,建议是 5-8位的密钥
}
func NewAesEncrypt(specialSign, key string) (*AesEncrypt, error) {
if specialSign == "" {
specialSign = AesBaseSpecialSign
}
specialSignLength := len(specialSign)
if specialSignLength+len(key) < AesKeyLength {
log.Printf("the length of specialSign and key less %v ", TripleDesKeyLength)
if specialSignLength%2 == 0 {
specialSign += AesBaseSpecialSign[:AesKeyLength-len(specialSign)]
} else {
specialSign += AesBaseSpecialSign[AesBaseSpecialSignLength-AesKeyLength:]
}
}
if specialSignLength > AesKeyLength {
if specialSignLength%2 == 0 {
specialSign = specialSign[:AesKeyLength+1]
} else {
specialSign = specialSign[len(specialSign)-AesKeyLength:]
}
}
if key == "" {
return nil, errors.New("need the key to encrypt, please add it. ")
}
return &AesEncrypt{
SpecialSign: specialSign,
Key: key,
}, nil
}
// GetPrefix 根据长短来判断前缀
func (a *AesEncrypt) getPrefix(length int) string {
if len(a.SpecialSign)%2 == 0 {
return a.SpecialSign[len(a.SpecialSign)-length:]
}
return a.SpecialSign[:length]
}
// GenerateAesKey 生成AES密钥
func (a *AesEncrypt) generateAesKey(id interface{}) []byte {
idStr := cast.ToString(id)
length := AesKeyLength - len(idStr) - len(a.Key)
buf := make([]byte, 0, AesKeyLength)
prefix := a.getPrefix(length)
buf = append(buf, []byte(prefix)...)
buf = append(buf, []byte(idStr)...)
buf = append(buf, []byte(a.Key)...)
return buf
}
// SecretEncrypt 加密
func (a *AesEncrypt) SecretEncrypt(secret interface{}, fields ...interface{}) string {
number := 0
for i := range fields {
number += fields[i].(int)
}
if secret != "" {
aesKey := a.generateAesKey(number)
ans, _ := a.aesEncrypt(cast.ToString(secret), aesKey)
return ans
}
return ""
}
// SecretDecrypt 解密
func (a *AesEncrypt) SecretDecrypt(secret interface{}, fields ...interface{}) string {
number := 0
for i := range fields {
number += fields[i].(int)
}
if secret != "" {
aesKey := a.generateAesKey(number)
b, _ := a.aesDecrypt(cast.ToString(secret), aesKey)
return string(b)
}
return ""
}
// AesEncrypt AES加密
func (a *AesEncrypt) aesEncrypt(encodeStr string, key []byte) (string, error) {
encodeByte := []byte(encodeStr)
// 根据key,生成密文
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
blockSize := block.BlockSize()
encodeByte = pkcs5Padding(encodeByte, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key)
crypted := make([]byte, len(encodeByte))
blockMode.CryptBlocks(crypted, encodeByte)
hexStr := fmt.Sprintf("%x", crypted)
return hexStr, nil
}
// AesDecrypt AES 解密
func (a *AesEncrypt) aesDecrypt(decodeStr string, key []byte) ([]byte, error) {
decodeBytes, err := hex.DecodeString(decodeStr)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, key)
origData := make([]byte, len(decodeBytes))
blockMode.CryptBlocks(origData, decodeBytes)
origData = pkcs5UnPadding(origData)
return origData, nil
}