欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-03 23:41:34
本文实例为大家分享了java微信公众号安全模式消息解密的具体代码,供大家参考,具体内容如下 1.微信公众平台下载解密工具,导入项目中,根据demo解密消息,解密工具官方下...

本文实例为大家分享了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目录下

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

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