-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfanEncryptTool.go
60 lines (56 loc) · 1.57 KB
/
fanEncryptTool.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
package fanTool
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
)
// GenerateEncryptKey 生成加密密钥
func GenerateEncryptKey() string {
encryptKey := make([]byte, 32)
_, _ = rand.Read(encryptKey)
return base64.StdEncoding.EncodeToString(encryptKey)
}
// Encrypt 加密
func Encrypt(data, key string) (string, error) {
KeyBt, _ := base64.StdEncoding.DecodeString(key)
block, err := aes.NewCipher(KeyBt)
if err != nil {
return "", err
}
//补码
dataByte := []byte(data)
blockSize := block.BlockSize()
padding := blockSize - len(dataByte)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
dataByte = append(dataByte, padText...)
ciphertext := make([]byte, aes.BlockSize+len(dataByte))
iv := ciphertext[:aes.BlockSize]
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
cipher.NewCBCEncrypter(block, iv).CryptBlocks(ciphertext[aes.BlockSize:], dataByte)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// Decrypt 解密
func Decrypt(data, key string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return "", err
}
KeyBt, _ := base64.StdEncoding.DecodeString(key)
block, err := aes.NewCipher(KeyBt)
if err != nil {
return "", err
}
iv := decoded[:aes.BlockSize]
ciphertext := decoded[aes.BlockSize:]
cipher.NewCBCDecrypter(block, iv).CryptBlocks(ciphertext, ciphertext)
//去除补码
length := len(ciphertext)
unPadding := int(ciphertext[length-1])
plaintext := ciphertext[:(length - unPadding)]
return string(plaintext), nil
}