mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-12-29 18:04:42 +08:00
test(wxapi): 增加 AES-CBC 解密的单元测试用例
This commit is contained in:
@@ -35,6 +35,31 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基于 CBC 模式加密数据。
|
||||
/// </summary>
|
||||
/// <param name="keyBytes">AES 密钥字节数组。</param>
|
||||
/// <param name="ivBytes">加密使用的初始化向量字节数组。</param>
|
||||
/// <param name="plainBytes">待加密数据字节数组。</param>
|
||||
/// <returns>加密后的数据字节数组。</returns>
|
||||
public static byte[] EncryptWithCBC(byte[] keyBytes, byte[] ivBytes, byte[] plainBytes)
|
||||
{
|
||||
if (keyBytes == null) throw new ArgumentNullException(nameof(keyBytes));
|
||||
if (ivBytes == null) throw new ArgumentNullException(nameof(ivBytes));
|
||||
if (plainBytes == null) throw new ArgumentNullException(nameof(plainBytes));
|
||||
|
||||
using (SymmetricAlgorithm aes = Aes.Create())
|
||||
{
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Key = keyBytes;
|
||||
aes.IV = ivBytes;
|
||||
|
||||
using ICryptoTransform transform = aes.CreateEncryptor();
|
||||
return transform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基于 CBC 模式解密数据。
|
||||
/// </summary>
|
||||
@@ -54,5 +79,25 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
|
||||
);
|
||||
return Encoding.UTF8.GetString(plainBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基于 CBC 模式加密数据。
|
||||
/// </summary>
|
||||
/// <param name="encodingKey">经 Base64 编码后的 AES 密钥。</param>
|
||||
/// <param name="encodingIV">经 Base64 编码后的 AES 初始化向量。</param>
|
||||
/// <param name="plainText">待加密文本。</param>
|
||||
/// <returns>经 Base64 编码的加密后的数据。</returns>
|
||||
public static string EncryptWithCBC(string encodingKey, string encodingIV, string plainText)
|
||||
{
|
||||
if (encodingKey == null) throw new ArgumentNullException(nameof(encodingKey));
|
||||
if (plainText == null) throw new ArgumentNullException(nameof(plainText));
|
||||
|
||||
byte[] plainBytes = EncryptWithCBC(
|
||||
keyBytes: Convert.FromBase64String(encodingKey),
|
||||
ivBytes: Convert.FromBase64String(encodingIV),
|
||||
plainBytes: Encoding.UTF8.GetBytes(plainText)
|
||||
);
|
||||
return Convert.ToBase64String(plainBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,18 +11,19 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
|
||||
internal static class XmlUtility
|
||||
{
|
||||
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||
private static Hashtable _serializers = new Hashtable();
|
||||
private static readonly Hashtable _xmlSerializers = new Hashtable();
|
||||
private static readonly XmlRootAttribute _xmlRoot = new XmlRootAttribute("xml");
|
||||
|
||||
private static XmlSerializer GetTypedSerializer(Type type)
|
||||
{
|
||||
if (type == null) throw new ArgumentNullException(nameof(type));
|
||||
|
||||
string skey = type.AssemblyQualifiedName ?? type.GetHashCode().ToString();
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer?)_serializers[skey];
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer?)_xmlSerializers[skey];
|
||||
if (xmlSerializer == null)
|
||||
{
|
||||
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||
_serializers[skey] = xmlSerializer;
|
||||
xmlSerializer = new XmlSerializer(type, _xmlRoot);
|
||||
_xmlSerializers[skey] = xmlSerializer;
|
||||
}
|
||||
|
||||
return xmlSerializer;
|
||||
|
||||
@@ -11,18 +11,19 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
||||
internal static class XmlUtility
|
||||
{
|
||||
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||
private static Hashtable _serializers = new Hashtable();
|
||||
private static readonly Hashtable _xmlSerializers = new Hashtable();
|
||||
private static readonly XmlRootAttribute _xmlRoot = new XmlRootAttribute("xml");
|
||||
|
||||
private static XmlSerializer GetTypedSerializer(Type type)
|
||||
{
|
||||
if (type == null) throw new ArgumentNullException(nameof(type));
|
||||
|
||||
string skey = type.AssemblyQualifiedName ?? type.GetHashCode().ToString();
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer?)_serializers[skey];
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer?)_xmlSerializers[skey];
|
||||
if (xmlSerializer == null)
|
||||
{
|
||||
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||
_serializers[skey] = xmlSerializer;
|
||||
xmlSerializer = new XmlSerializer(type, _xmlRoot);
|
||||
_xmlSerializers[skey] = xmlSerializer;
|
||||
}
|
||||
|
||||
return xmlSerializer;
|
||||
|
||||
@@ -11,18 +11,19 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
|
||||
internal static class XmlUtility
|
||||
{
|
||||
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||
private static Hashtable _serializers = new Hashtable();
|
||||
private static readonly Hashtable _xmlSerializers = new Hashtable();
|
||||
private static readonly XmlRootAttribute _xmlRoot = new XmlRootAttribute("xml");
|
||||
|
||||
private static XmlSerializer GetTypedSerializer(Type type)
|
||||
{
|
||||
if (type == null) throw new ArgumentNullException(nameof(type));
|
||||
|
||||
string skey = type.AssemblyQualifiedName ?? type.GetHashCode().ToString();
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer?)_serializers[skey];
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer?)_xmlSerializers[skey];
|
||||
if (xmlSerializer == null)
|
||||
{
|
||||
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||
_serializers[skey] = xmlSerializer;
|
||||
xmlSerializer = new XmlSerializer(type, _xmlRoot);
|
||||
_xmlSerializers[skey] = xmlSerializer;
|
||||
}
|
||||
|
||||
return xmlSerializer;
|
||||
|
||||
Reference in New Issue
Block a user