2021-09-18 12:08:45 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
|
|
|
|
|
{
|
|
|
|
|
public static class AESUtility
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 解密数据。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="keyBytes">AES 密钥字节数组。</param>
|
|
|
|
|
/// <param name="ivBytes">加密使用的初始化向量字节数组。</param>
|
|
|
|
|
/// <param name="cipherBytes">待解密数据字节数组。</param>
|
|
|
|
|
/// <returns>解密后的数据字节数组。</returns>
|
2021-09-03 14:23:54 +08:00
|
|
|
|
public static byte[] Decrypt(byte[] keyBytes, byte[] ivBytes, byte[] cipherBytes)
|
2021-09-18 12:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
if (keyBytes == null) throw new ArgumentNullException(nameof(keyBytes));
|
|
|
|
|
if (ivBytes == null) throw new ArgumentNullException(nameof(ivBytes));
|
2021-09-03 14:23:54 +08:00
|
|
|
|
if (cipherBytes == null) throw new ArgumentNullException(nameof(cipherBytes));
|
|
|
|
|
|
|
|
|
|
using (SymmetricAlgorithm aes = Aes.Create())
|
|
|
|
|
{
|
|
|
|
|
aes.Key = keyBytes;
|
|
|
|
|
aes.IV = ivBytes;
|
|
|
|
|
|
|
|
|
|
using (Stream ms = new MemoryStream(cipherBytes))
|
|
|
|
|
using (Stream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
|
|
|
|
|
{
|
|
|
|
|
byte[] plainBytes = new byte[cipherBytes.Length];
|
|
|
|
|
cs.Read(plainBytes, 0, plainBytes.Length);
|
|
|
|
|
return plainBytes;
|
|
|
|
|
}
|
2021-09-18 12:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 解密数据。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="encodingKey">经 Base64 编码后的 AES 密钥。</param>
|
|
|
|
|
/// <param name="encodingIV">经 Base64 编码后的 AES 初始化向量。</param>
|
|
|
|
|
/// <param name="encodingCipherText">经 Base64 编码后的待解密数据。</param>
|
|
|
|
|
/// <returns>解密后的文本数据。</returns>
|
2021-09-03 14:23:54 +08:00
|
|
|
|
public static string Decrypt(string encodingKey, string encodingIV, string encodingCipherText)
|
2021-09-18 12:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
if (encodingKey == null) throw new ArgumentNullException(nameof(encodingKey));
|
2021-09-03 14:23:54 +08:00
|
|
|
|
if (encodingCipherText == null) throw new ArgumentNullException(nameof(encodingCipherText));
|
|
|
|
|
|
2021-09-18 12:08:45 +08:00
|
|
|
|
byte[] plainBytes = Decrypt(
|
|
|
|
|
keyBytes: Convert.FromBase64String(encodingKey),
|
|
|
|
|
ivBytes: Convert.FromBase64String(encodingIV),
|
|
|
|
|
cipherBytes: Convert.FromBase64String(encodingCipherText)
|
|
|
|
|
);
|
|
|
|
|
return Encoding.UTF8.GetString(plainBytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|