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

企微获取会话内容,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.lryc.cn/news/2386984.html

相关文章:

  • MyBatis入门:快速搭建数据库操作框架 + 增删改查(CRUD)
  • 离线安装Microsoft 照片【笔记】
  • 地理卷积神经网络加权回归模型的详细实现方案
  • 【后端高阶面经:Elasticsearch篇】39、Elasticsearch 查询性能优化:分页、冷热分离与 JVM 调优
  • 光伏电站及时巡检:守护清洁能源的“生命线”
  • 基于 ZU49DR FPGA 的无线电射频数据采样转换开发平台核心板
  • 软考 系统架构设计师系列知识点之杂项集萃(69)
  • 从源码编译支持ffmpeg(H264编码)的opencv(创建mp4视频报错:H264 is not supported with codec id 28)
  • leetcode 83和84 Remove Duplicates from Sorted List 和leetcode 1836
  • 每日leetcode(昨天赶飞机没做,今天补)
  • SDL2常用函数:SDL_BlitSurfaceSDL_UpdateWindowSurface 数据结构及使用介绍
  • 【LeetCode 热题 100】买卖股票的最佳时机 / 跳跃游戏 / 划分字母区间
  • 万亿参数背后的算力密码:大模型训练的分布式架构与自动化运维全解析
  • LangChain03-图数据库与LangGraph
  • rabbitmq单机多实例部署
  • Linux10正式版发布,拥抱AI了!
  • 在离线 OpenEuler-22.03 服务器上升级 OpenSSH 的完整指南
  • 全能邮箱全能邮箱:实现邮件管理的自动化!
  • [特殊字符] Linux 日志查看与分析常用命令全攻略
  • mysql-tpcc-mysql压测工具使用
  • Qt找不到windows API报错:error: LNK2019: 无法解析的外部符号 __imp_OpenClipboard
  • 机试 | vector/array Minimum Glutton C++
  • OpenCv高阶(十七)——dlib库安装、dlib人脸检测
  • 前端内容黑白处理、轮播图、奇妙的头像特效
  • 蓝桥杯 10. 安全序列
  • (10)-java+ selenium->元素之By class name
  • Git - .gitignore 文件
  • MPI与多线程(如OpenMP)混合编程注意事项与性能优化
  • 计算机网络学习(八)——MAC
  • 英语六级-阅读篇