3

I'm trying to decrypt a password-encrypted private key in Java, and I've come across this previous question, but when I try the same, I get the following exception:

Exception in thread "main" java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = 48)
at sun.security.util.ObjectIdentifier.<init>(ObjectIdentifier.java:257)
at sun.security.util.DerInputStream.getOID(DerInputStream.java:314)
at com.sun.crypto.provider.PBES2Parameters.engineInit(PBES2Parameters.java:267)
at java.security.AlgorithmParameters.init(AlgorithmParameters.java:293)
at sun.security.x509.AlgorithmId.decodeParams(AlgorithmId.java:132)
at sun.security.x509.AlgorithmId.<init>(AlgorithmId.java:114)
at sun.security.x509.AlgorithmId.parse(AlgorithmId.java:372)
at javax.crypto.EncryptedPrivateKeyInfo.<init>(EncryptedPrivateKeyInfo.java:95)
at TestDecryptKey.main(TestDecryptKey.java:65)

where TestDecryptKey.java is my test file. Here's a snippet of what I'm doing:

import javax.crypto.EncryptedPrivateKeyInfo;
import javax.crypto.spec.PBEKeySpec;
import java.util.Base64;
import java.security.GeneralSecurityException;
import java.io.IOException;

public class TestDecryptKey {
  public static void main(String[] args) throws GeneralSecurityException, IOException {
    String encryptedBase64PrivateKey = "MII...";
    String password = "...";
    byte[] encryptedPrivateKey = Base64.getDecoder().decode(encryptedBase64PrivateKey.getBytes("UTF-8"));
    PBEKeySpec pbeSpec = new PBEKeySpec(password.toCharArray());
    // Exception is thrown here
    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey);
 }
}

I'm assuming it's claiming that the encrypted key data I've provided isn't a valid encrypted key, but openssl disagrees. I saved the Base64 string with the header and footer "-----BEGIN ENCRYPTED PRIVATE KEY-----" and "-----END ENCRYPTED PRIVATE KEY-----" as the file "pem_key" and using the following command and the password from above:

openssl pkcs8 -inform pem -in pem_key -outform der

I'm able to output the decrypted key bytes. Where am I going wrong?

srujzs
  • 340
  • 3
  • 14
  • It's clear that snippet cannot represent what's going on, because it doesn't compile. Please create a valid minimal, complete, and verifiable example. Compile and run it yourself and make sure it works before posting it. You can create a test private key that you only use for this example, and post that. – President James K. Polk Aug 16 '18 at 23:23
  • 5
    [JCE parsing of PBES2Parameters is buggy.](https://bugs.openjdk.java.net/browse/JDK-8076999) If you have or get BouncyCastle it works correctly for this, and is covered by existing Qs e.g. https://stackoverflow.com/questions/46767281/reading-pkcs8-in-pem-format-cannot-find-provider . Alternatively, Suncle will work if you create or re-create your PKCS8 to use a NON-PBES2 scheme: PKCS5-PBES1 schemes are mostly weak, but PKCS12-PBE are mostly okay. Or you could write the ASN.1 parsing yourself, but that's not easy. – dave_thompson_085 Aug 16 '18 at 23:35
  • 1
    Edited to make the example compile-able. @dave_thompson_085, I think that might be the case because I've tried re-encrypting the key with the passphrase after I decrypted it using openssl and then provided that new encrypted key to the EncryptePrivateKeyInfo which works. – srujzs Aug 17 '18 at 02:45

1 Answers1

1

I've got the same error when tying to use a keystore generated with x64 JDK's keytool for java app run with x32 JVM. Changing JVM to x64 helped.