2

I generate the private key

openssl genrsa -out private_key.pem 2048

Then I generate the public key from the private key

openssl rsa -in private_key.pem -out public_key.pem -outform PEM -pubout 

I would like to hard-code these keys into my java program. And I want to use it with the PrivateKey and PublicKey classes in java.

My initial thought is I have to base64 encode the .pem files. Then there must be some java methods to convert the base64 encoded keys to their respective PrivateKey and PublicKey classes.

Thoughts?

Openssl CLI does have the base64 encode command but ....

There's the usual:

-----BEGIN PUBLIC KEY-----
keyinfo
-----END PUBLIC KEY-----

Do I need to parse these away before doing the base64 encode?

jww
  • 97,681
  • 90
  • 411
  • 885
MrBuggy
  • 423
  • 4
  • 12
  • I think the contents of that block is already base64 encoded. You should remove the BEGIN and END lines before base64 decoding them. Big Note: It may be fine to include your public key in your Java program, however you should not directly include your private key. See here for some more insight: https://rammic.github.io/2015/07/28/hiding-secrets-in-android-apps/ – Jamie Oct 24 '17 at 16:11
  • [Load RSA public key from file](https://stackoverflow.com/q/11410770/608639), [How to read .pem file to get private and public key](https://stackoverflow.com/q/11787571/608639), [Getting RSA private key from PEM BASE64 Encoded private key file](https://stackoverflow.com/q/7216969/608639), [How can I read a BouncyCastle private key PEM file using JCA?](https://stackoverflow.com/q/14228282/608639), etc. – jww Oct 25 '17 at 09:54

0 Answers0