0Pricing
Cryptology Academy · Lesson

RSA Key Generation Step by Step

Generate p, q, n, e, d and understand each parameter's role.

RSA Key Generation Step by Step is a free Cryptology Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

In this lesson we generate an RSA key pair from scratch, following every mathematical step from prime selection to key parameters.

Step 1: Choose Two Large Primes

Choose large random primes p and q. In production: p,q ≥ 2^1023 (for 2048-bit RSA). For demonstration: p=61, q=53.

Step 2: Compute the Modulus

n = p × q = 61 × 53 = 3233. The modulus n is the public component. RSA-2048 uses n ≈ 2^2048. Factoring n is computationally infeasible for large values.

Step 3: Compute Totient

φ(n) = (p-1)(q-1) = 60 × 52 = 3120. This is kept secret after key generation — together with the factorization, it allows computing the private key.

Step 4: Choose Public Exponent e

Choose e such that 1 < e < φ(n) and GCD(e, φ(n)) = 1. Standard choice: e = 65537 (0x10001). It is prime, has low Hamming weight (fast encryption), and is widely supported.

Step 5: Compute Private Exponent d

d = e^(-1) mod φ(n): find d such that e×d ≡ 1 (mod 3120). For e=17, φ=3120: d=2753 (verify: 17×2753 = 46801 = 15×3120 + 1 ✓). Python: d = pow(e, -1, phi_n)

The Key Pair

Public key: (e=17, n=3233) — shareable Private key: (d=2753, n=3233) — secret Share (e, n) freely. Protect d with your life. Never reveal p, q, or φ(n) after key generation.

Key Sizes in Practice

RSA-1024: obsolete since 2010. RSA-2048: minimum recommended. RSA-4096: high security, 4× slower. Most TLS certificates use RSA-2048 or ECDSA-256 (equivalent security, much faster).

CRT Optimization

Chinese Remainder Theorem (CRT) speeds up RSA decryption by ~4×. Store private key as (p, q, dp, dq, qInv) instead of just d. Compute mod p and mod q separately, then combine.

Key Generation in Python

from cryptography.hazmat.primitives.asymmetric import rsa private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048 ) public_key = private_key.public_key()

Serializing Keys

from cryptography.hazmat.primitives import serialization pem = private_key.private_bytes( serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.BestAvailableEncryption(b'passphrase') )

Quick Check

In RSA key generation with p=7, q=11: what is the correct value of n?

Recap

You can now generate RSA keys from scratch. Next we apply these keys to encrypt and decrypt messages using modular exponentiation.

Frequently asked questions

Is the “RSA Key Generation Step by Step” lesson free?

Yes — the full text of “RSA Key Generation Step by Step” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.

What will I learn in “RSA Key Generation Step by Step”?

Generate p, q, n, e, d and understand each parameter's role. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “RSA Key Generation Step by Step” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Public-Key Cryptography Concepts
  2. RSA Key Generation Step by Step
  3. RSA Encryption & Decryption Math
  4. RSA Real-World Use Cases & Key Sizes
← Back to Cryptology Academy