Memra

RSA & public-key cryptography

◈ 6 cards

Key generation from two primes, encryption/decryption by modular exponentiation, why correctness follows from Euler/Fermat, and a runnable toy RSA round-trip and signature.

Public-key cryptography: the asymmetry

In a public-key system each party has two keys: a public key anyone may use, and a secret key kept private. They are inverse transformations — and for every message — but knowing does not reveal . RSA realizes this with modular exponentiation: the public and secret operations are and , where and are exponents that undo each other modulo .

The number theory behind it

RSA depends on Euler's phi function — the count of integers in that are relatively prime to — and on Euler's theorem:

When is a product of two distinct primes, . Fermat's little theorem is the special case for prime — it patches the correctness proof when happens to share a factor with .

Key generation

  1. Choose two large random primes (1024 bits each in practice).
  2. Compute the modulus and .
  3. Pick a small odd with (often ).
  4. Compute with EXTENDED-EUCLID (lesson 8.1).
  5. Publish ; keep secret. Discard , , .

Encryption of a message (an integer with ): . Decryption: . Both are single modular exponentiations — the algorithm from 8.1.

Why decryption inverts encryption

By construction , so for some integer . Then

using Euler's theorem (and Fermat + the Chinese Remainder Theorem to cover the rare case). The round-trip is exact, not approximate.

Worked example — the classic small instance

Take , . Then and . Choose (it is coprime to ); EXTENDED-EUCLID gives . To send :

  • Encrypt: .
  • Decrypt: . ✓

Signatures, and why it is secure

Running the keys in the other order gives a digital signature: the owner signs with the secret key, , and anyone verifies with the public key by checking . Only the secret-key holder could have produced .

Security rests on the hardness of factoring: recovering requires , which requires the factorization . No polynomial-time factoring algorithm is known — the best (general number field sieve) is sub-exponential — so for 2048-bit this is infeasible. (Note: finding the primes in step 1 is easy — primality testing, e.g. Miller–Rabin, is fast; it is un-multiplying that is hard. That gap is the whole game.)

multiply(p−1)(q−1)chooseinvertp = 61, q = 53distinct primesn = 3233n = p·qφ(n) = 312060 · 52e = 17gcd(e, φ) = 1d = 2753e⁻¹ mod φ(n)
Key generation on p = 61, q = 53. Only the last step needs an algorithm — d is e⁻¹ mod φ(n) from EXTENDED-EUCLID (lesson 8.1).
e = 17d = 2753M = 65plaintext, M < nC = 279065^17 mod 3233M = 652790^2753 mod 3233
Encryption and decryption are the same primitive — one modular exponentiation each — run with the two exponents that undo one another mod n.
valuestatuswhyn = 3233publishedthe modulus, in both keyse = 17publishedencryption exponentd = 2753secretdecrypts and signsp = 61, q = 53destroyedthey give φ(n)φ(n) = 3120destroyedit gives d from eFactoring 3233 back into 61 · 53 collapses the whole chain — that is the one hard step.
What the world sees, and what it must not. Everything in the bottom two rows can be recovered only by factoring n.
NORMAL ~/memra/learn/comp-372/rsa-public-key-cryptography utf-8 LF