Rsa 私钥解密
通常java 解密方法如下
/**
* RSA私钥解密
*
* @param str 加密字符串
* @param privateKey 私钥
* @return 明文
* @throws Exception 解密过程中的异常信息
*/
public static String decrypt(String str, String privateKey) {
//64位解码加密后的字符串
String outStr = null;
try {
//
byte[] inputByte = Base64.getDecoder().decode(str);
//base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
outStr = new String(cipher.doFinal(inputByte), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "decypt exception is" + e.getMessage());
}
return outStr;
}
如果Android 用这种方式创建Cipher 的话 会出现乱码
解决:
Cipher cipher = Cipher.getInstance("RSA");
替换成
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
完美解决!!