Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read RSA private key of format PKCS1 in JAVA

Is it possible to read the RSA private key of format PKCS1 in JAVA without converting to PKCS8? if yes, sample code is appreciated.

-----BEGIN RSA PRIVATE KEY-----
 BASE64 ENCODED DATA
-----END RSA PRIVATE KEY-----
like image 734
Jenananthan Avatar asked Jan 30 '17 11:01

Jenananthan


People also ask

What is the file format of RSA private key?

This format is called PEM (Privacy Enhanced Email). The private key is encoded as a big blob of Base64 text. To parse it, you need to save it in a file and use the "asn1parse" command. Execute these commands to generate a "key.

What do I do with my RSA private key?

The RSA private key is used to generate digital signatures, and the RSA public key is used to verify digital signatures. The RSA public key is also used for key encryption of DES or AES DATA keys and the RSA private key for key recovery.

Can you generate RSA public key from private key?

According to https://www.madboa.com/geek/openssl/#key-rsa, you can generate a public key from a private key. My initial thinking was that they are generated in a pair together. Does the RSA private key contain the sum?


1 Answers

Java does not come with out-of-the-box support for PKCS1 keys. You can however use Bouncycastle

PEMParser pemParser = new PEMParser(new FileReader(privateKeyFile));
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
Object object = pemParser.readObject();
KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
PrivateKey privateKey = kp.getPrivate();
like image 130
pedrofb Avatar answered Nov 02 '22 23:11

pedrofb