-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_manager.go
47 lines (35 loc) · 994 Bytes
/
aes_manager.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
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
)
var default_iv string = "22277966616d9bc47177bd02603d08c9a67d5380d0fe8cf3b44438dff7b9"
func EncryptAes(data []byte, key string) ([]byte, error) {
encrypted := make([]byte, len(data))
iv, err := hex.DecodeString(default_iv)
if err != nil {
return encrypted, err
}
aesBlockEncrypter, err := aes.NewCipher([]byte(key))
if err != nil {
return encrypted, err
}
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
aesEncrypter.XORKeyStream(encrypted, data)
return encrypted, nil
}
func DecryptAes(data []byte, key string) ([]byte, error) {
decrypted := make([]byte, len(data))
iv, err := hex.DecodeString(default_iv)
if err != nil {
return decrypted, err
}
aesBlockDecrypter, err := aes.NewCipher([]byte(key))
if err != nil {
return decrypted, err
}
aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv)
aesDecrypter.XORKeyStream(decrypted, data)
return decrypted, nil
}