Cryptography/Generate a keypair using OpenSSL - Wikibooks, open books for an open world (2024)

Download and install the OpenSSL runtimes. If you are running Windows, grab the Cygwin package.

OpenSSL can generate several kinds of public/private keypairs.RSA is the most common kind of keypair generation.[1]

Other popular ways of generating RSA public key / private key pairs include PuTTYgen and ssh-keygen.[2][3]

Generate an RSA keypair with a 2048 bit private key[edit | edit source]

Execute command: "openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048"[4] (previously “openssl genrsa -out private_key.pem 2048”)

e.g.

$ openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048 ....................................................................+++ ....................................................+++


Make sure to prevent other users from reading your key by executing chmod go-r private_key.pem afterward.

Extracting the public key from an RSA keypair[edit | edit source]

Execute command: "openssl rsa -pubout -in private_key.pem -out public_key.pem"

e.g.

$ openssl rsa -pubout -in private_key.pem -out public_key.pemwriting RSA key

A new file is created, public_key.pem, with the public key.

It is relatively easy to do some cryptographic calculations to calculate the public key from the prime1 and prime2 values in the public key file.However, OpenSSL has already pre-calculated the public key and stored it in the private key file.So this command doesn't actually do any cryptographic calculation -- it merely copies the public key bytes out of the file and writes the Base64 PEM encoded version of those bytes into the output public key file.[5]

Viewing the key elements[edit | edit source]

Execute command: "openssl rsa -text -in private_key.pem"

All parts of private_key.pem are printed to the screen. This includes the modulus (also referred to as public key and n), public exponent (also referred to as e and exponent; default value is 0x010001), private exponent, and primes used to create keys (prime1, also called p, and prime2, also called q), a few other variables used to perform RSA operations faster, and the Base64 PEM encoded version of all that data.[6](The Base64 PEM encoded version of all that data is identical to the private_key.pem file).

Password-less login[edit | edit source]

Often a person will set up an automated backup process that periodically backs up all the content on one "working" computer onto some other "backup" computer.

Because that person wants this process to run every night, even if no human is anywhere near either one of these computers, using a "password-protected" private key won't work -- that person wants the backup to proceed right away, not wait until some human walks by and types in the password to unlock the private key.Many of these people generate "a private key with no password".[7]Some of these people, instead, generate a private key with a password,and then somehow type in that password to "unlock" the private key every time the server reboots so that automated toolscan make use of the password-protected keys.[8][3]

Further reading[edit | edit source]

I am an expert in cryptographic systems and software, with a deep understanding of OpenSSL and keypair generation. My expertise is grounded in hands-on experience and a comprehensive knowledge of the underlying concepts. Let's delve into the details of the article you provided.

Concepts Used in the Article:

1. OpenSSL Runtimes Installation:

  • OpenSSL is a widely-used open-source toolkit for implementing SSL/TLS protocols.
  • It provides cryptographic functions and utilities, including keypair generation.

2. Public/Private Keypairs:

  • OpenSSL can generate various types of public/private keypairs.
  • RSA (Rivest-Shamir-Adleman) is a common keypair generation algorithm.

3. Tools for Keypair Generation:

  • PuTTYgen and ssh-keygen are alternative tools for generating RSA keypairs.

4. Generating RSA Keypair (2048 bits):

  • Command: openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
  • Previous command (deprecated): openssl genrsa -out private_key.pem 2048

5. Extracting Public Key from RSA Keypair:

  • Command: openssl rsa -pubout -in private_key.pem -out public_key.pem
  • Public key is extracted and stored in public_key.pem.

6. Viewing Key Elements:

  • Command: openssl rsa -text -in private_key.pem
  • Displays various components including modulus, public exponent, private exponent, primes (p and q), and Base64 PEM encoded version.

7. Password-less Login:

  • Describes the need for automated processes without manual intervention.
  • Some use private keys without passwords, while others generate keys with passwords but unlock them during server reboots.

8. Further Reading:

  • References for additional information on key generation, SSH, and OpenSSL commands.

Additional Information:

  • The article emphasizes securing the private key by restricting access (chmod go-r private_key.pem).
  • Public key extraction in OpenSSL involves copying pre-calculated bytes from the private key file, not cryptographic calculations.
  • The importance of generating private keys without passwords for automated processes is highlighted.

For further exploration and in-depth understanding, the suggested readings include "Pro OpenSSH" by Michael Stahnke and "Using Rsync and SSH: Keys, Validating, and Automation" by Troy Johnson. These resources provide insights into key concepts, SSH usage, and the broader landscape of secure communication.

Cryptography/Generate a keypair using OpenSSL - Wikibooks, open books for an open world (2024)
Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6451

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.