Generate RSA Key Pair using OpenSSL

Generate RSA Key Pair using OpenSSL

RSA is an asymmetric cryptography algorithm. Asymmetric means that two keys are used: private key and public key. This tutorial demonstrates how to generate an RSA key pair using OpenSSL.

The openssl genpkey command can be used for generating private keys. To generate the 2048-bit RSA private key, run the following command:

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out test.key

The meaning of options:

  • -algorithm RSA - RSA algorithm will be used to generate private key.
  • -pkeyopt - allows to specify public key options.
  • rsa_keygen_bits:2048 - option that specifies the number of bits in the generated key.
  • -out test.key - specifies where private key should be saved.

Once the private key was generated, use the following command to extract RSA public key from private key:

openssl rsa -in test.key -pubout -out test.pub

The meaning of options:

  • -in test.key - specifies the filename to read a private key.
  • -pubout - outputs a public key instead of a private key.
  • -out test.pub - specifies where public key should be saved.

Leave a Comment

Cancel reply

Your email address will not be published.