I first generate a private RSA key file using OpenSSL, and then convert it to the encrypted der file:
$ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der
Next, I try to decrypt this file with Java using the following code (at this point I already read the file in the array byte[] keyusing the code that is at the bottom of this post):
public static byte[] decryptPrivateKey(byte[] key) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
PBEKeySpec passKeySpec = new PBEKeySpec("p".toCharArray());
EncryptedPrivateKeyInfo encryptedKey = new EncryptedPrivateKeyInfo(key);
System.out.println(encryptedKey.getAlgName());
System.out.println("key length: " + key.length);
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(encryptedKey.getAlgName());
SecretKey passKey = keyFac.generateSecret(passKeySpec);
Cipher pbeCipher = Cipher.getInstance(encryptedKey.getAlgName());
pbeCipher.init(Cipher.DECRYPT_MODE, passKey, encryptedKey.getAlgParameters());
return pbeCipher.doFinal(key);
}
I get the following stack trace in the above expression:
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.PBECipherCore.doFinal(PBECipherCore.java:422)
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(PBEWithMD5AndDESCipher.java:316)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at roland.test.crypto.Test.decryptPrivateKey(Test.java:96)
at roland.test.crypto.Test.getPrivateKey(Test.java:74)
at roland.test.crypto.Test.test(Test.java:58)
at roland.test.crypto.Test.main(Test.java:30)
The key is read from the der file as a byte array:
public static PrivateKey getPrivateKey() throws Exception {
byte[] key = null;
try(final InputStream resourceStream = getMyClass().getResourceAsStream("private_key.der")) {
key = ByteStreams.toByteArray(resourceStream);
} catch (IOException e) {
e.printStackTrace();
}
key = decryptPrivateKey(key);
}
source
share