Sample.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using Senparc.Weixin.Tencent;
  7. namespace MsgCryptTest
  8. {
  9. class Sample
  10. {
  11. static void Main(string[] args)
  12. {
  13. //公众平台上开发者设置的token, appID, EncodingAESKey
  14. string sToken = "QDG6eK";
  15. string sAppID = "wx5823bf96d3bd56c7";
  16. string sEncodingAESKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
  17. WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sAppID);
  18. /* 1. 对用户回复的数据进行解密。
  19. * 用户回复消息或者点击事件响应时,企业会收到回调消息,假设企业收到的推送消息:
  20. * POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1
  21. Host: qy.weixin.qq.com
  22. Content-Length: 613
  23. *
  24. * <xml>
  25. <ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName>
  26. <Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt>
  27. </xml>
  28. */
  29. string sReqMsgSig = "477715d11cdb4164915debcba66cb864d751f3e6";
  30. string sReqTimeStamp = "1409659813";
  31. string sReqNonce = "1372623149";
  32. string sReqData = "<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></xml>";
  33. string sMsg = ""; //解析之后的明文
  34. int ret = 0;
  35. ret = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData, ref sMsg);
  36. if (ret != 0)
  37. {
  38. System.Console.WriteLine("ERR: Decrypt fail, ret: " + ret);
  39. return;
  40. }
  41. System.Console.WriteLine(sMsg);
  42. /*
  43. * 2. 企业回复用户消息也需要加密和拼接xml字符串。
  44. * 假设企业需要回复用户的消息为:
  45. * <xml>
  46. * <ToUserName><![CDATA[mycreate]]></ToUserName>
  47. * <FromUserName><![CDATA[wx5823bf96d3bd56c7]]></FromUserName>
  48. * <CreateTime>1348831860</CreateTime>
  49. <MsgType><![CDATA[text]]></MsgType>
  50. * <Content><![CDATA[this is a test]]></Content>
  51. * <MsgId>1234567890123456</MsgId>
  52. * </xml>
  53. * 生成xml格式的加密消息过程为:
  54. */
  55. string sRespData = "<xml><ToUserName><![CDATA[mycreate]]></ToUserName><FromUserName><![CDATA[wx582测试一下中文的情况,消息长度是按字节来算的396d3bd56c7]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is a test]]></Content><MsgId>1234567890123456</MsgId></xml>";
  56. string sEncryptMsg = ""; //xml格式的密文
  57. ret = wxcpt.EncryptMsg(sRespData, sReqTimeStamp, sReqNonce, ref sEncryptMsg);
  58. System.Console.WriteLine("sEncryptMsg");
  59. System.Console.WriteLine(sEncryptMsg);
  60. /*测试:
  61. * 将sEncryptMsg解密看看是否是原文
  62. * */
  63. XmlDocument doc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed();
  64. doc.LoadXml(sEncryptMsg);
  65. XmlNode root = doc.FirstChild;
  66. string sig = root["MsgSignature"].InnerText;
  67. string enc = root["Encrypt"].InnerText;
  68. string timestamp = root["TimeStamp"].InnerText;
  69. string nonce = root["Nonce"].InnerText;
  70. string stmp = "";
  71. ret = wxcpt.DecryptMsg(sig, timestamp, nonce, sEncryptMsg, ref stmp);
  72. System.Console.WriteLine("stemp");
  73. System.Console.WriteLine(stmp + ret);
  74. return;
  75. }
  76. }
  77. }