评论

收藏

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

编程语言 编程语言 发布于:2021-10-06 15:52 | 阅读数:263 | 评论:0

这篇文章主要为大家详细介绍了Java微信公众号安全模式消息解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java微信公众号安全模式消息解密的具体代码,供大家参考,具体内容如下
1.微信公众平台下载解密工具,导入项目中,根据demo解密消息,解密工具官方下载地址:点击打开链接
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实体类
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目录下
DSC0000.jpg

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/Caisin_He/article/details/78852181

关注下面的标签,发现更多相似文章