From 30405cc55fe3d8fca325b4c29eade53a75d95d3a Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 28 Feb 2022 21:37:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(work):=20=E7=A7=BB=E9=99=A4=20WxBizMsgCryp?= =?UTF-8?q?tor=20=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatApiClientEventExtensions.cs | 32 +++++++-------- ...geCryptor.cs => WechatEventDataCryptor.cs} | 14 +++---- .../WechatWorkClientEventExtensions.cs | 41 +++++++++---------- .../Properties/AssemblyInfo.cs | 3 ++ .../WechatEventDataCryptor.cs} | 26 ++++++------ ...> TestCase_WechatEventDataCryptorTests.cs} | 8 ++-- ...ase_WechatEventDataCryptorUtilityTests.cs} | 12 +++--- 7 files changed, 66 insertions(+), 70 deletions(-) rename src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/{WechatEventMessageCryptor.cs => WechatEventDataCryptor.cs} (97%) create mode 100644 src/SKIT.FlurlHttpClient.Wechat.Work/Properties/AssemblyInfo.cs rename src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/{WxBizMsgCryptor.cs => Internal/WechatEventDataCryptor.cs} (95%) rename test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/{TestCase_WechatEventMessageCryptorTests.cs => TestCase_WechatEventDataCryptorTests.cs} (83%) rename test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/{TestCase_WxBizMsgCryptorUtilityTests.cs => TestCase_WechatEventDataCryptorUtilityTests.cs} (84%) diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs index 59a34499..cafbc749 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs @@ -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(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(callbackJson); @@ -73,9 +68,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api { if (callbackXml.Contains("") && callbackXml.Contains("")) { - 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(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(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, diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/WechatEventMessageCryptor.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/WechatEventDataCryptor.cs similarity index 97% rename from src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/WechatEventMessageCryptor.cs rename to src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/WechatEventDataCryptor.cs index d1c8ae03..dd249cfc 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/WechatEventMessageCryptor.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/WechatEventDataCryptor.cs @@ -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 /// 微信推送来的 XML 数据。 /// 如果解析成功,将返回解析后的 `Encrypt` 字段的值。 /// 指示是否是有效的 XML 内容。 - 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 /// 如果解析成功,将返回解析后的 `ToUserName` 字段的值。 /// 如果解析成功,将返回解析后的 `Encrypt` 字段的值。 /// 指示是否是有效的 XML 内容。 - 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); } diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs index 22d468d7..a0bbd10c 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs @@ -8,7 +8,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work /// 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(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(callbackJson); + callbackJson = Utilities.WechatEventDataCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _); return client.JsonSerializer.Deserialize(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(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(callbackJson); - return Utilities.WxBizMsgCryptor.VerifySignature( + var encryptedEvent = client.JsonSerializer.Deserialize(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, diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/Properties/AssemblyInfo.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..2af48bda --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("SKIT.FlurlHttpClient.Wechat.Work.UnitTests")] diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/WxBizMsgCryptor.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/WechatEventDataCryptor.cs similarity index 95% rename from src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/WxBizMsgCryptor.cs rename to src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/WechatEventDataCryptor.cs index 1f85a42e..5666c4af 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/WxBizMsgCryptor.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/WechatEventDataCryptor.cs @@ -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 /// 企业微信推送来的 XML 数据。 /// 如果解析成功,将返回解析后的 `Encrypt` 字段的值。 /// 指示是否是有效的 XML 内容。 - 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 /// 如果解析成功,将返回解析后的 `AgentId` 字段的值。 /// 指示是否是有效的 XML 内容。 /// - 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); } diff --git a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/TestCase_WechatEventMessageCryptorTests.cs b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/TestCase_WechatEventDataCryptorTests.cs similarity index 83% rename from test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/TestCase_WechatEventMessageCryptorTests.cs rename to test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/TestCase_WechatEventDataCryptorTests.cs index 36124e9f..27e489b2 100644 --- a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/TestCase_WechatEventMessageCryptorTests.cs +++ b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/TestCase_WechatEventDataCryptorTests.cs @@ -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 = "\n\n1409659813\n\n\n4561255354251345929\n218\n"; 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)); } } } diff --git a/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/TestCase_WxBizMsgCryptorUtilityTests.cs b/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/TestCase_WechatEventDataCryptorUtilityTests.cs similarity index 84% rename from test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/TestCase_WxBizMsgCryptorUtilityTests.cs rename to test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/TestCase_WechatEventDataCryptorUtilityTests.cs index ee0c4012..ebd795d1 100644 --- a/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/TestCase_WxBizMsgCryptorUtilityTests.cs +++ b/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/TestCase_WechatEventDataCryptorUtilityTests.cs @@ -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 = ""; - 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 = "\n\n1409659813\n\n\n4561255354251345929\n218\n"; 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)); } } }