Path: blob/main/foundations/04-number-theory-rsa/sage/04f-rsa-encryption-decryption.ipynb
483 views
RSA Encryption and Decryption
Module 04 | 04-number-theory-rsa
The climax: everything from Module 04 comes together
Motivating Question: You can tell everyone your public key . Anyone in the world can encrypt a message to you. But only you can decrypt it, using your secret . How is this possible? What mathematical miracle makes one-way encryption a reality?
This notebook is the payoff for everything you have built in Module 04. Every tool you learned --- GCD, extended GCD, Euler's theorem, CRT, primality testing --- was a piece of the RSA puzzle. Now we assemble those pieces into a complete cryptosystem.
Objectives
By the end of this notebook you will be able to:
Perform RSA key generation, encryption, and decryption by hand on small examples.
Prove why RSA decryption recovers the original message (using Euler's theorem).
Identify three concrete attacks on textbook RSA and explain why padding (OAEP) is essential.
Implement RSA-CRT decryption and measure the speedup.
Apply RSA with realistic key sizes in SageMath.
Prerequisites
This notebook ties together all five previous notebooks in Module 04:
| Notebook | What you learned | How RSA uses it |
|---|---|---|
| 04a | GCD, Euclidean algorithm | Choosing : verify |
| 04b | Extended GCD, modular inverse | Computing |
| 04c | Euler's totient, Euler's theorem | Correctness proof: why |
| 04d | CRT, isomorphism | RSA-CRT optimization (4x decryption speedup) |
| 04e | Primality testing, random_prime() | Generating safe primes |
If any of these feel shaky, revisit them now. You will need every single one.
Part 1: RSA Key Generation (Step by Step)
We will walk through key generation with small, hand-checkable numbers. The classic textbook example uses and .
Step 1: Choose two distinct primes and
In practice, we use random_prime() to find primes of 1024+ bits (notebook 04e). For learning, we pick small primes so you can verify every computation by hand.
Step 2: Compute
The modulus is the product of our two primes. This is the number that is public --- everyone knows . The security of RSA rests on the assumption that nobody can factor back into and .
Step 3: Compute
Recall from notebook 04c: Euler's totient counts the integers in that are coprime to . For with distinct primes:
This value is secret --- anyone who knows can compute the private key.
Step 4: Choose coprime to
We need with . This ensures has a modular inverse (notebook 04b: an inverse exists if and only if the GCD is 1). We will use .
In practice, is the standard choice because it is prime and has only two 1-bits in binary, making modular exponentiation fast.
Bridge to 04a: We verify the coprimality condition using the GCD algorithm you learned in notebook 04a.
Step 5: Compute
The private exponent satisfies . We compute it using the extended Euclidean algorithm (notebook 04b).
Bridge to 04b: In that notebook, you learned that
xgcd(a, m)returns with . When , the coefficient is exactly .
The RSA Key Pair
We now have our complete key pair:
| Key | Value | |
|---|---|---|
| Public key | ||
| Private key |
The public key is shared with the world. The private key (and the factors , and ) must be kept secret.
Checkpoint: Before running the next cell, compute by hand: what is ? That is, what is ? (Hint: it had better be 1.)
Part 2: Textbook RSA Encryption
Encryption is remarkably simple. Given a message (an integer with ) and the public key :
That is it. One modular exponentiation.
Checkpoint: Before running the next cell, try to predict: what is ? This is hard to compute by hand, but you can use repeated squaring. Or just make a guess and see if you are right.
Part 3: Textbook RSA Decryption
Decryption is the exact same operation, but using the private exponent :
Let us verify that we recover the original message.
Part 4: Why RSA Works (The Proof)
This is the deepest part of the notebook. We need to show that decryption undoes encryption:
Why does ?
Since , we can write for some integer .
Bridge to 04c: By Euler's theorem (notebook 04c), if then .
So:
That is the whole proof. Every tool serves a purpose:
Euler's theorem (04c) makes , so the part vanishes.
Extended GCD (04b) lets us find such that .
GCD (04a) guarantees the inverse exists (because ).
Technical note: The proof above requires . What if shares a factor with ? Since , this means or , which happens with negligible probability for random messages. The proof can be extended to cover this case using CRT and Fermat's little theorem on each prime factor separately.
Part 5: Why Textbook RSA Is INSECURE
Misconception Callout: "RSA is secure because factoring is hard."
More precisely: RSA is secure if factoring is hard and you use proper padding. Textbook RSA (no padding) is broken even if factoring is hard. Here are three concrete attacks.
Attack 1: Deterministic Encryption (No Semantic Security)
Textbook RSA is deterministic: encrypting the same message twice produces the same ciphertext. This means an attacker who guesses your message can verify the guess.
Attack 2: Malleability (Homomorphic Property)
Textbook RSA is multiplicatively homomorphic: given and , anyone can compute without knowing or .
An attacker can manipulate ciphertexts in meaningful ways without the private key.
Attack 3: Small Message Attack
If the message is small enough that (no modular reduction happens), then the attacker can simply compute the -th root of over the integers.
With (common in some implementations), any is vulnerable.
Why Padding Matters: OAEP
All three attacks above are defeated by OAEP (Optimal Asymmetric Encryption Padding, Bellare and Rogaway, 1994). Before encrypting message , OAEP:
Adds randomness: Each encryption of the same message produces a different ciphertext (defeats Attack 1).
Adds structure: The padded message has algebraic structure that is destroyed by multiplication (defeats Attack 2).
Fills the message space: The padded message is always close to in size (defeats Attack 3).
Rule of practice: Never use textbook RSA. Always use RSA-OAEP (PKCS#1 v2).
Misconception Callout: "I will just add some random bytes myself." Ad-hoc padding schemes have been broken repeatedly. OAEP has a security proof in the random oracle model. Use the standard.
Part 6: RSA-CRT Optimization
Standard RSA decryption computes . Since and are both large (e.g., 2048 bits), this is expensive.
Bridge to 04d: The Chinese Remainder Theorem (notebook 04d) tells us that when . We can decrypt modulo and modulo separately, then combine the results with CRT.
RSA-CRT decryption:
Precompute and
Compute and
Combine:
Why is this faster? Exponentiating a number modulo a -bit number costs roughly using schoolbook arithmetic. Two exponentiations modulo -bit numbers cost . That is a 4x speedup.
Part 7: RSA with Realistic Key Sizes
Our toy example used 6-bit primes. In practice, RSA uses 1024-bit primes (for a 2048-bit modulus) or larger. Let us see the full process at scale.
Bridge to 04e: We use
random_prime()(notebook 04e) to generate cryptographic-strength primes.
Exercises
Exercise 1: Full RSA Round-Trip (Worked Example)
Let us do a complete RSA example with different parameters: , , , and message .
Exercise 2: RSA-CRT Decryption (Guided)
Using the key from Exercise 1 (, , , ciphertext from above), perform RSA-CRT decryption. Fill in the TODOs.
Exercise 3: Build Your Own RSA (Independent)
Build a complete RSA system from scratch. Choose your own primes (at least 10 digits each), generate a key pair, encrypt and decrypt a message of your choice, and verify the malleability attack. No starter code is provided.
Summary
| Concept | Key idea |
|---|---|
| RSA key generation | Uses GCD (04a) to choose , extended GCD (04b) to compute , and primality testing (04e) to generate and |
| Encryption and decryption | Encryption: . Decryption: . One modular exponentiation each way |
| Correctness proof | Follows from Euler's theorem (04c): |
| Textbook RSA is insecure | Deterministic (no semantic security), multiplicatively malleable, and vulnerable to small-message attacks. Always use RSA-OAEP |
| RSA-CRT optimization | Uses CRT (04d) to decrypt roughly 4x faster by working modulo and separately |
| Module 04 assembly | Every prior notebook (04a through 04e) contributes a specific piece to the RSA cryptosystem |
Crypto Foreshadowing: RSA is being phased out in favor of elliptic curve cryptography (Module 06) and lattice-based cryptography (Module 08). ECC offers the same security with much smaller keys (256-bit ECC 3072-bit RSA). And lattice-based systems are believed to resist quantum computers, which would break RSA entirely via Shor's algorithm. But understanding RSA is essential: it teaches you the paradigm of public-key cryptography that all these newer systems build upon.
Next: Module 05 --- Discrete Logarithm Problem and Diffie-Hellman