|
1 |
| -package me.chanjar.weixin.common.util.crypto; |
2 |
| - |
3 |
| -import org.testng.annotations.Test; |
4 |
| -import org.w3c.dom.Document; |
5 |
| -import org.w3c.dom.Element; |
6 |
| -import org.w3c.dom.NodeList; |
7 |
| -import org.xml.sax.InputSource; |
8 |
| -import org.xml.sax.SAXException; |
9 |
| - |
10 |
| -import javax.xml.parsers.DocumentBuilder; |
11 |
| -import javax.xml.parsers.DocumentBuilderFactory; |
12 |
| -import javax.xml.parsers.ParserConfigurationException; |
13 |
| -import java.io.IOException; |
14 |
| -import java.io.StringReader; |
15 |
| - |
16 |
| -import static org.testng.Assert.assertEquals; |
17 |
| -import static org.testng.Assert.fail; |
18 |
| - |
19 |
| -@Test |
20 |
| -public class WxCryptUtilTest { |
21 |
| - String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; |
22 |
| - String token = "pamtest"; |
23 |
| - String timestamp = "1409304348"; |
24 |
| - String nonce = "xxxxxx"; |
25 |
| - String appId = "wxb11529c136998cb6"; |
26 |
| - String randomStr = "aaaabbbbccccdddd"; |
27 |
| - |
28 |
| - String xmlFormat = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>"; |
29 |
| - String replyMsg = "我是中文abcd123"; |
30 |
| - |
31 |
| - String afterAesEncrypt = "jn1L23DB+6ELqJ+6bruv21Y6MD7KeIfP82D6gU39rmkgczbWwt5+3bnyg5K55bgVtVzd832WzZGMhkP72vVOfg=="; |
32 |
| - |
33 |
| - String replyMsg2 = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-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>"; |
34 |
| - String afterAesEncrypt2 = "jn1L23DB+6ELqJ+6bruv23M2GmYfkv0xBh2h+XTBOKVKcgDFHle6gqcZ1cZrk3e1qjPQ1F4RsLWzQRG9udbKWesxlkupqcEcW7ZQweImX9+wLMa0GaUzpkycA8+IamDBxn5loLgZpnS7fVAbExOkK5DYHBmv5tptA9tklE/fTIILHR8HLXa5nQvFb3tYPKAlHF3rtTeayNf0QuM+UW/wM9enGIDIJHF7CLHiDNAYxr+r+OrJCmPQyTy8cVWlu9iSvOHPT/77bZqJucQHQ04sq7KZI27OcqpQNSto2OdHCoTccjggX5Z9Mma0nMJBU+jLKJ38YB1fBIz+vBzsYjrTmFQ44YfeEuZ+xRTQwr92vhA9OxchWVINGC50qE/6lmkwWTwGX9wtQpsJKhP+oS7rvTY8+VdzETdfakjkwQ5/Xka042OlUb1/slTwo4RscuQ+RdxSGvDahxAJ6+EAjLt9d8igHngxIbf6YyqqROxuxqIeIch3CssH/LqRs+iAcILvApYZckqmA7FNERspKA5f8GoJ9sv8xmGvZ9Yrf57cExWtnX8aCMMaBropU/1k+hKP5LVdzbWCG0hGwx/dQudYR/eXp3P0XxjlFiy+9DMlaFExWUZQDajPkdPrEeOwofJb"; |
35 |
| - |
36 |
| - public void testNormal() throws ParserConfigurationException, SAXException, IOException { |
37 |
| - WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
38 |
| - String encryptedXml = pc.encrypt(this.replyMsg); |
39 |
| - |
40 |
| - System.out.println(encryptedXml); |
41 |
| - |
42 |
| - DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
43 |
| - DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
44 |
| - Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml))); |
45 |
| - |
46 |
| - Element root = document.getDocumentElement(); |
47 |
| - String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent(); |
48 |
| - System.out.println(cipherText); |
49 |
| - |
50 |
| - String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent(); |
51 |
| - System.out.println(msgSignature); |
52 |
| - |
53 |
| - String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent(); |
54 |
| - System.out.println(timestamp); |
55 |
| - |
56 |
| - String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent(); |
57 |
| - System.out.println(nonce); |
58 |
| - |
59 |
| - String messageText = String.format(this.xmlFormat, cipherText); |
60 |
| - System.out.println(messageText); |
61 |
| - |
62 |
| - // 第三方收到企业号平台发送的消息 |
63 |
| - String plainMessage = pc.decrypt(cipherText); |
64 |
| - System.out.println(plainMessage); |
65 |
| - |
66 |
| - assertEquals(plainMessage, this.replyMsg); |
67 |
| - } |
68 |
| - |
69 |
| - public void testAesEncrypt() { |
70 |
| - WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
71 |
| - assertEquals(pc.encrypt(this.randomStr, this.replyMsg), this.afterAesEncrypt); |
72 |
| - } |
73 |
| - |
74 |
| - public void testAesEncrypt2() { |
75 |
| - WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
76 |
| - assertEquals(pc.encrypt(this.randomStr, this.replyMsg2), this.afterAesEncrypt2); |
77 |
| - } |
78 |
| - |
79 |
| - public void testValidateSignatureError() throws ParserConfigurationException, SAXException, |
80 |
| - IOException { |
81 |
| - try { |
82 |
| - WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
83 |
| - String afterEncrpt = pc.encrypt(this.replyMsg); |
84 |
| - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
85 |
| - DocumentBuilder db = dbf.newDocumentBuilder(); |
86 |
| - StringReader sr = new StringReader(afterEncrpt); |
87 |
| - InputSource is = new InputSource(sr); |
88 |
| - Document document = db.parse(is); |
89 |
| - |
90 |
| - Element root = document.getDocumentElement(); |
91 |
| - NodeList nodelist1 = root.getElementsByTagName("Encrypt"); |
92 |
| - |
93 |
| - String encrypt = nodelist1.item(0).getTextContent(); |
94 |
| - String fromXML = String.format(this.xmlFormat, encrypt); |
95 |
| - pc.decrypt("12345", this.timestamp, this.nonce, fromXML); // 这里签名错误 |
96 |
| - } catch (RuntimeException e) { |
97 |
| - assertEquals(e.getMessage(), "加密消息签名校验失败"); |
98 |
| - return; |
99 |
| - } |
100 |
| - fail("错误流程不抛出异常???"); |
101 |
| - } |
102 |
| - |
103 |
| -} |
| 1 | +package me.chanjar.weixin.common.util.crypto; |
| 2 | + |
| 3 | +import org.testng.annotations.Test; |
| 4 | +import org.w3c.dom.Document; |
| 5 | +import org.w3c.dom.Element; |
| 6 | +import org.w3c.dom.NodeList; |
| 7 | +import org.xml.sax.InputSource; |
| 8 | +import org.xml.sax.SAXException; |
| 9 | + |
| 10 | +import javax.xml.parsers.DocumentBuilder; |
| 11 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 12 | +import javax.xml.parsers.ParserConfigurationException; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.StringReader; |
| 15 | + |
| 16 | +import static org.testng.Assert.assertEquals; |
| 17 | +import static org.testng.Assert.fail; |
| 18 | + |
| 19 | +@Test |
| 20 | +public class WxCryptUtilTest { |
| 21 | + String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; |
| 22 | + String token = "pamtest"; |
| 23 | + String timestamp = "1409304348"; |
| 24 | + String nonce = "xxxxxx"; |
| 25 | + String appId = "wxb11529c136998cb6"; |
| 26 | + String randomStr = "aaaabbbbccccdddd"; |
| 27 | + |
| 28 | + String xmlFormat = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>"; |
| 29 | + String replyMsg = "我是中文abcd123"; |
| 30 | + |
| 31 | + String afterAesEncrypt = "jn1L23DB+6ELqJ+6bruv21Y6MD7KeIfP82D6gU39rmkgczbWwt5+3bnyg5K55bgVtVzd832WzZGMhkP72vVOfg=="; |
| 32 | + |
| 33 | + String replyMsg2 = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-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>"; |
| 34 | + String afterAesEncrypt2 = "jn1L23DB+6ELqJ+6bruv23M2GmYfkv0xBh2h+XTBOKVKcgDFHle6gqcZ1cZrk3e1qjPQ1F4RsLWzQRG9udbKWesxlkupqcEcW7ZQweImX9+wLMa0GaUzpkycA8+IamDBxn5loLgZpnS7fVAbExOkK5DYHBmv5tptA9tklE/fTIILHR8HLXa5nQvFb3tYPKAlHF3rtTeayNf0QuM+UW/wM9enGIDIJHF7CLHiDNAYxr+r+OrJCmPQyTy8cVWlu9iSvOHPT/77bZqJucQHQ04sq7KZI27OcqpQNSto2OdHCoTccjggX5Z9Mma0nMJBU+jLKJ38YB1fBIz+vBzsYjrTmFQ44YfeEuZ+xRTQwr92vhA9OxchWVINGC50qE/6lmkwWTwGX9wtQpsJKhP+oS7rvTY8+VdzETdfakjkwQ5/Xka042OlUb1/slTwo4RscuQ+RdxSGvDahxAJ6+EAjLt9d8igHngxIbf6YyqqROxuxqIeIch3CssH/LqRs+iAcILvApYZckqmA7FNERspKA5f8GoJ9sv8xmGvZ9Yrf57cExWtnX8aCMMaBropU/1k+hKP5LVdzbWCG0hGwx/dQudYR/eXp3P0XxjlFiy+9DMlaFExWUZQDajPkdPrEeOwofJb"; |
| 35 | + |
| 36 | + public void testNormal() throws ParserConfigurationException, SAXException, IOException { |
| 37 | + WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
| 38 | + String encryptedXml = pc.encrypt(this.replyMsg); |
| 39 | + |
| 40 | + System.out.println(encryptedXml); |
| 41 | + |
| 42 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 43 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 44 | + Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml))); |
| 45 | + |
| 46 | + Element root = document.getDocumentElement(); |
| 47 | + String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent(); |
| 48 | + System.out.println(cipherText); |
| 49 | + |
| 50 | + String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent(); |
| 51 | + System.out.println(msgSignature); |
| 52 | + |
| 53 | + String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent(); |
| 54 | + System.out.println(timestamp); |
| 55 | + |
| 56 | + String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent(); |
| 57 | + System.out.println(nonce); |
| 58 | + |
| 59 | + String messageText = String.format(this.xmlFormat, cipherText); |
| 60 | + System.out.println(messageText); |
| 61 | + |
| 62 | + // 第三方收到企业号平台发送的消息 |
| 63 | + String plainMessage = pc.decrypt(cipherText); |
| 64 | + System.out.println(plainMessage); |
| 65 | + |
| 66 | + assertEquals(plainMessage, this.replyMsg); |
| 67 | + } |
| 68 | + |
| 69 | + public void testAesEncrypt() { |
| 70 | + WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
| 71 | + assertEquals(pc.encrypt(this.randomStr, this.replyMsg), this.afterAesEncrypt); |
| 72 | + } |
| 73 | + |
| 74 | + public void testAesEncrypt2() { |
| 75 | + WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
| 76 | + assertEquals(pc.encrypt(this.randomStr, this.replyMsg2), this.afterAesEncrypt2); |
| 77 | + } |
| 78 | + |
| 79 | + public void testValidateSignatureError() throws ParserConfigurationException, SAXException, |
| 80 | + IOException { |
| 81 | + try { |
| 82 | + WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); |
| 83 | + String afterEncrpt = pc.encrypt(this.replyMsg); |
| 84 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 85 | + DocumentBuilder db = dbf.newDocumentBuilder(); |
| 86 | + StringReader sr = new StringReader(afterEncrpt); |
| 87 | + InputSource is = new InputSource(sr); |
| 88 | + Document document = db.parse(is); |
| 89 | + |
| 90 | + Element root = document.getDocumentElement(); |
| 91 | + NodeList nodelist1 = root.getElementsByTagName("Encrypt"); |
| 92 | + |
| 93 | + String encrypt = nodelist1.item(0).getTextContent(); |
| 94 | + String fromXML = String.format(this.xmlFormat, encrypt); |
| 95 | + pc.decrypt("12345", this.timestamp, this.nonce, fromXML); // 这里签名错误 |
| 96 | + } catch (RuntimeException e) { |
| 97 | + assertEquals(e.getMessage(), "加密消息签名校验失败"); |
| 98 | + return; |
| 99 | + } |
| 100 | + fail("错误流程不抛出异常???"); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments