Skip to content
Advertisement

Load RSA public key from file

I’ve generated a private key with:

JavaScript

After this I’ve generated a public key with:

JavaScript

I want to sign some messages with my private key, and verify some other messages with my public key, using code like this:

JavaScript

I found a solution to convert my private key to PKCS8 format and load it. It works with some code like this:

JavaScript

And finally my question is: How do I load my RSA Public Key from a file?

I think maybe I need to convert my public key file to x509 format, and use X509EncodedKeySpec. But how can I do this?

Advertisement

Answer

Below is the relevant information from the link which Zaki provided.

Generate a 2048-bit RSA private key

$ openssl genrsa -out private_key.pem 2048

Convert private Key to PKCS#8 format (so Java can read it)

$ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der -nocrypt

Output public key portion in DER format (so Java can read it)

$ openssl rsa -in private_key.pem -pubout -outform DER -out public_key.der

Private key

JavaScript

Public key

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement