收藏
回答

企业微信会话存解密问题: 报错信息:too much data for RSA block?

  public static String decryptRSA(String str, String privateKey) throws Exception {
     Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
     Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
     rsa.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKey));
     byte[] s = Base64.decodeBase64(str);
     byte[] utf8 = rsa.doFinal(s);  // 这行报错
     String result = new String(utf8,"UTF-8");
     return result;
}

 public static PrivateKey getPrivateKey (String privateKey) throws Exception {
     Reader privateKeyReader = new StringReader(privateKey);
     PEMParser privatePemParser = new PEMParser(privateKeyReader);
     Object privateObject = privatePemParser.readObject();
     if (privateObject instanceof PEMKeyPair) {
         PEMKeyPair pemKeyPair = (PEMKeyPair) privateObject;
         JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
         PrivateKey privKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo());
         return privKey;
     }
     return null;
 }
回答关注问题邀请回答
收藏
登录 后发表内容