好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java微信公众号安全模式消息解密

本文实例为大家分享了java 微信公众号 安全模式消息解密的具体代码,供大家参考,具体内容如下

1.微信公众平台下载解密工具,导入项目中,根据demo解密消息,解密工具官方下载地址:点击打开 链接

?

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转为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 );

   //将解密后的消息转为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实体类

?

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目录中local_policy.jar和us_export_policy.jar两个文件拷贝到 jdk\jre\lib\security目录下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/Caisin_He/article/details/78852181

查看更多关于Java微信公众号安全模式消息解密的详细内容...

  阅读:33次