惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 是谁啊?

jenkins 权限控制(用户只能看指定的项目) NoSQLBooster for MongoDB延长-试用期 使用openVPN组建企业局域网,连接之后无法访问互联网的问题解决 - 是谁啊? mac os socks5转http proxy M1 Mac Xcode模拟器无法运行 解决git“Unable to negotiate with 218.244.143.137 port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss”的问题 macos 升级后提示 python 命令需要使用命令行开发者工具,你要现在安装该工具吗 解决 Android studio 代理 Connect to 127.0.0.1:[/127.0.0.1] failed: Connection refused nginx 代理https后,应用redirect https变成http centos6无法安装nginx Postfix maillog邮件发送各阶段延时的日志记录 高德坐标系转wgs(苹果坐标系) java代码 How Do I test James Distributed Version with JMeter? springBoot项目不重新上传jar包,增量升级步骤 mac安装homebrew(国内) 启动apache,将debug日志输出在console窗口 转:solr6.0配置中文分词器IK Analyzer git 忽略提交某个指定的文件(不从版本库中删除) git pull 和本地文件冲突问题解决
企业微信会话存档消息解密(Java RSA PKCS1解密)
是谁啊? · 2020-03-22 · via 博客园 - 是谁啊?

https://www.cnblogs.com/eleclsc/p/12082000.html

转:https://www.cnblogs.com/zengsf/p/10136886.html

在线rsa加解密工具http://tool.chacuo.net/cryptrsaprikey

在linux环境中生成公私钥:

openssl
然后生成私钥:

genrsa -out app_private_key.pem 2048 # 私钥的生成
在利用私钥生成公钥:

rsa -in app_private_key.pem -pubout -out app_public_key.pem #导出公钥
这样就生成了rsa2的私钥和公钥了。可以用于支付宝的公密钥的生成

pkcs1转pkcs8
openssl pkcs8 -topk8 -inform PEM -in app_private_key.pem -outform pem -nocrypt -out pkcs8.pem

1. 只需要使用pkcs1

Q:拉取的消息如何解密?
A:企业微信对待存储的消息使用企业最新上传的公钥进行加密,企业自行使用对应版本的私钥进行解密。对json消息体内的encrypt_random_key,base64处理后首先使用对应版本的私钥进行解密得到字符串内容,将字符串内容与消息体内的encrypt_chat_msg,使用sdk的接口DecryptData,得到真正的明文消息。

这文档写的,稀烂。

如果只看上面的描述,并且你用Java去实现,那么在解密时,你十有八九是搞不定的。其实在接口文档处,还有一处这样的描述:

encrypt_random_key是使用企业在管理端填写的公钥(使用模值为2048bit的秘钥),采用RSA加密算法进行加密处理后base64 encode的内容,加密内容为企业微信产生。RSA使用PKCS1。
企业得到消息内容后,需先进行base64 decode,使用消息指明版本的私钥,使用RSA PKCS1算法进行解密,得到解密内容,为下一步进行消息明文解析做准备。

翻译一下:

需要对接收到的消息中encrypt_random_key进行base64 decode, 然后使用对应的私钥,使用rsa算法进行解密,注意需要使用RSA PKCS1。对于Java来说,大都使用PKCS8,所以需要注意在实现算法时,需要正确选择文档所说的算法。

私钥是PKCS1(一般以如下开头:begin rsa private key)或者是PKCS8(一般以 begin private key 开头)版本的,都ok,只要选择了正确的私钥读取方式就ok。

读取公钥:

public static PublicKey getPublicKey(String base64PublicKey){
PublicKey publicKey = null;
try{
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return publicKey;
}

读取pkcs1格式的private key
public static PrivateKey getPrivateKey(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);
return privateKey;
}


java rsa pkcs1解密实现:
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(data));

加密时只需要在cipher.init时传入对应mode为Cipher.ENCRYPT_MODE, publicKey即可。

对于已经base64 encode的字符串解密时,只需要先进行base64.decode,得到byte[]作为要解密的data进行解密即可。


参考资料:
https://www.devglan.com/java8/rsa-encryption-decryption-java
http://defned.com/post/java-rsa-pkcs1/

https://github.com/greigdp/Javacard-ALG_RSA_SHA256_PKCS1