Blame view

src/test/java/com/jfinal/weixin/sdk/encrypt/Program.java 2.41 KB
e80df919   atao   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  package com.jfinal.weixin.sdk.encrypt;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import java.io.StringReader;
  
  
  public class Program {
  
      public static void main(String[] args) throws Exception {
  
          //
          // 第三方回复公众平台
          //
  
          // 需要加密的明文
          String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
          String token = "pamtest";
          String timestamp = "1409304348";
          String nonce = "xxxxxx";
          String appId = "wxb11529c136998cb6";
          String replyMsg = " 中文<xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
  
          WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
          String mingwen = pc.encryptMsg(replyMsg, timestamp, nonce);
          System.out.println("加密后: " + mingwen);
  
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          StringReader sr = new StringReader(mingwen);
          InputSource is = new InputSource(sr);
          Document document = db.parse(is);
  
          Element root = document.getDocumentElement();
          NodeList nodelist1 = root.getElementsByTagName("Encrypt");
          NodeList nodelist2 = root.getElementsByTagName("MsgSignature");
  
          String encrypt = nodelist1.item(0).getTextContent();
          String msgSignature = nodelist2.item(0).getTextContent();
  
          String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
          String fromXML = String.format(format, encrypt);
  
          //
          // 公众平台发送消息给第三方,第三方处理
          //
  
          // 第三方收到公众号平台发送的消息
          String result2 = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
          System.out.println("解密后明文: " + result2);
  
          //pc.verifyUrl(null, null, null, null);
      }
  }