请问解决了吗?我也遇到了这个问题
会话存档解密encryptRandomKey突然开始失败,不知道是什么原因?之前一已经写好了,在服务器上面运行有半年了,突然日志报错,说解密失败,秘钥公钥等等都没有动过。 下面是解密的代码:秘钥文件是可以读取出来的,就是在doFinal的时候报错了。 public static void boot() throws IOException, KeyException, NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); String key = getKeyString(); Reader privateKeyReader = new StringReader(key); PEMParser privatePemParser = new PEMParser(privateKeyReader); Object privateObject = privatePemParser.readObject(); PrivateKey privateKey; if (privateObject instanceof PEMKeyPair pemKeyPair) { JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); privateKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo()); } else { throw new KeyException("rsa秘钥错误"); } // 初始化解密类 rsa.init(Cipher.DECRYPT_MODE, privateKey); } private static String getKeyString() throws IOException { final FileInputStream inputStream = new FileInputStream(Config.get("private_key_file")); final byte[] bytes = inputStream.readAllBytes(); final String s = new String(bytes); inputStream.close(); return s; } public static String decryptRSA(String encryptRandomKey) throws IllegalBlockSizeException, BadPaddingException { byte[] decodeKey = Base64.getDecoder().decode(encryptRandomKey); byte[] utf8 = Decipherer.rsa.doFinal(decodeKey); return new String(utf8, StandardCharsets.UTF_8); } 下面是异常信息: org.bouncycastle.jcajce.provider.util.BadBlockException: unable to decrypt block at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.getOutput(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at javax.crypto.Cipher.doFinal(Cipher.java:2205) ~[?:?] at com.pjcy.pullwechatmsg.Decipherer.decryptRSA(Decipherer.java:56) ~[getwechatmsg.jar:?] at com.pjcy.pullwechatmsg.Decipherer.decrypt(Decipherer.java:63) ~[getwechatmsg.jar:?] at com.pjcy.pullwechatmsg.ChatSet.decrypt(ChatSet.java:48) ~[getwechatmsg.jar:?] at com.pjcy.pullwechatmsg.Dispatcher.store(Dispatcher.java:57) ~[getwechatmsg.jar:?] at com.pjcy.pullwechatmsg.Dispatcher.start(Dispatcher.java:41) ~[getwechatmsg.jar:?] at com.pjcy.pullwechatmsg.ServiceLaunch.lambda$init$0(ServiceLaunch.java:28) ~[getwechatmsg.jar:?] at java.lang.Thread.run(Thread.java:833) ~[?:?] Caused by: org.bouncycastle.crypto.InvalidCipherTextException: block incorrect at org.bouncycastle.crypto.encodings.PKCS1Encoding.decodeBlock(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.crypto.encodings.PKCS1Encoding.processBlock(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] ... 10 more
2024-09-19