大约一年前,我为Android编写了一个应用程序,并在其中使用了一个类RSA在这个类中,有以下代码片段,应用程序工作正常
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
但是当我重新输入应用程序代码时,我没有打开新的加密信息来更改私钥,直到我将上面的代码行更改为下面的代码行。
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
问题是,如果我在RSA类中替换上面的代码片段,就不可能再打开以前加密的信息(使用与以前相同的密钥)。我看到了下面的错误
javax.crypto.BadPaddingException: error:04000084:RSA routines:OPENSSL_internal:PKCS_DECODING_ERROR
RSA解密
public static byte[] decryptByPrivateKey(byte[] data, String key)
throws Exception {
byte[] keyBytes = decryptBASE64(key);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());