OpenSSL: Generating an RSA Key From the Command Line

来源:互联网 发布:英语口语测试软件 编辑:程序博客网 时间:2024/05/19 03:42


原文:https://rietta.com/blog/2012/01/27/openssl-generating-rsa-key-from-command/


OpenSSL: Generating an RSA Key From the Command Line

JAN 27TH, 2012 ♦ 

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 me privately and it will be addressed ASAP. If the discovery is security related, please follow Rietta’s Responsible Disclosure contact procedure.

This post is one of the most visited on this blog and is a maintained post that is periodically updated to be most useful to you. For more, see the list of all maintained posts.


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.


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

The -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:

less public.pem

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.pem to verify that it starts with a -----BEGIN RSA PRIVATE KEY-----
  • less public.pem to 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:

private.pem
1234567891011121314151617181920212223242526272829
-----BEGIN RSA PRIVATE KEY-----Proc-Type: 4,ENCRYPTEDDEK-Info: DES-EDE3-CBC,32495A90F3FF199DlrMAsSjjkKiRxGdgR8p5kZJj0AFgdWYa3OT2snIXnN5+/p7j13PSkseUcrAFyokcV9pgeDfitAhb9lpdjxjjuxRcuQjBfmNVLPF9MFyNOvhrprGNukUh/12oSKO9dFEts39F/2h6Ld5IQrGt3gZaBB1aGO+tw3ill1VBy2zGPIDeuSz6DS3GG/oQ2gLSSMP4OVfQ32Oajo496iHRkdIh/7Hho7BNzMYr1GxrYTcE9/Znr6xgeSdNT37CCeCH8cmPaEAUgSMTeIMVSpILwkKeNvBURic1EWaqXRgPRIWK0vNyOCs/+jNoFISnV4pu1ROF92vayHDNSVw9wHcdSQ75XSE4Msawqv5U1iI7e2lD64uo1qhmJdrPcXDJQCiDbh+FhQhF+wAoLRvMNwwhg+LttL8vXqMDQl3olsWSvWPs6b/MZpB0qwd1bklzA6P+PeAUsfOvTqi9edIOfKqvXqTXEhBP8qC7ZtOKLGnryZb7W04SSVrNtuJU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oGH-----END RSA PRIVATE KEY-----

The public key, public.pem, file looks like:

public.pem
123456789
-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxzYuc22QSst/dS7geYYK5l5kLxU0tayNdixkEQ17ix+CUcUbKIsnyftZxaCYT46rQtXgCaYRdJcbB3hmyrOavkhTpX79xJZnQmfuamMbZBqitvscxW9zRR9tBUL6vdi/0rpoUwPMEh8+Bw7CgYR0FK0DhWYBNDfe9HKcyZEv3max8Cdq18htxjEsdYO0iwzhtKRXomBWTdhD5ykd/fACVTr4+KEY+IeLvubHVmLUhbE5NgWXxrRpGasDqzKhCTmsa2Ysf712rl57SlH0Wz/Mr3F7aM9YpErzeYLrl0GhQr9BVJxOvXcVd4kmY+XkiCcrkyS1cnghnllh+LCwQu1sYwIDAQAB-----END PUBLIC KEY-----

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.

Revision History

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 -pubout flag 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

 Jan 27th, 2012  Encryption, Maintained, Security, Web Application Topics

0 0
原创粉丝点击