错误代码事例
public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
// 对密钥解密
byte[] keyBytes =decryptBASE64(key);
// 取得公钥
X509EncodedKeySpec x509KeySpec =new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
// 对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
正确事例
private static StringRSAC ="RSA/ECB/PKCS1Padding";
public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
// 对密钥解密
byte[] keyBytes =decryptBASE64(key);
// 取得公钥
X509EncodedKeySpec x509KeySpec =new X509EncodedKeySpec(keyBytes);
KeyFactorykeyFactory= KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey =keyFactory.generatePublic(x509KeySpec);
// 对数据解密
Cipher cipher = Cipher.getInstance(RSAC);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}