Cryptography/Generate a keypair using OpenSSL
| This page was transwikied from another project and needs to be adopted.
This page needs to be moved to the main namespace, either as the main page of a book, or as a chapter of another book. After moving, please remove
OpenSSL can generate several kinds of public/private keypairs. RSA is the most common kind of keypair generation.
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" (previously “openssl genrsa -out private_key.pem 2048”)
$ 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"
$ openssl rsa -pubout -in private_key.pem -out public_key.pem writing 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.
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. (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". 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 tools can make use of the password-protected keys.
Further reading[edit | edit source]
- Key Generation
- Michael Stahnke. "Pro OpenSSH". p. 247.
- "SourceForge.net Documentation: SSH Key Overview"
- "genpkey(1) - Linux man page"
- "Public – Private key encryption using OpenSSL"
- "OpenSSL 1024 bit RSA Private Key Breakdown"
- "DreamHost: Personal Backup".
- Troy Johnson. "Using Rsync and SSH: Keys, Validating, and Automation".
- Internet_Technologies/SSH describes how to use "ssh-keygen" and "ssh-copy-id" on your local machine so you can quickly and securely ssh from your local machine to a remote host.
- "OpenSSL: Command Line Utilities: Create / Handle Public Key Certificates"