using System; using System.Security.Cryptography; namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities { using SKIT.FlurlHttpClient.Primitives; /// /// AES 算法工具类。 /// public static class AESUtility { /// /// 基于 ECB 模式解密数据。 /// /// AES 密钥字节数组。 /// 待解密数据字节数组。 /// 解密后的数据字节数组。 public static byte[] DecryptWithECB(byte[] keyBytes, byte[] cipherBytes) { if (keyBytes is null) throw new ArgumentNullException(nameof(keyBytes)); if (cipherBytes is 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 EncodedString DecryptWithECB(EncodedString encodingKey, EncodedString encodingCipher) { if (encodingKey.Value is null) throw new ArgumentNullException(nameof(encodingKey)); if (encodingCipher.Value is null) throw new ArgumentNullException(nameof(encodingCipher)); byte[] plainBytes = DecryptWithECB( keyBytes: EncodedString.FromString(encodingKey, fallbackEncodingKind: EncodingKinds.Base64), cipherBytes: EncodedString.FromString(encodingCipher, fallbackEncodingKind: EncodingKinds.Base64) ); return EncodedString.ToLiteralString(plainBytes); } } }