Last Updated on March 18, 2014. Thanks to a tip from Chase Schultz, a security researcher in San Francisco, it came to my attention that the instructions in this post contained an error and if you followed them exactly you ended up exporting copy of the private key rather than the public key.
I have updated this post to correct the error related to the command used to export the public key. The
-pubout flag had been editorially dropped in error when this blog was converted to Markdown format from the old Blogger site. The example has been corrected and additional information about how to visually inspect the generated key file to ensure that it is a public key and not a private key has been added.
If you discover an error in the content on this post or any post on this blog, please contact email@example.com privately and it will be addressed ASAP.
This post is part of our ongoing Encryption Series that provides in-depth coverage of OpenSSL. To learn more about encryption key generation, management, and use please see the posts in the Encryption category. Our tips and tricks are immediately applicable with examples that you can use right away. If you like this article, you may be interested in the Raspberry Pi crypto key management project as well as Rietta’s Application Security Learning Center, our catalog of video resources on how to succeed with web application security.
A few of weeks ago, I posted about how to Encrypt a File with a Password from the Command Line using OpenSSL. While very useful in its own right, the real power of the OpenSSL library is its ability to support the use of public key cryptograph for encrypting or validating data in an unattended manner (where the password is not required to encrypt) is done with public keys.
The Commands to Run
Generate a 2048 bit RSA Key
You can generate a public and private RSA key pair like this:
openssl genrsa -des3 -out private.pem 2048
That generates a 2048-bit RSA key pair, encrypts them with a password you provide, and writes them to a file. You need to next extract the public key file. You will use this, for instance, on your web server to encrypt content so that it can only be read with the private key.
Export the RSA Public Key to a File
This is a command that is
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
-pubout flag is really important. Be sure to include it.
Next open the
public.pem and ensure that it starts with a
-----BEGIN PUBLIC KEY-----. This is how you know that this file is the public key of the pair and not a private key.
To check the file from the command line you can use the
less command, like this:
Do Not Run This, it Exports the Private Key
A previous version of the post gave this example in error.
openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM
The error is that the
-pubout was dropped from the end of the command. That changes the meaning of the command from that of exporting the public key to exporting the private key outside of its encrypted wrapper. Inspecting the output file, in this case
private_unencrypted.pem clearly shows that the key is a RSA private key as it starts with
-----BEGIN RSA PRIVATE KEY-----.
Visually Inspect Your Key Files
It is important to visually inspect you private and public key files to make sure that they are what you expect. OpenSSL will clearly explain the nature of the key block with a
-----BEGIN RSA PRIVATE KEY----- or
-----BEGIN PUBLIC KEY-----.
You can use less to inspect each of your two files in turn:
less private.pemto verify that it starts with a
-----BEGIN RSA PRIVATE KEY-----
less public.pemto verify that it starts with a
-----BEGIN PUBLIC KEY-----
The next section shows a full example of what each key file should look like.
The Generated Key Files
The generated files are base64-encoded encryption keys in plain text format. If you select a password for your private key, its file will be encrypted with your password. Be sure to remember this password or the key pair becomes useless.
The private.pem file looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
The public key, public.pem, file looks like:
1 2 3 4 5 6 7 8 9
Protecting Your Keys
Depending on the nature of the information you will protect, it’s important to keep the private key backed up and secret. The public key can be distributed anywhere or embedded in your web application scripts, such as in your PHP, Ruby, or other scripts. Again, backup your keys!
Remember, if the key goes away the data encrypted to it is gone.Keeping a printed copy of the key material in a sealed envelope in a bank safety deposit box is a good way to protect important keys against loss due to fire or hard drive failure.
But! How do I use this Key?
Don’t worry, I will write about how to use your freshly minted RSA keys in a future blog post. In the mean time checkout the PHP functionality at http://us2.php.net/openssl_seal. Hint, you can use your new public key with openssl_seal.
I hope this helps!
Oh, and one last thing.
If you, dear reader, were planning any funny business with the private key that I have just published here. Know that they were made especially for this series of blog posts. I do not use them for anything else.
Invitation to the Web Application Topics Newsletter
This post is part of the Web Application Topics series. If you are interested in having future issues sent directly to you by e-mail, please sign up for free, today. For back issues, see the Web Application Topics category on this blog.
As a maintained post, this document is updated from time to time.
- March 18, 2014: Corrected error related to the command used to export the public RSA key. The
-puboutflag had been editorially dropped in error when this blog was converted to Markdown format from Blogger. The example has been corrected and additional content about checking the type of the key added.
- January 3, 2014: Added the video version to visually demonstrate the process of generating a military-grade RSA key pair in Mac OS X Mavericks.
- October 6, 2013: Promoted to a Maintained Post status
- September 3, 2013: Reformatted as part of the migration to the new blog.
- January 27, 2012: Originally posted on The Rietta Blog, hosted on the Blogger platform