Re: "No such algorithm: 1.2.840.113549.1.5.13" occurs when creating an EncryptedPrivateKey instance.

Satoru Otsubo <trnsec-96mi//[email protected]> Wed, 7 Nov 2018 15:05:09 +0900
Newsgroups gmane.comp.encryption.bouncy-castle.devel
Message-ID <[email protected]>
Hi Peter,

Thank you for your program code.

Since I just wanted to get Privatekey to be used in creating CMSSignedData through CMSSignedDataGenerator,
Your code is very useful for me.

I took many hours to solve why no such algorithm occurs.
Your code let me solve it very quickly.
Thank you.


Satoru Otsubo


> Hi Satoru,
> 
> To parse openssl-generated keys, you're better off using the bcpkix
> jar's org.bouncycastle.openssl package. This example code will read a
> key generated with the command line you used:
> 
> 
> import java.io.FileInputStream;
> import java.io.InputStreamReader;
> import java.security.PrivateKey;
> import java.security.Security;
> 
> import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
> import org.bouncycastle.jce.provider.BouncyCastleProvider;
> import org.bouncycastle.openssl.PEMParser;
> import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
> import
> org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
> import org.bouncycastle.operator.InputDecryptorProvider;
> import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
> 
> public class PEMTest
> {
>     public static void main(String[] args) throws Exception
>     {
>         Security.addProvider(new BouncyCastleProvider());
> 
>         PEMParser pemParser = new PEMParser(new InputStreamReader(new
> FileInputStream("key.pem")));
> 
>         PKCS8EncryptedPrivateKeyInfo encPKInfo =
> (PKCS8EncryptedPrivateKeyInfo)pemParser.readObject();
> 
>         InputDecryptorProvider decProv = new
> JceOpenSSLPKCS8DecryptorProviderBuilder().setProvider("BC").build("password".toCharArray());
>         PrivateKeyInfo pkInfo = encPKInfo.decryptPrivateKeyInfo(decProv);
> 
>         PrivateKey privKey = new
> JcaPEMKeyConverter().setProvider("BC").getPrivateKey(pkInfo);
>     }
> }
> 
> 
> This doesn't exactly answer your question, but I guess if you are really
> curious about how it works, it's a good starting point.
> 
> Regards,
> Pete Dettman
> 
> 
> On 6/11/18 6:36 pm, Satoru Otsubo wrote:
> > Hi,
> > 
> > I made a self-signed certificate and produced an encrypted key.pem.
> >  by using the following command and entering a password:
> > 
> >  $ openssl req -x509 -newkey rsa:2048 -keyout key.pem -outform pem -out cert.pem -days 365
> > 
> > Then, I tried to make the javax.crypto.EncryptedPrivateKeyInfo instance from the above key.pem with the program below,
> >  by referring to org.bouncycastle.jce.provider.test.EncryptedPrivateKeyInfoTest.java
> > (I used jdk1.7.0_80 and ***-jdk15on-160.jar)
> > 
> > But "java.security.NoSuchAlgorithmException: No such algorithm: 1.2.840.113549.1.5.13" occured.
> > 
> > As there is also org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo, I tried to use it.
> > 
> > But even in this case,"java.security.NoSuchAlgorithmException: No such algorithm: 1.2.840.113549.1.5.13" occured.
> > 
> > I appreciate if anyone can explain why "No such algorithm: 1.2.840.113549.1.5.13" occurs.
> > 
> > Thanks in advance.
> > 
> > Satoru Otsubo
> 


-- 
Satoru Otsubo <student-96mi//[email protected]>