123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #region Apache License Version 2.0
- #endregion Apache License Version 2.0
- using System;
- using System.Collections;
- using System.Security.Cryptography;
- using System.Text;
- using System.Xml;
- namespace Senparc.Weixin.Tencent
- {
- public class WXBizMsgCrypt
- {
- string m_sToken;
- string m_sEncodingAESKey;
- string m_sAppID;
- enum WXBizMsgCryptErrorCode
- {
- WXBizMsgCrypt_OK = 0,
- WXBizMsgCrypt_ValidateSignature_Error = -40001,
- WXBizMsgCrypt_ParseXml_Error = -40002,
- WXBizMsgCrypt_ComputeSignature_Error = -40003,
- WXBizMsgCrypt_IllegalAesKey = -40004,
- WXBizMsgCrypt_ValidateAppid_Error = -40005,
- WXBizMsgCrypt_EncryptAES_Error = -40006,
- WXBizMsgCrypt_DecryptAES_Error = -40007,
- WXBizMsgCrypt_IllegalBuffer = -40008,
- WXBizMsgCrypt_EncodeBase64_Error = -40009,
- WXBizMsgCrypt_DecodeBase64_Error = -40010
- };
-
-
-
-
- public WXBizMsgCrypt(string sToken, string sEncodingAESKey, string sAppID)
- {
- m_sToken = sToken;
- m_sAppID = sAppID;
- m_sEncodingAESKey = sEncodingAESKey;
- }
-
-
-
-
-
-
-
- public int DecryptMsg(string sMsgSignature, string sTimeStamp, string sNonce, string sPostData, ref string sMsg)
- {
- if (m_sEncodingAESKey.Length != 43)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_IllegalAesKey;
- }
- XmlDocument doc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed();
- XmlNode root;
- string sEncryptMsg;
- try
- {
- doc.LoadXml(sPostData);
- root = doc.FirstChild;
- sEncryptMsg = root["Encrypt"].InnerText;
- }
- catch (Exception)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_ParseXml_Error;
- }
-
- int ret = 0;
- ret = VerifySignature(m_sToken, sTimeStamp, sNonce, sEncryptMsg, sMsgSignature);
- if (ret != 0)
- return ret;
-
- string cpid = "";
- try
- {
- sMsg = Cryptography.AES_decrypt(sEncryptMsg, m_sEncodingAESKey, ref cpid);
- }
- catch (FormatException)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_DecodeBase64_Error;
- }
- catch (Exception)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_DecryptAES_Error;
- }
- if (cpid != m_sAppID)
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_ValidateAppid_Error;
- return 0;
- }
-
-
-
-
-
-
-
- public int EncryptMsg(string sReplyMsg, string sTimeStamp, string sNonce, ref string sEncryptMsg)
- {
- if (m_sEncodingAESKey.Length != 43)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_IllegalAesKey;
- }
- string raw = "";
- try
- {
- raw = Cryptography.AES_encrypt(sReplyMsg, m_sEncodingAESKey, m_sAppID);
- }
- catch (Exception)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_EncryptAES_Error;
- }
- string MsgSigature = "";
- int ret = 0;
- ret = GenarateSinature(m_sToken, sTimeStamp, sNonce, raw, ref MsgSigature);
- if (0 != ret)
- return ret;
- sEncryptMsg = "";
- string EncryptLabelHead = "<Encrypt><![CDATA[";
- string EncryptLabelTail = "]]></Encrypt>";
- string MsgSigLabelHead = "<MsgSignature><![CDATA[";
- string MsgSigLabelTail = "]]></MsgSignature>";
- string TimeStampLabelHead = "<TimeStamp><![CDATA[";
- string TimeStampLabelTail = "]]></TimeStamp>";
- string NonceLabelHead = "<Nonce><![CDATA[";
- string NonceLabelTail = "]]></Nonce>";
- sEncryptMsg = sEncryptMsg + "<xml>" + EncryptLabelHead + raw + EncryptLabelTail;
- sEncryptMsg = sEncryptMsg + MsgSigLabelHead + MsgSigature + MsgSigLabelTail;
- sEncryptMsg = sEncryptMsg + TimeStampLabelHead + sTimeStamp + TimeStampLabelTail;
- sEncryptMsg = sEncryptMsg + NonceLabelHead + sNonce + NonceLabelTail;
- sEncryptMsg += "</xml>";
- return 0;
- }
- public class DictionarySort : IComparer
- {
- public int Compare(object oLeft, object oRight)
- {
- string sLeft = oLeft as string;
- string sRight = oRight as string;
- int iLeftLength = sLeft.Length;
- int iRightLength = sRight.Length;
- int index = 0;
- while (index < iLeftLength && index < iRightLength)
- {
- if (sLeft[index] < sRight[index])
- return -1;
- else if (sLeft[index] > sRight[index])
- return 1;
- else
- index++;
- }
- return iLeftLength - iRightLength;
- }
- }
-
- private static int VerifySignature(string sToken, string sTimeStamp, string sNonce, string sMsgEncrypt, string sSigture)
- {
- string hash = "";
- int ret = 0;
- ret = GenarateSinature(sToken, sTimeStamp, sNonce, sMsgEncrypt, ref hash);
- if (ret != 0)
- return ret;
-
- if (hash == sSigture)
- return 0;
- else
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_ValidateSignature_Error;
- }
- }
- public static int GenarateSinature(string sToken, string sTimeStamp, string sNonce, string sMsgEncrypt, ref string sMsgSignature)
- {
- ArrayList AL = new ArrayList();
- AL.Add(sToken);
- AL.Add(sTimeStamp);
- AL.Add(sNonce);
- AL.Add(sMsgEncrypt);
- AL.Sort(new DictionarySort());
- string raw = "";
- for (int i = 0; i < AL.Count; ++i)
- {
- raw += AL[i];
- }
- SHA1 sha;
- ASCIIEncoding enc;
- string hash = "";
- try
- {
- #if NET35 || NET40 || NET45
- sha = new SHA1CryptoServiceProvider();
- #else
- sha = SHA1.Create();
- #endif
- enc = new ASCIIEncoding();
- byte[] dataToHash = enc.GetBytes(raw);
- byte[] dataHashed = sha.ComputeHash(dataToHash);
- hash = BitConverter.ToString(dataHashed).Replace("-", "");
- hash = hash.ToLower();
- }
- catch (Exception)
- {
- return (int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_ComputeSignature_Error;
- }
- sMsgSignature = hash;
- return 0;
- }
- }
- }
|