8.4 Building a Small RSA System
Section 8.3 built three tools:
- modular exponentiation performs arithmetic inside a finite residue system;
- the extended Euclidean algorithm finds modular inverses;
- the Chinese Remainder Theorem relates computations modulo different coprime factors.
RSA assembles these tools into a public-key cryptosystem.
This section uses tiny numbers so every step remains visible. Such keys are educational only and provide no real security.
The public-key idea
In a traditional shared-key system, the sender and receiver must already share one secret key.
A public-key system instead uses a related pair:
- a public key may be distributed to anyone and is used here for encryption;
- a private key is kept secret and is used here for decryption.
Knowing the public key should not make the private operation practical to reconstruct when real parameters are large.
RSA represents a message block by an integer in the range
where is part of the public key.
Euler’s totient counts invertible residues
Before generating keys, we need one new function.
For a positive integer , Euler's totient function counts the integers in
that are coprime to .
For example, the numbers below that are coprime to are
so
If is prime, every integer from through is coprime to . Therefore
If and are distinct primes, then
One way to see this is to count numbers from through and exclude multiples of or . Inclusion–exclusion gives
Euler’s theorem explains the exponent cycle
Euler's theorem states that if , then
This means that powers of a residue coprime to return to after an exponent of .
RSA chooses two exponents whose product is congruent to modulo . Euler's theorem then makes one exponent undo the other.
RSA key generation, one step at a time
Step 1: choose two distinct primes
Choose
Real RSA uses much larger randomly generated primes.
Step 2: form the modulus
Multiply the primes:
The modulus belongs to both keys.
Step 3: compute the totient
Because and are distinct primes,
Step 4: choose the public exponent
Choose an integer satisfying
and
Choose
It is valid because .
Step 5: compute the private exponent
Choose as the modular inverse of modulo :
Here
so
The resulting keys are
and
The primes and totient must also remain secret in a real system because they make easy to reconstruct.
Build a toy RSA key machine from prime cartridges. The workshop rejects repeated or composite inputs, maps every possible public exponent against the totient, and runs the extended Euclidean gears to derive a private exponent only when the inverse certificate is valid.
Encrypting a message block
With public key , encrypt a message integer by computing
where is the ciphertext.
Let
It is a valid block because . Encrypt using :
So the transmitted ciphertext is
Decrypting with the private exponent
Decrypt by computing
For the toy key,
Directly evaluating creates an unnecessarily large integer. Instead, use repeated squaring.
Compute powers whose exponents double:
Because
combine the corresponding rows:
Thus
which recovers the original message.
Why the exponents undo each other
The key equation
means that some integer satisfies
For a message coprime to , Euler's theorem gives
Encryption followed by decryption raises to exponent , so the original residue returns.
This proof assumed . RSA also works for all residues modulo a product of distinct primes; a complete proof checks the congruence separately modulo and modulo , then uses the Chinese Remainder Theorem. The coprime case above contains the core exponent-cycle idea.
Repeated squaring is an algorithm
An exponent can be written as a sum of powers of . Repeated squaring computes only those powers:
and reduces modulo after every multiplication.
This requires only squaring and multiplication stages for exponent , instead of multiplying by itself times. It connects modular arithmetic back to the logarithmic growth analysis of Chapter 5.
Route a numerical message through a public encryption booth and a private decryption booth. A live exponent ladder exposes each square-and-multiply residue, while tampering with the ciphertext or swapping the private exponent lets learners observe exactly when recovery fails.
Why the classroom system is not secure
Our modulus is immediately factored as . Anyone who factors can compute and reconstruct from public exponent .
Real RSA security depends on carefully generated large primes and mature cryptographic implementations. Raw textbook RSA is deterministic and vulnerable to multiple attacks. Secure applications use standardized randomized padding, such as RSA-OAEP for encryption, and vetted libraries.
Never design a production cryptosystem by copying the toy arithmetic in this section. Its purpose is to reveal the mathematical mechanism, not to specify secure software.
Chapter bridge
Chapter 8 developed arithmetic structure for integers: divisibility led to unique prime factorizations, Euclid's algorithm produced gcd and Bézout coefficients, congruence created finite residue systems, CRT aligned several systems, and RSA combined them into a public-key mechanism.
Chapter 9 introduces discrete probability. Counting from Chapter 6 will define probabilities, while modular and cryptographic examples will motivate random choices and uncertainty.