-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
54 lines (50 loc) · 1.15 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
package encrypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"io"
)
func EncryptText32(key, text []byte) ([]byte, error) {
block, err := getCipher(key)
if err != nil {
return nil, err
}
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 nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(b))
return cipherText, nil
}
func DecryptText32(key, text []byte) ([]byte, error) {
block, err := getCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, 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 nil, err
}
return data, nil
}
func getCipher(key []byte) (cipher.Block, error) {
key = MD5OfBytes(key)
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return c, nil
}