Rsa_generate_key_ex Example



RSA_generate_key_ex, RSA_generate_key - generate RSA key pair

  1. A openssl eBooks created from contributions of Stack Overflow users.
  2. In order to generate an RSA key, an EVPPKEY must first be allocated with EVPPKEYnew. EVPPKEY.pkey; pkey = EVPPKEYnew; An exponent for the key is also needed, which will require allocating a BIGNUM with BNnew and then assigning with BNsetword.; bn = BNnew; BNsetword(bn, RSAF4).

In order to generate an RSA key, an EVPPKEY must first be allocated with EVPPKEYnew. EVPPKEY.pkey; pkey = EVPPKEYnew; An exponent for the key is also needed, which will require allocating a BIGNUM with BNnew and then assigning with BNsetword.




RSA_generate_key_ex() generates a key pair and stores it in the RSA structure provided in rsa. The pseudo-random number generator must be seeded prior to calling RSA_generate_key_ex().

The modulus size will be of length bits, and the public exponent will be e. Key sizes with num < 1024 should be considered insecure. The exponent is an odd number, typically 3, 17 or 65537.

A callback function may be used to provide feedback about the progress of the key generation. If cb is not NULL, it will be called as follows using the BN_GENCB_call() function described on the BN_generate_prime(3) page.

  • While a random prime number is generated, it is called as described in BN_generate_prime(3).

  • When the n-th randomly generated prime is rejected as not suitable for the key, BN_GENCB_call(cb, 2, n) is called.

  • When a random p has been found with p-1 relatively prime to e, it is called as BN_GENCB_call(cb, 3, 0).

The process is then repeated for prime q with BN_GENCB_call(cb, 3, 1).

RSA_generate_key is deprecated (new applications should use RSA_generate_key_ex instead). RSA_generate_key works in the same way as RSA_generate_key_ex except it uses 'old style' call backs. See BN_generate_prime(3) for further details.


If key generation fails, RSA_generate_key() returns NULL.

The error codes can be obtained by ERR_get_error(3).


ExampleRsa_generate_key_exRsa_generate_key_ex Example

BN_GENCB_call(cb, 2, x) is used with two different meanings.

RSA_generate_key() goes into an infinite loop for illegal input values.


ERR_get_error(3), rand(3), rsa(3), RSA_free(3), BN_generate_prime(3)


The cb_arg argument was added in SSLeay 0.9.0.

While Encrypting a File with a Password from the Command Line using OpenSSLis very useful in its own right, the real power of the OpenSSL library is itsability to support the use of public key cryptograph for encrypting orvalidating data in an unattended manner (where the password is not required toencrypt) 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 provideand writes them to a file. You need to next extract the public key file. You willuse this, for instance, on your web server to encrypt content so that it canonly 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-----BEGIN PUBLIC KEY-----. This is how you know that this file is thepublic 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 keyto exporting the private key outside of its encrypted wrapper. Inspecting theoutput file, in this case private_unencrypted.pem clearly shows that the keyis a RSA private key as it starts with -----BEGIN RSA PRIVATE KEY-----.

Rsa_generate_key_ex Example

Visually Inspect Your Key Files

It is important to visually inspect you private and public key files to makesure that they are what you expect. OpenSSL will clearly explain the nature ofthe 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.

Generate Rsa Key Linux

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 withyour password. Be sure to remember this password or the key pair becomes useless.

The private.pem file looks something like this:

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

Protecting Your Keys

Depending on the nature of the information you will protect, it’s important tokeep the private key backed up and secret. The public key can be distributedanywhere 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 aprinted copy of the key material in a sealed envelope in a bank safety depositbox is a good way to protect important keys against loss due to fire or harddrive failure.

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.

Rsa_generate_key_ex Example

Found an issue?

Rietta plans, develops, and maintains applications.

Rsa_generate_key_ex Example C

Learn more about our services or drop us your email and we'll e-mail you back.

Powershell Generate Rsa Key

Other Blog Articles Published by