Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrypt a pkcs8 encrypted private key using bouncy castle?

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);
like image 861
krk92 Avatar asked Apr 22 '15 06:04

krk92


1 Answers

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

like image 127
Christophe Avatar answered Nov 14 '22 23:11

Christophe