using System;
using System.Security.Cryptography;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
{
///
/// AES 算法工具类。
///
public static class AESUtility
{
///
/// 基于 ECB 模式解密数据。
///
/// AES 密钥字节数组。
/// 待解密数据字节数组。
/// 解密后的数据字节数组。
public static byte[] DecryptWithECB(byte[] keyBytes, byte[] cipherBytes)
{
if (keyBytes == null) throw new ArgumentNullException(nameof(keyBytes));
if (cipherBytes == null) throw new ArgumentNullException(nameof(cipherBytes));
using (SymmetricAlgorithm aes = Aes.Create())
{
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
using ICryptoTransform transform = aes.CreateDecryptor();
return transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
}
///
/// 基于 ECB 模式解密数据。
///
/// 经 Base64 编码后的 AES 密钥。
/// 经 Base64 编码后的待解密数据。
/// 解密后的文本数据。
public static string DecryptWithECB(string encodingKey, string encodingCipherText)
{
if (encodingKey == null) throw new ArgumentNullException(nameof(encodingKey));
if (encodingCipherText == null) throw new ArgumentNullException(nameof(encodingCipherText));
byte[] plainBytes = DecryptWithECB(
keyBytes: Convert.FromBase64String(encodingKey),
cipherBytes: Convert.FromBase64String(encodingCipherText)
);
return Encoding.UTF8.GetString(plainBytes);
}
}
}