feat(work): 移除 WxBizMsgCryptor 工具类

This commit is contained in:
Fu Diwei 2022-02-28 21:37:11 +08:00
parent 62b871023c
commit 30405cc55f
7 changed files with 66 additions and 70 deletions

View File

@ -1,14 +1,6 @@
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
{
@ -47,8 +39,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
{
if (callbackJson.Contains("\"Encrypt\""))
{
if (string.IsNullOrEmpty(client.Credentials.PushEncodingAESKey))
throw new Exceptions.WechatApiEventSerializationException("Decrypt event failed, because there is no encoding AES key.");
InnerEncryptedEvent encryptedEvent = client.JsonSerializer.Deserialize<InnerEncryptedEvent>(callbackJson);
callbackJson = Utilities.WechatEventMessageCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
callbackJson = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
}
return client.JsonSerializer.Deserialize<TEvent>(callbackJson);
@ -73,9 +68,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
{
if (callbackXml.Contains("<Encrypt>") && callbackXml.Contains("</Encrypt>"))
{
XDocument xDocument = XDocument.Parse(callbackXml);
string encryptedData = xDocument.Root.Element("Encrypt").Value;
callbackXml = Utilities.WechatEventMessageCryptor.AESDecrypt(cipherText: encryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
if (!Utilities.WechatEventDataCryptor.TryParseXml(callbackXml, out string encryptedXml))
throw new Exceptions.WechatApiEventSerializationException("Decrypt event failed, because of empty encrypted data.");
callbackXml = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText: encryptedXml, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
}
return Utilities.XmlUtility.Deserialize<TEvent>(callbackXml);
@ -171,12 +167,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
{
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
string nonce = DateTimeOffset.Now.Ticks.ToString("x");
string cipher = Utilities.WechatEventMessageCryptor.AESEncrypt(
string cipher = Utilities.WechatEventDataCryptor.AESEncrypt(
plainText: json,
encodingAESKey: client.Credentials.PushEncodingAESKey!,
appId: client.Credentials.AppId
);
string sign = Utilities.WechatEventMessageCryptor.GenerateSignature(
string sign = Utilities.WechatEventDataCryptor.GenerateSignature(
sToken: client.Credentials.PushToken!,
sTimestamp: timestamp,
sNonce: nonce,
@ -231,13 +227,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
try
{
string cipher = Utilities.WechatEventMessageCryptor.AESEncrypt(
string cipher = Utilities.WechatEventDataCryptor.AESEncrypt(
plainText: xml,
encodingAESKey: client.Credentials.PushEncodingAESKey!,
appId: client.Credentials.AppId
);
xml = Utilities.WechatEventMessageCryptor.WrapXml(sToken: client.Credentials.PushToken!, sMsgEncrypt: cipher);
xml = Utilities.WechatEventDataCryptor.WrapXml(sToken: client.Credentials.PushToken!, sMsgEncrypt: cipher);
}
catch (Exception ex)
{
@ -287,7 +283,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
try
{
var encryptedEvent = client.JsonSerializer.Deserialize<InnerEncryptedEvent>(callbackJson);
return Utilities.WechatEventMessageCryptor.VerifySignature(
return Utilities.WechatEventDataCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,
@ -321,7 +317,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
XDocument xDoc = XDocument.Parse(callbackXml);
string? msgEncrypt = xDoc.Root?.Element("Encrypt")?.Value;
return Utilities.WechatEventMessageCryptor.VerifySignature(
return Utilities.WechatEventDataCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,

View File

@ -10,7 +10,7 @@ using System.Xml;
namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
{
internal static class WechatEventMessageCryptor
internal static class WechatEventDataCryptor
{
private const int AES_KEY_SIZE = 256;
private const int AES_BLOCK_SIZE = 128;
@ -247,7 +247,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
/// <param name="xml">微信推送来的 XML 数据。</param>
/// <param name="encryptedMsg">如果解析成功,将返回解析后的 `Encrypt` 字段的值。</param>
/// <returns>指示是否是有效的 XML 内容。</returns>
public static bool TryParseXml(string xml, out string? encryptedMsg)
public static bool TryParseXml(string xml, out string encryptedMsg)
{
return TryParseXml(xml, out encryptedMsg, out _);
}
@ -259,12 +259,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
/// <param name="toUserName">如果解析成功,将返回解析后的 `ToUserName` 字段的值。</param>
/// <param name="encryptedMsg">如果解析成功,将返回解析后的 `Encrypt` 字段的值。</param>
/// <returns>指示是否是有效的 XML 内容。</returns>
public static bool TryParseXml(string xml, out string? encryptedMsg, out string? toUserName)
public static bool TryParseXml(string xml, out string encryptedMsg, out string toUserName)
{
if (xml == null) throw new ArgumentNullException(nameof(xml));
encryptedMsg = null;
toUserName = null;
encryptedMsg = string.Empty;
toUserName = string.Empty;
try
{
@ -276,8 +276,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
if (xmlRoot == null)
return false;
encryptedMsg = xmlRoot["Encrypt"]?.InnerText?.ToString();
toUserName = xmlRoot["ToUserName"]?.InnerText?.ToString();
encryptedMsg = xmlRoot["Encrypt"]?.InnerText?.ToString() ?? string.Empty;
toUserName = xmlRoot["ToUserName"]?.InnerText?.ToString() ?? string.Empty;
return !string.IsNullOrEmpty(encryptedMsg);
}

View File

@ -8,7 +8,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
/// </summary>
public static class WechatWorkClientEventExtensions
{
private class EncryptedWechatWorkEvent
private class InnerEncryptedEvent
{
[Newtonsoft.Json.JsonProperty("Encrypt")]
[System.Text.Json.Serialization.JsonPropertyName("Encrypt")]
@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
[Newtonsoft.Json.JsonProperty("TimeStamp")]
[System.Text.Json.Serialization.JsonPropertyName("TimeStamp")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.NumericalStringConverter))]
public string Timestamp { get; set; } = default!;
public string TimestampString { get; set; } = default!;
[Newtonsoft.Json.JsonProperty("Nonce")]
[System.Text.Json.Serialization.JsonPropertyName("Nonce")]
@ -37,13 +37,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
try
{
if (string.IsNullOrEmpty(client.Credentials.PushEncodingAESKey))
throw new Exceptions.WechatWorkEventSerializationException("Encrypt event failed, because there is no encoding AES key.");
throw new Exceptions.WechatWorkEventSerializationException("Decrypt event failed, because there is no encoding AES key.");
var encryptedEvent = client.JsonSerializer.Deserialize<EncryptedWechatWorkEvent>(callbackJson);
if (string.IsNullOrEmpty(encryptedEvent.EncryptedData))
throw new Exceptions.WechatWorkEventSerializationException("Encrypt event failed, because of empty encrypted data.");
callbackJson = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
InnerEncryptedEvent encryptedEvent = client.JsonSerializer.Deserialize<InnerEncryptedEvent>(callbackJson);
callbackJson = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
return client.JsonSerializer.Deserialize<TEvent>(callbackJson);
}
@ -65,10 +62,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
try
{
if (!Utilities.WxBizMsgCryptor.TryParseXml(callbackXml, out string? encryptedXml))
throw new Exceptions.WechatWorkEventSerializationException("Encrypt event failed, because of empty encrypted data.");
if (!Utilities.WechatEventDataCryptor.TryParseXml(callbackXml, out string? encryptedXml))
throw new Exceptions.WechatWorkEventSerializationException("Decrypt event failed, because of empty encrypted data.");
callbackXml = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
callbackXml = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
return Utilities.XmlUtility.Deserialize<TEvent>(callbackXml);
}
catch (WechatWorkException)
@ -159,22 +156,22 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
{
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
string nonce = DateTimeOffset.Now.Ticks.ToString("x");
string cipher = Utilities.WxBizMsgCryptor.AESEncrypt(
string cipher = Utilities.WechatEventDataCryptor.AESEncrypt(
plainText: json,
encodingAESKey: client.Credentials.PushEncodingAESKey!,
corpOrSuiteId: string.IsNullOrEmpty(client.Credentials.SuiteId) ? client.Credentials.CorpId : client.Credentials.SuiteId!
);
string sign = Utilities.WxBizMsgCryptor.GenerateSignature(
string sign = Utilities.WechatEventDataCryptor.GenerateSignature(
sToken: client.Credentials.PushToken!,
sTimestamp: timestamp,
sNonce: nonce,
sMsgEncrypt: cipher
);
json = client.JsonSerializer.Serialize(new EncryptedWechatWorkEvent()
json = client.JsonSerializer.Serialize(new InnerEncryptedEvent()
{
EncryptedData = cipher,
Timestamp = timestamp,
TimestampString = timestamp,
Nonce = nonce,
Signature = sign
});
@ -215,13 +212,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
try
{
string cipher = Utilities.WxBizMsgCryptor.AESEncrypt(
string cipher = Utilities.WechatEventDataCryptor.AESEncrypt(
plainText: xml,
encodingAESKey: client.Credentials.PushEncodingAESKey!,
corpOrSuiteId: string.IsNullOrEmpty(client.Credentials.SuiteId) ? client.Credentials.CorpId : client.Credentials.SuiteId!
);
xml = Utilities.WxBizMsgCryptor.WrapXml(sToken: client.Credentials.PushToken!, sMsgEncrypt: cipher);
xml = Utilities.WechatEventDataCryptor.WrapXml(sToken: client.Credentials.PushToken!, sMsgEncrypt: cipher);
}
catch (Exception ex)
{
@ -254,7 +251,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
try
{
bool ret = Utilities.WxBizMsgCryptor.VerifySignature(
bool ret = Utilities.WechatEventDataCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,
@ -264,7 +261,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (ret)
{
replyEcho = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: callbackEcho, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
replyEcho = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText: callbackEcho, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _);
return true;
}
}
@ -293,8 +290,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
try
{
var encryptedEvent = client.JsonSerializer.Deserialize<EncryptedWechatWorkEvent>(callbackJson);
return Utilities.WxBizMsgCryptor.VerifySignature(
var encryptedEvent = client.JsonSerializer.Deserialize<InnerEncryptedEvent>(callbackJson);
return Utilities.WechatEventDataCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,
@ -330,7 +327,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
XDocument xDoc = XDocument.Parse(callbackXml);
string? msgEncrypt = xDoc.Root?.Element("Encrypt")?.Value;
return Utilities.WxBizMsgCryptor.VerifySignature(
return Utilities.WechatEventDataCryptor.VerifySignature(
sToken: client.Credentials.PushToken!,
sTimestamp: callbackTimestamp,
sNonce: callbackNonce,

View File

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

View File

@ -10,7 +10,7 @@ using System.Xml;
namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
{
public static class WxBizMsgCryptor
internal static class WechatEventDataCryptor
{
private const int AES_KEY_SIZE = 256;
private const int AES_BLOCK_SIZE = 128;
@ -81,8 +81,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
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,8 +93,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
byte[] bMsg = new byte[cipherBytes.Length + 32 - cipherBytes.Length % 32];
Array.Copy(cipherBytes, bMsg, cipherBytes.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());
@ -247,7 +247,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
/// <param name="xml">企业微信推送来的 XML 数据。</param>
/// <param name="encryptedMsg">如果解析成功,将返回解析后的 `Encrypt` 字段的值。</param>
/// <returns>指示是否是有效的 XML 内容。</returns>
public static bool TryParseXml(string xml, out string? encryptedMsg)
public static bool TryParseXml(string xml, out string encryptedMsg)
{
return TryParseXml(xml, out encryptedMsg, out _, out _);
}
@ -261,13 +261,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
/// <param name="agentId">如果解析成功,将返回解析后的 `AgentId` 字段的值。</param>
/// <returns>指示是否是有效的 XML 内容。</returns>
/// <returns></returns>
public static bool TryParseXml(string xml, out string? encryptedMsg, out string? toUserName, out string? agentId)
public static bool TryParseXml(string xml, out string encryptedMsg, out string toUserName, out string agentId)
{
if (xml == null) throw new ArgumentNullException(nameof(xml));
encryptedMsg = null;
toUserName = null;
agentId = null;
encryptedMsg = string.Empty;
toUserName = string.Empty;
agentId = string.Empty;
try
{
@ -279,9 +279,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
if (xmlRoot == null)
return false;
encryptedMsg = xmlRoot["Encrypt"]?.InnerText?.ToString();
toUserName = xmlRoot["ToUserName"]?.InnerText?.ToString();
agentId = xmlRoot["AgentID"]?.InnerText?.ToString();
encryptedMsg = xmlRoot["Encrypt"]?.InnerText?.ToString() ?? string.Empty;
toUserName = xmlRoot["ToUserName"]?.InnerText?.ToString() ?? string.Empty;
agentId = xmlRoot["AgentID"]?.InnerText?.ToString() ?? string.Empty;
return !string.IsNullOrEmpty(encryptedMsg);
}

View File

@ -2,7 +2,7 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.UnitTests
{
public class TestCase_WechatEventMessageCryptorTests
public class TestCase_WechatEventDataCryptorTests
{
[Fact(DisplayName = "试用例:验签并解密回调数据")]
public void TestVerifyAndDecryptEvent()
@ -15,12 +15,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.UnitTests
string reqNonce = "1372623149";
string reqCipherText = "RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==";
string actualPlain = Utilities.WechatEventMessageCryptor.AESDecrypt(reqCipherText, aesKey, out string actualAppId);
string actualPlain = Utilities.WechatEventDataCryptor.AESDecrypt(reqCipherText, aesKey, out string actualAppId);
string expectedPlain = "<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName>\n<FromUserName><![CDATA[mycreate]]></FromUserName>\n<CreateTime>1409659813</CreateTime>\n<MsgType><![CDATA[text]]></MsgType>\n<Content><![CDATA[hello]]></Content>\n<MsgId>4561255354251345929</MsgId>\n<AgentID>218</AgentID>\n</xml>";
Assert.Equal(expectedPlain, actualPlain);
Assert.Equal(appId, actualAppId);
Assert.True(Utilities.WechatEventMessageCryptor.VerifySignature(token, reqTimeStamp, reqNonce, reqCipherText, reqMsgSig));
Assert.True(Utilities.WechatEventDataCryptor.VerifySignature(token, reqTimeStamp, reqNonce, reqCipherText, reqMsgSig));
}
[Fact(DisplayName = "试用例:验签回调数据")]
@ -32,7 +32,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.UnitTests
string reqNonce = "1811081856";
string reqCipherText = "Q/jUxIL3/jRaFeTKesIr1QSq2SOEApDqlzcRrRM6Jlk4EbMBns3plPOR/W3gThOEq+zYI42fNSoIUb3cQwt9zyD1aLU/7D3WNLute7LQ9LSHjZEfVmx5zcIR9zvrUWGjhe1whTPH4e1WR6vbOYs8o/bDRF0vX/NcE4XK7P83Y6CzQiJoKbjVCne84s0zcw5eh+ZUDB55eaDHPSoS7kAC8kB00pfBoDF0jyfc8CUKLW97e72vJGyUWjZ0BvYN+R+tFjMgEzg/EN1imuuFnf40DMAcvB6y+C97TuaWjpgfRdowGWzn10JAFNukRfQqjdA0e2bfczJ7+t9w/t8/XSMADJOt1xbnP+I5cRX/r7ueBGmG/6ejP3myO9yTXHdujGvwrXHuWw+J7qD4VoUVjbm2vQ1qQKbrweKssr6O+3XSbanZ5R3n26EpN/gfgX+r6rcGViqsFop9Ai9xMnfJUubB6Q==";
Assert.True(Utilities.WechatEventMessageCryptor.VerifySignature(token, reqTimeStamp, reqNonce, reqCipherText, reqMsgSig));
Assert.True(Utilities.WechatEventDataCryptor.VerifySignature(token, reqTimeStamp, reqNonce, reqCipherText, reqMsgSig));
}
}
}

View File

@ -2,14 +2,14 @@
namespace SKIT.FlurlHttpClient.Wechat.Work.UnitTests
{
public class TestCase_WxBizMsgCryptorUtilityTests
public class TestCase_WechatEventDataCryptorUtilityTests
{
[Fact(DisplayName = "测试用例:回调信息解析")]
public void TestWxBizMsgCryptorParsing()
{
string xml = "<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt><AgentID><![CDATA[218]]></AgentID></xml>";
bool isValidXml = Utilities.WxBizMsgCryptor.TryParseXml(xml, out string encryptedMsg, out string toUserName, out string agentId);
bool isValidXml = Utilities.WechatEventDataCryptor.TryParseXml(xml, out string encryptedMsg, out string toUserName, out string agentId);
Assert.True(isValidXml);
Assert.Equal("wx5823bf96d3bd56c7", toUserName);
Assert.Equal("218", agentId);
@ -23,7 +23,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.UnitTests
string encodingAESKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
string cipherText = "RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==";
string actualCipherText = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText, encodingAESKey, out string actualCorpId);
string actualCipherText = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText, encodingAESKey, out string actualCorpId);
string expectedCipherText = "<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName>\n<FromUserName><![CDATA[mycreate]]></FromUserName>\n<CreateTime>1409659813</CreateTime>\n<MsgType><![CDATA[text]]></MsgType>\n<Content><![CDATA[hello]]></Content>\n<MsgId>4561255354251345929</MsgId>\n<AgentID>218</AgentID>\n</xml>";
Assert.Equal(corpId, actualCorpId);
@ -38,11 +38,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.UnitTests
string nonce = "1372623149";
string cipherText = "RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==";
string actualSignText = Utilities.WxBizMsgCryptor.GenerateSignature(sToken: token, sTimestamp: timestamp, sNonce: nonce, sMsgEncrypt: cipherText);
string actualSignText = Utilities.WechatEventDataCryptor.GenerateSignature(sToken: token, sTimestamp: timestamp, sNonce: nonce, sMsgEncrypt: cipherText);
string expectedSignText = "477715d11cdb4164915debcba66cb864d751f3e6";
Assert.Equal(expectedSignText, actualSignText, ignoreCase: true);
Assert.True(Utilities.WxBizMsgCryptor.VerifySignature(sToken: token, sTimestamp: timestamp, sNonce: nonce, sMsgEncrypt: cipherText, sMsgSign: expectedSignText));
Assert.True(Utilities.WechatEventDataCryptor.VerifySignature(sToken: token, sTimestamp: timestamp, sNonce: nonce, sMsgEncrypt: cipherText, sMsgSign: expectedSignText));
}
[Fact(DisplayName = "测试用例:回调信息验证签名")]
@ -54,7 +54,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.UnitTests
string reqNonce = "1811081856";
string reqCipherText = "Q/jUxIL3/jRaFeTKesIr1QSq2SOEApDqlzcRrRM6Jlk4EbMBns3plPOR/W3gThOEq+zYI42fNSoIUb3cQwt9zyD1aLU/7D3WNLute7LQ9LSHjZEfVmx5zcIR9zvrUWGjhe1whTPH4e1WR6vbOYs8o/bDRF0vX/NcE4XK7P83Y6CzQiJoKbjVCne84s0zcw5eh+ZUDB55eaDHPSoS7kAC8kB00pfBoDF0jyfc8CUKLW97e72vJGyUWjZ0BvYN+R+tFjMgEzg/EN1imuuFnf40DMAcvB6y+C97TuaWjpgfRdowGWzn10JAFNukRfQqjdA0e2bfczJ7+t9w/t8/XSMADJOt1xbnP+I5cRX/r7ueBGmG/6ejP3myO9yTXHdujGvwrXHuWw+J7qD4VoUVjbm2vQ1qQKbrweKssr6O+3XSbanZ5R3n26EpN/gfgX+r6rcGViqsFop9Ai9xMnfJUubB6Q==";
Assert.True(Utilities.WxBizMsgCryptor.VerifySignature(token, reqTimeStamp, reqNonce, reqCipherText, reqMsgSig));
Assert.True(Utilities.WechatEventDataCryptor.VerifySignature(token, reqTimeStamp, reqNonce, reqCipherText, reqMsgSig));
}
}
}