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

网友投稿 355 2023-01-17


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 xmlToMap(HttpServletRequest request, Message msg) throws Exception {

SAXReader reader = new SAXReader();

String token = "";

String encodingAesKey = "";

String appId = "";

//获取加密消息xml字符串

/* String format = "<![CDATA[toUser]]><![CDATA[%1$s]]>";

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 map = new HashMap<>(6);

//将解密后的消息转为xml

Document doc = DocumentHelper.parseText(result);

Element root = doc.getRootElement();

List list = root.elements();

for (Element e : lisuezukHpt) {

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目录下


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何对接口权限管理工具(接口权限控制)
下一篇:详解maven的setting配置文件中mirror和repository的区别
相关文章

 发表评论

暂时没有评论,来抢沙发吧~