V2bX/common/crypt/aes.go

31 lines
585 B
Go
Raw Normal View History

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
}
en := make([]byte, 0, len(data))
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
}
de := make([]byte, 0, len(data))
a.Decrypt(de, d)
return string(de), nil
}