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
package utils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"gin-vue-admin/global"
"strings"
)
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func ZEROPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding) //用0去填充
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
//加密 aes-cbc-128
func AesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
//解密 aes-cbc-128
func AesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, nil
}
// 数据加密
func DataAesEncrypt(Data string) (int, string, error) {
cert := global.GVA_CONFIG.Encrypt
data := []byte(Data)
xpass, err := AesEncrypt(data, []byte(cert.Key))
if err != nil {
return 0, "", err
}
pass64 := base64.StdEncoding.EncodeToString(xpass)
return 1, pass64, nil
}
// 数据解密
func DataAesDecrypt(Data string) (int, string, error) {
cert := global.GVA_CONFIG.Encrypt
Data = strings.Replace(Data, " ", "", -1)
bytesPass, err := base64.StdEncoding.DecodeString(Data)
xpass, err := AesDecrypt(bytesPass, []byte(cert.Key))
if err != nil {
return 0, "", err
}
return 1, string(xpass[:]), nil
}
// 数据加密
func TestDataAesEncrypt(Data string) (int, string, error) {
cert := global.GVA_CONFIG.Encrypt
data := []byte(Data)
xpass, err := TestAesEncrypt(data, []byte(cert.Key))
if err != nil {
return 0, "", err
}
pass64 := base64.StdEncoding.EncodeToString(xpass)
return 1, pass64, nil
}
//加密 aes-cbc-128
func TestAesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = ZEROPadding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}