feat(wxapi): 移除 WxBizMsgCryptor 工具类

This commit is contained in:
Fu Diwei
2022-02-28 21:29:31 +08:00
parent 23d9d247ef
commit 62b871023c
4 changed files with 34 additions and 23 deletions

View File

@@ -1,6 +1,14 @@
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace SKIT.FlurlHttpClient.Wechat.Api
{
@@ -29,7 +37,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
public string Signature { get; set; } = default!;
}
private static TEvent InnerDeserializeEventFromJson<TEvent>(this WechatApiClient client, string callbackJson)
private static TEvent InnerDeserializeEventFromJson<TEvent>(WechatApiClient client, string callbackJson)
where TEvent : WechatApiEvent
{
if (client == null) throw new ArgumentNullException(nameof(client));
@@ -40,7 +48,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (callbackJson.Contains("\"Encrypt\""))
{
InnerEncryptedEvent encryptedEvent = client.JsonSerializer.Deserialize<InnerEncryptedEvent>(callbackJson);
callbackJson = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
callbackJson = Utilities.WechatEventMessageCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
}
return client.JsonSerializer.Deserialize<TEvent>(callbackJson);
@@ -55,7 +63,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
}
}
private static TEvent InnerDeserializeEventFromXml<TEvent>(this WechatApiClient client, string callbackXml)
private static TEvent InnerDeserializeEventFromXml<TEvent>(WechatApiClient client, string callbackXml)
where TEvent : WechatApiEvent
{
if (client == null) throw new ArgumentNullException(nameof(client));
@@ -67,7 +75,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
{
XDocument xDocument = XDocument.Parse(callbackXml);
string encryptedData = xDocument.Root.Element("Encrypt").Value;
callbackXml = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
callbackXml = Utilities.WechatEventMessageCryptor.AESDecrypt(cipherText: encryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
}
return Utilities.XmlUtility.Deserialize<TEvent>(callbackXml);
@@ -163,12 +171,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
{
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
string nonce = DateTimeOffset.Now.Ticks.ToString("x");
string cipher = Utilities.WxBizMsgCryptor.AESEncrypt(
string cipher = Utilities.WechatEventMessageCryptor.AESEncrypt(
plainText: json,
encodingAESKey: client.Credentials.PushEncodingAESKey!,
appId: client.Credentials.AppId
);
string sign = Utilities.WxBizMsgCryptor.GenerateSignature(
string sign = Utilities.WechatEventMessageCryptor.GenerateSignature(
sToken: client.Credentials.PushToken!,
sTimestamp: timestamp,
sNonce: nonce,
@@ -223,13 +231,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
try
{
string cipher = Utilities.WxBizMsgCryptor.AESEncrypt(
string cipher = Utilities.WechatEventMessageCryptor.AESEncrypt(
plainText: xml,
encodingAESKey: client.Credentials.PushEncodingAESKey!,
appId: client.Credentials.AppId
);
xml = Utilities.WxBizMsgCryptor.WrapXml(sToken: client.Credentials.PushToken!, sMsgEncrypt: cipher);
xml = Utilities.WechatEventMessageCryptor.WrapXml(sToken: client.Credentials.PushToken!, sMsgEncrypt: cipher);
}
catch (Exception ex)
{
@@ -279,7 +287,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
try
{
var encryptedEvent = client.JsonSerializer.Deserialize<InnerEncryptedEvent>(callbackJson);
return Utilities.WxBizMsgCryptor.VerifySignature(
return Utilities.WechatEventMessageCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,
@@ -313,7 +321,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
XDocument xDoc = XDocument.Parse(callbackXml);
string? msgEncrypt = xDoc.Root?.Element("Encrypt")?.Value;
return Utilities.WxBizMsgCryptor.VerifySignature(
return Utilities.WechatEventMessageCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,

View File

@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("SKIT.FlurlHttpClient.Wechat.Api.UnitTests")]

View File

@@ -10,7 +10,7 @@ using System.Xml;
namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
{
public static class WxBizMsgCryptor
internal static class WechatEventMessageCryptor
{
private const int AES_KEY_SIZE = 256;
private const int AES_BLOCK_SIZE = 128;
@@ -74,15 +74,15 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
return res;
}
private static byte[] AESDecrypt(byte[] keyBytes, byte[] ivBytes, byte[] ciperBytes)
private static byte[] AESDecrypt(byte[] keyBytes, byte[] ivBytes, byte[] cipherBytes)
{
if (keyBytes == null) throw new ArgumentNullException(nameof(keyBytes));
if (ivBytes == null) throw new ArgumentNullException(nameof(ivBytes));
if (ciperBytes == null) throw new ArgumentNullException(nameof(ciperBytes));
if (cipherBytes == null) throw new ArgumentNullException(nameof(cipherBytes));
using var aes = Aes.Create();
aes.KeySize = 256;
aes.BlockSize = 128;
aes.KeySize = AES_KEY_SIZE;
aes.BlockSize = AES_BLOCK_SIZE;
aes.Mode = CipherMode.CBC;
//aes.Padding = PaddingMode.PKCS7;
aes.Padding = PaddingMode.None;
@@ -93,9 +93,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
byte[] bMsg = new byte[ciperBytes.Length + 32 - ciperBytes.Length % 32];
Array.Copy(ciperBytes, bMsg, ciperBytes.Length);
cs.Write(ciperBytes, 0, ciperBytes.Length);
byte[] msgBytes = new byte[cipherBytes.Length + 32 - cipherBytes.Length % 32];
Array.Copy(cipherBytes, msgBytes, cipherBytes.Length);
cs.Write(cipherBytes, 0, cipherBytes.Length);
byte[] plainBytes = Decode2(ms.ToArray());
return plainBytes;
@@ -148,7 +148,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
byte[] keyBytes = Convert.FromBase64String(encodingAESKey + "=");
byte[] ivBytes = new byte[16];
Array.Copy(keyBytes, ivBytes, 16);
byte[] btmpMsg = AESDecrypt(ciperBytes: cipherBytes, ivBytes: ivBytes, keyBytes: keyBytes);
byte[] btmpMsg = AESDecrypt(cipherBytes: cipherBytes, ivBytes: ivBytes, keyBytes: keyBytes);
int len = BitConverter.ToInt32(btmpMsg, 16);
len = IPAddress.NetworkToHostOrder(len);