plugin/utils/aes.go

33 lines
747 B
Go
Raw Normal View History

2024-11-14 18:33:56 +08:00
package utils
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func AesDecode(data string, key []byte) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return "", fmt.Errorf("base64 decode error: %v", err)
}
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("aes new cipher error: %v", err)
}
if len(decoded) < aes.BlockSize {
return "", fmt.Errorf("ciphertext too short")
}
decrypted := make([]byte, len(decoded))
mode := cipher.NewCBCDecrypter(block, make([]byte, aes.BlockSize))
mode.CryptBlocks(decrypted, decoded)
padding := decrypted[len(decrypted)-1]
decrypted = decrypted[:len(decrypted)-int(padding)]
return string(decrypted), nil
}