本文實(shí)例為大家分享了java微信公眾號(hào)安全模式消息解密的具體代碼,供大家參考,具體內(nèi)容如下
1.微信公眾平臺(tái)下載解密工具,導(dǎo)入項(xiàng)目中,根據(jù)demo解密消息,解密工具官方下載地址:點(diǎn)擊打開(kāi)鏈接
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
|
public static string streamtostring(httpservletrequest request) throws ioexception { bufferedreader reader = new bufferedreader( new inputstreamreader(request.getinputstream())); stringbuilder sb = new stringbuilder(); string line; try { while ((line = reader.readline()) != null ) { sb.append(line); } } catch (ioexception e) { e.printstacktrace(); } return sb.tostring(); } /** * xml轉(zhuǎn)為map集合 * * @param request * @param msg * @return * @throws ioexception * @throws documentexception */ public static map<string, string> xmltomap(httpservletrequest request, message msg) throws exception { saxreader reader = new saxreader(); string token = "" ; string encodingaeskey = "" ; string appid = "" ; //獲取加密消息xml字符串 /* string format = "<xml><tousername><![cdata[touser]]></tousername><encrypt><![cdata[%1$s]]></encrypt></xml>"; document document = reader.read(request.getinputstream()); element rootelement = document.getrootelement(); element encrypt = rootelement.element("encrypt");*/ // string fromxml = string.format(format, encrypt.gettext()); string fromxml = streamtostring(request); //解密消息 wxbizmsgcrypt pc = new wxbizmsgcrypt(token, encodingaeskey, appid); //獲得解密消息 string result = pc.decryptmsg(msg.getmsg_signature(), msg.gettimestamp(), msg.getnonce(), fromxml); map<string, string> map = new hashmap<>( 6 ); //將解密后的消息轉(zhuǎn)為xml document doc = documenthelper.parsetext(result); element root = doc.getrootelement(); list<element> list = root.elements(); for (element e : list) { map.put(e.getname(), e.gettext()); } return map; } |
message實(shí)體類
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.caisin.weixin.domain; import lombok.data; @data public class message { private string signature; private string timestamp; private string nonce; private string openid; private string msg_signature; private string encrypt_type; } |
2.將jdk中 jdk\jre\lib\security\policy\unlimited目錄中l(wèi)ocal_policy.jar和us_export_policy.jar兩個(gè)文件拷貝到 jdk\jre\lib\security目錄下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Caisin_He/article/details/78852181