Open In App

How to Create a Public/Private Key Pair?

Improve
Improve
Like Article
Like
Save
Share
Report

Public/private key pairs are the cryptographic tool that allows you to encrypt and decrypt data. A public key can be shared with anyone without worry, but a private key should never be given out without protection.

In Private key encryption, the same key (secret key) is used for encryption and decryption. In this key is symmetric because the only key is copied or shared by another party to decrypt the cipher text. It is faster than public-key cryptography.

In a Public key, two keys are used, one key is used for encryption and another key is used for decryption. One key (public key) is used to encrypt the plain text to convert it into cipher text and another key (private key) is used by the receiver to decrypt the cipher text to read the message.

The article focuses on creating public/private key pairs using OpenSSL.

OpenSSL

OpenSSL is a cryptography software library or toolkit that makes communication over computer networks more secure. The OpenSSL program is a command-line tool for using the various cryptography functions of OpenSSL’s crypto library from the shell. It is generally used for Transport Layer Security(TSL) or Secure Socket Layer(SSL) protocols. OpenSSL is licensed under an apache-style license, which means that under some simple license conditions, one can use the toolkit for commercial or non-commercial purposes.

  • Download OpenSSL’s required libraries from here
  • Run the .exe file and install OpenSSL in the system.
  • Open the command prompt (cmd) and redirect the path to the bin folder.
Redirect path to bin folder

 

Steps to Create Public/ Private Key Pair

Step 1: Creating a Private Key 

Type command openssl, hit enter and then use the following command to create private key:

genrsa -out myprivatekey.pem

Private key in OpenSSL

private key command

This command generates 512 bit long private key and stores it into a file having .pem extension

Output:

Private key

private key

Step 2: Create Public Key 

Type command openssl, hit enter and then use the following command to create public key:

rsa -in myprivatekey.pem -pubout -out mypublickey.pem 

Public key using OpenSSL

public key command

This command will create the public key for the private key created in the previous step.

Output:

Public Key

public key


Last Updated : 02 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads