6

i am trying to decrypt a pkcs8 encrypted private key using bouncy castle library. I parsed the file containing the private key using PEMParser provided by bouncy castle. I got PKCS8EncryptedPrivateKeyInfo object. I am unable to get the PrivateKeyInfo object from this. I am getting the following exception while trying to decrypt.

org.bouncycastle.pkcs.PKCSException: unable to read encrypted data: 1.2.840.113549.1.5.13 not available: No such provider: 1.2.840.113549.1.5.13

here is the code which I used

PEMParser parser = new PEMParser(br);
PKCS8EncryptedPrivateKeyInfo pair =       (PKCS8EncryptedPrivateKeyInfo)parser.readObject();
JceOpenSSLPKCS8DecryptorProviderBuilder jce = new JceOpenSSLPKCS8DecryptorProviderBuilder();
                jce.setProvider("1.2.840.113549.1.5.13");
                InputDecryptorProvider decProv = jce.build(password.toCharArray());
                PrivateKeyInfo info = pair.decryptPrivateKeyInfo(decProv);
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
krk92
  • 69
  • 1
  • 4
  • 1
    Did you see this http://stackoverflow.com/questions/2654949/how-to-read-a-password-encrypted-key-with-java ? – psyched Apr 22 '15 at 07:00

2 Answers2

6

Have you tried with jce.setProvider("BC"); instead of jce.setProvider("1.2.840.113549.1.5.13");

Edit to add solution provided by @PeterDettman :

In addition to use jce.setProvider("BC"); also install the BC provider bouncycastle.org/wiki/display/JA1/Provider+Installation

Christophe
  • 2,131
  • 2
  • 21
  • 35
  • Yes tried that too. This is the exception i got org.bouncycastle.pkcs.PKCSException: unable to read encrypted data: 1.2.840.113549.1.5.13 not available: No such provider: BC – krk92 Apr 22 '15 at 07:23
  • 1
    @krk92 It should definitely be setProvider("BC"). Have you actually installed the BC provider? (http://www.bouncycastle.org/wiki/display/JA1/Provider+Installation) – Peter Dettman Apr 22 '15 at 07:36
  • I didn't install BC provider. Now it worked after installing. Thank you. – krk92 Apr 22 '15 at 08:55
  • This solved it for me, as well. The wiki has moved to the github repo, for reference: https://github.com/bcgit/bc-java/wiki/Provider-Installation – blimmer Aug 16 '22 at 22:06
1

Security.addProvider(new BouncyCastleProvider());

thats whats missing

kallianas
  • 121
  • 1
  • 1
  • 6