You are here:

Public and private keys – what are they used for and how do you generate them?

A pair of keys – public and private – forms the basis of asymmetric cryptography. It enables the secure encryption of data, the creation of digital signatures and the authentication of users without the use of passwords.

What are cryptographic keys used for?

  • Public and private keys are used, amongst other things, for:
  • encrypting and protecting confidential data,
  • signing files and emails,
  • securely logging in to servers and network devices (SSH, routers, switches) without having to enter a password
Network cable with a padlock symbolising network security

 

How do you generate a key pair?

There are many ways to generate cryptographic keys. Here are the two most popular:


1. OpenSSL (Linux, macOS, Windows)

In the terminal, type the following commands in sequence:
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private.pem -out public.pem


This will generate a private key (private.pem) and its corresponding public key (public.pem).

 

2. PuTTYgen (Windows)

PuTTYgen is a simple tool for generating SSH keys.


Step by step:

  1. Download and install PuTTY from https://www.putty.org.
  2. Launch PuTTYgen.
  3. Select the key type – preferably Ed25519 (it is more modern and secure than RSA).
  4. Click Generate and move the mouse to generate randomness.
  5. Save the private key and copy the public key.
  6. Paste the public key into the ~/.ssh/authorized_keys file on the server.


From now on, you can log in to the server (e.g. a VPS or router) without a password – simply select your private key in PuTTY.

 

Summary

Public and private keys form the basis of secure online communication. They enable data encryption, user authentication and password-less login. In this article, we have shown you how to generate a key pair yourself using OpenSSL or PuTTYgen, and how to use it for SSH logins. This will make your connections to servers not only more convenient, but above all – significantly more secure.

 

Author: Marek Czwartyński, Senior IT Support Specialist, Support and Service. SI-Consulting