/
security_aes.go
62 lines (53 loc) · 1.32 KB
/
security_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
package helper
import (
"io"
"fmt"
"errors"
"crypto/aes"
"crypto/rand"
"crypto/cipher"
"encoding/hex"
"encoding/base64"
)
func AesEncrypt(plainText string, secretKey string) (string, error) {
// key must be 32 bytes or 32 character
key := []byte(secretKey)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
text := []byte(plainText)
b := base64.StdEncoding.EncodeToString(text)
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
return fmt.Sprintf("%0x\n", ciphertext), nil
}
func AesDecrypt(cipherText string, secretKey string) (string, error) {
// key must be 32 bytes or 32 character
key := []byte(secretKey)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
text, err := hex.DecodeString(cipherText)
if err != nil {
return "", err
}
if len(text) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return "", err
}
return string(data), nil
}