当前位置: 首页 > news >正文

企微获取会话内容,RSA 解密函数

企微获取会话内容,RSA 解密函数

  • 企微获取会话内容
    • 下载SDK
    • SDK配置
    • 解密过程
    • 解密代码参考
    • SDK文件上传到服务器
    • 最后

企微获取会话内容

官方文档:
https://developer.work.weixin.qq.com/document/path/91774

下载SDK

根据自己的环境下载对应的SDK。

企微获取会话内容官方文档

SDK配置

1、sdk中的文件拷贝到项目中
我这里的linux的.so文件,原本是_Java.so那个,我复制了一份去掉了_Java后缀,内容一样的。
在这里插入图片描述
2、Finance文件
在这里插入图片描述
1)注意要放在 指定路径下

package com.tencent.wework;

2)修改Finance文件
将最后的static代码块改成以下代码

static {if (isWindows()) {String path = Finance.class.getClassLoader().getResource("").getPath() + "\\lib\\qywx\\win\\";path = path.replaceAll("%20", " ").replaceFirst("/", "").replace("/", "\\\\");//加载顺序不能变System.load(path.concat("libcrypto-1_1-x64.dll"));System.load(path.concat("libssl-1_1-x64.dll"));System.load(path.concat("libcurl-x64.dll"));System.load(path.concat("WeWorkFinanceSdk.dll"));} else {// linux 加载指定so文件在linux系统下的位置System.load("/usr/lib/libWeWorkFinanceSdk.so");System.loadLibrary("WeWorkFinanceSdk");}}public static boolean isWindows() {String osName = System.getProperties().getProperty("os.name");return osName.toUpperCase().indexOf("WINDOWS") != -1;}

解密过程

参考官方文档案例演示: https://developer.work.weixin.qq.com/document/path/91551

我这里只展示需要自己开发的RSA解密方法,官方文档是这样描述解密过程的:

encrypt_random_key内容解密说明:
encrypt_random_key是使用企业在管理端填写的公钥(使用模值为2048bit的秘钥),采用RSA加密算法进行加密处理后base64
encode的内容,加密内容为企业微信产生。RSA使用PKCS1。 企业通过GetChatData获取到会话数据后:
a) 需首先对每条消息的encrypt_random_key内容进行base64 decode,得到字符串str1.
b) 使用publickey_ver指定版本的私钥,使用RSA PKCS1算法对str1进行解密,得到解密内容str2.
c) 得到str2与对应消息的encrypt_chat_msg,调用下方描述的DecryptData接口,即可获得消息明文。

:需要注意的是,如果你用的json处理库会把+号自动转换为空格,要手动转换回来,不然会出错。

String encryptRandomKey = jsonObject.getString("encrypt_random_key");
encryptRandomKey = encryptRandomKey.replace(" ", "+");

比如说com.alibaba.fastjson.JSONObject

解密代码参考

String data = this.decodeWeworkData(encryptRandomKey);//解密
long msg = Finance.NewSlice();
int decryptDataReturn = Finance.DecryptData(sdk, data, encryptChatMsg, msg);//调用企微sdk获取消息明文msg
String decryDataMsg = Finance.GetContentFromSlice(msg);//消息明文,json格式
Finance.FreeSlice(msg);//释放

:最后decryDataMsg 就是解密后的消息明文。
下面是具体的解密方法

public String decodeWeworkData(String chatData) {//解密String str = null;try {str = getPrivateKeyByPKCS1(chatData, priKey);} catch (Exception e) {System.out.println("解密错误");e.printStackTrace();}return str;}
public static String getPrivateKeyByPKCS1(String encrypt_random_key,String privKeyPEM) throws Exception {String privKeyPEMnew = privKeyPEM.replaceAll("\\n", "").replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "");byte[] bytes = java.util.Base64.getDecoder().decode(privKeyPEMnew);DerInputStream derReader = new DerInputStream(bytes);DerValue[] seq = derReader.getSequence(0);BigInteger modulus = seq[1].getBigInteger();BigInteger publicExp = seq[2].getBigInteger();BigInteger privateExp = seq[3].getBigInteger();BigInteger prime1 = seq[4].getBigInteger();BigInteger prime2 = seq[5].getBigInteger();BigInteger exp1 = seq[6].getBigInteger();BigInteger exp2 = seq[7].getBigInteger();BigInteger crtCoef = seq[8].getBigInteger();RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(keySpec);// 64位解码加密后的字符串byte[] inputByte = Base64.decodeBase64(encrypt_random_key.getBytes("UTF-8"));Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");cipher.init(Cipher.DECRYPT_MODE, privateKey);//RSA解密String outStr = new String(cipher.doFinal(inputByte));return outStr;}

其中priKey是私钥,我是直接写在了代码里了,参考案例演示
在这里插入图片描述

:RSA使用PKCS1,模值为2048bit

SDK文件上传到服务器

例如使用Docker部署,添加如下代码至Dockerfile,

COPY src/main/resources/lib/qywx/linux/libWeWorkFinanceSdk.so /usr/lib/libWeWorkFinanceSdk.so

最后

希望对你有所帮助,有疑问和错误欢迎讨论和指正。

http://www.xdnf.cn/news/650395.html

相关文章:

  • 从零开始学电机(一)认识电机
  • [Java恶补day7] 42. 接雨水
  • 聊天室H5实时群聊聊天室全开源系统(源码下载)
  • 篇章三 基础——不可变类
  • 工信部中文点选验证码识别
  • Engineering a direct k-way Hypergraph Partitioning Algorithm【2017 ALENEX】
  • 基于JWT+Redis的登录流程实现
  • 分布式ID
  • 解决虚拟机挂起后,docker容器无法访问的问题
  • Qt6无法识别OpenCV(Windows端开发)
  • 【RabbitMQ】基于Spring Boot + RabbitMQ 完成应用通信
  • 七、【前端路由篇】掌控全局:Vue Router 实现页面导航、动态路由与权限控制
  • 2025/5/26 学习日记 基本/扩展正则表达式 linux三剑客之grep
  • [ARM][架构] 02.AArch32 程序状态
  • 3DVR拍摄指南:从理论到实践
  • [特殊字符] next-intl 服务端 i18n getTranslations 教程
  • 三分钟了解 MCP 概念(Model Context Protocol,模型上下文协议)
  • CLAM完整流程。patches-feature-split-train-eval
  • 5.26 面经整理 360共有云 golang
  • Java大师成长计划之第31天:Docker与Java应用容器化
  • 基于matlab版本的三维直流电法反演算法
  • 论文阅读: 2023 NeurIPS Jailbroken: How does llm safety training fail?
  • 支持selenium的chrome driver更新到136.0.7103.113
  • C++寻位映射的究极密码:哈希扩展
  • ubuntu 22.04 配置静态IP、网关、DNS
  • 鸿蒙OSUniApp 实现的日期选择器与时间选择器组件#三方框架 #Uniapp
  • 对数的运算困惑
  • 鸿蒙OSUniApp 开发带有通知提示的功能组件#三方框架 #Uniapp
  • Linux《基础IO》
  • 深入Java TCP流套接字编程:高效服务器构建与高并发实战优化指南​