2023-07-14 00:54:09 -04:00
|
|
|
package crypt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"encoding/base64"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AesEncrypt(data []byte, key []byte) (string, error) {
|
|
|
|
a, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-07-15 06:13:16 -04:00
|
|
|
en := make([]byte, len(data))
|
2023-07-14 00:54:09 -04:00
|
|
|
a.Encrypt(en, data)
|
|
|
|
return base64.StdEncoding.EncodeToString(en), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AesDecrypt(data string, key []byte) (string, error) {
|
|
|
|
d, err := base64.StdEncoding.DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
a, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-07-15 06:13:16 -04:00
|
|
|
de := make([]byte, len(data))
|
2023-07-14 00:54:09 -04:00
|
|
|
a.Decrypt(de, d)
|
|
|
|
return string(de), nil
|
|
|
|
}
|