企业微信会话存档解密

在企业微信会话存档解密中,腾讯官方文档是这样说明的:

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

对于解密不熟悉的人看了后 满脸问号???  什么意思,我该怎么搞?sdk FinanceDemo 文件中也没有展示这里该怎么解密。然后无从下手,网上文档也很少!!!  稀碎

翻译一下上面的

需要对接收到的消息中encrypt_random_key进行base64 decode, 然后使用对应的私钥,使用rsa PKCS1算法进行解密。

具体该怎么弄,废话不多说了,直接干

为了方便先在http://tool.chacuo.net/cryptrsapubkey 这个网址上生成2048位 PKCS1秘钥对 方便验证

用此方法先获取秘钥
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;
}

然后 对 encrypt_random_key 进行解密

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);/getPrivateKey()返回的privateKey

byte[] randomkeybyte = Base64.getDecoder().decode(encrypt_random_key );
byte[] finalrandomkeybyte = cipher.doFinal(randomkeybyte);
String finalrandomkey = new String(finalrandomkeybyte);
最后调用微信接口解密就行 
DecryptData(finalrandomkey,encrypt_chat_msg);
请注意,Python代码的格式对于程序的正确性非常重要。下面是修改后的代码: ```python from Crypto.Cipher import AES, PKCS1_OAEP from Crypto.Random import get_random_bytes from Crypto.Util.Padding import pad, unpad from Crypto.PublicKey import RSA # 生成RSA公私钥对 key = RSA.generate(2048) pub_key = key.publickey() # 加载明文文件 with open('p_text.txt', 'rb') as f: plain_text = f.read() # 生成随机密钥 session_key = get_random_bytes(16) # 用接收方公钥加密会话密钥 cipher_rsa = PKCS1_OAEP.new(pub_key) encrypted_key = cipher_rsa.encrypt(session_key) # 使用OFB模式加密明文文件 cipher_aes = AES.new(session_key, AES.MODE_OFB) cipher_text = cipher_aes.encrypt(pad(plain_text, AES.block_size)) # 保存密文文件 with open('c_text.txt', 'wb') as f: f.write(cipher_text) # 接收方用私钥解密会话密钥 cipher_rsa = PKCS1_OAEP.new(key) decrypted_key = cipher_rsa.decrypt(encrypted_key) # 用会话密钥解密密文文件 cipher_aes = AES.new(decrypted_key, AES.MODE_OFB) decrypted_text = unpad(cipher_aes.decrypt(cipher_text), AES.block_size) # 保存解密后的明文文件 with open('p1_text.txt', 'wb') as f: f.write(decrypted_text) # 比较明文文件和解密后的明文文件 with open('p_text.txt', 'rb') as f1, open('p1_text.txt', 'rb') as f2: if f1.read() == f2.read(): print('文件一致') else: print('文件不一致') ``` 这段代码的主要修改是对缩进进行了修正,使其符合 Python 代码的语法要求。同时,可以看到这段代码使用了 Python 标准库中的 `Crypto` 模块,来实现 RSA 和 AES 算法的加密和解密
评论 55
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值