解密代码如下:
/**
* 私钥解密
*
* @param ciphertext 被加密字符串
* @return 解密后字符串
*/
public static String decryptByPriKey(String ciphertext, String privateKeyStr) {
log.info("私钥解密 ciphertext:【{}】,privateKeyStr:【{}】",ciphertext ,privateKeyStr);
PrivateKey privateKey = null;
try {
Reader privateKeyReader = new StringReader(privateKeyStr);
PEMParser privatePemParser = new PEMParser(privateKeyReader);
Object privateObject = privatePemParser.readObject();
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
if (privateObject instanceof PEMKeyPair) {
PEMKeyPair pemKeyPair = (PEMKeyPair) privateObject;
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
privateKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo());
}
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
rsa.init(Cipher.DECRYPT_MODE, privateKey);
byte[] utf8 = rsa.doFinal(Base64.getDecoder().decode(ciphertext));
return new String(utf8, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
报错如下:
org.bouncycastle.jcajce.provider.util.BadBlockException: unable to decrypt block at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.getOutput(Unknown Source) at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source) at javax.crypto.Cipher.doFinal(Cipher.java:2164) at com.qdum.workwechat.common.utils.vm.RSAUtils.decryptByPriKey(RSAUtils.java:87) at com.qdum.workwechat.common.utils.vm.WorkMsgBackUpUtil.main(WorkMsgBackUpUtil.java:282) Caused by: org.bouncycastle.crypto.InvalidCipherTextException: block incorrect at org.bouncycastle.crypto.encodings.PKCS1Encoding.decodeBlock(Unknown Source) at org.bouncycastle.crypto.encodings.PKCS1Encoding.processBlock(Unknown Source) ... 5 more
疑问,是什么原因会导致这个问题那,会话存档公钥配置错误?
你好,用自己的rsa私钥解密encrypt_random_key 失败了吗,这个需要检查下私钥和代码
有解决的回来回复一下吧o(╥﹏╥)o
哥们 你解决了吗