Setting Up Your RSA Toolkit — Python, pycryptodome and OpenSSL

Install Python, set up a virtual environment, and add pycryptodome (plus optional OpenSSL) — your toolkit for the rest of the RSA course.

Overview

Before diving into the RSA construction itself, let's set up the tools we'll use throughout the rest of the course. The whole stack is small:

  • Python3 — all code in this course is written in Python
  • pycryptodome — the cryptography library used for primes and key generation
  • openssl (optional) — to inspect real-world keys produced by industrial tooling

If you already have a working Python environment with pycryptodome installed, you can skip ahead to chapter 3.

Installing Python

Most Linux distributions ship Python 3 by default. Check your version:

python3 --version
You need Python 3.8 or newer. If you don't have it, install it from your package manager: - Debian / Ubuntu: sudo apt install python3 python3-venv python3-pip - Arch / Manjaro: sudo pacman -S python python-pip - Fedora: sudo dnf install python3 python3-pip - macOS (with Homebrew): brew install python - Windows: download from python.org — check "Add Python to PATH" during install.

Setting up a virtual environment

We'll work inside a virtual environment to keep our dependencies isolated from the system Python. This is the modern standard and avoids permission issues.

# Create the virtual environment
python3 -m venv venv-rsa

# Activate it
source venv-rsa/bin/activate          # Linux / macOS
venv-rsa\Scripts\activate              # Windows (PowerShell)
Once activated, your prompt should display (rsa-course) at the start. To leave the environment later, just type deactivate.

Note

You'll need to re-activate the environment each time you open a new terminal.

Installing pycryptodome

pycryptodome is a self-contained Python package providing modern cryptographic primitives, including secure prime generation, RSA key handling, and standard padding schemes. It's the library we'll use everywhere in the course.

With your virtual environment active:

pip install pycryptodome

Don't confuse pycryptodome and pycrypto

The old pycrypto package has been unmaintained since 2013 and contains known vulnerabilities. Always install pycryptodome (a fork that fixes those issues and stays up to date).

Smoke test

Let's verify the install works. Create a file test_install.py:

from Crypto.Util import number
from Crypto.PublicKey import RSA


# Generate a small prime (256 bits — fast, just for the test)
p = number.getPrime(256)
print(f"Random 256-bit prime: {p}")

# Generate a full RSA key
key = RSA.generate(1024)
print(f"RSA modulus N has {key.size_in_bits()} bits")
print(f"Public exponent e = {key.e}")
Run it:
python test_install.py
If you see a large prime and a 1024-bit modulus printed, you're ready.

Optional: OpenSSL

OpenSSL is the command-line workhorse used by virtually every system that touches RSA in production (web servers, SSH, certificate authorities). We'll use it occasionally to inspect real keys and certificates.

Most Linux distributions and macOS ship it by default:

openssl version
On Windows, install Git for Windows (which bundles OpenSSL) or use WSL.

Quick check

Generate a real 2048-bit RSA key and print its parameters:

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out test_key.pem
openssl rsa -in test_key.pem -text -noout
You should see \(N\), \(e\), \(d\), \(p\), \(q\), and the CRT components, exactly the values we'll be computing by hand in the next chapter.

← Previous Next →