V2bX/common/crypt/aes.go
2023-07-15 18:13:16 +08:00

31 lines
579 B
Go

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, 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, len(data))
a.Decrypt(de, d)
return string(de), nil
}