01 / 21

RSA & Public-Key
Cryptography

Discover how two different keys enable secure communication: the mathematics of modular exponentiation, prime factoring, and the RSA algorithm.

Discrete Math

What Is Cryptography?

Cryptography is the science of encoding messages so that only authorized parties can read them. Every time you visit a website with HTTPS, send a text message, or make an online payment, cryptography protects your data.

Plaintext
The original, readable message before encryption.
Ciphertext
The scrambled, unreadable output after encryption.
Key
A secret value that controls how encryption and decryption work.

Two Kinds of Keys

Symmetric Key Alice K Bob K Same key Both parties share one secret key. Problem: How do you share it safely? Public-Key Alice Pub Priv Bob Pub Priv Public shared openly Two keys per person. No secret sharing needed!

Why RSA Matters

RSA (Rivest, Shamir, Adleman, 1977) was one of the first practical public-key systems and is still widely used today. Its security rests on a single hard problem:

Easy direction
Multiply two 300-digit primes: takes milliseconds.
Hard direction
Factor a 600-digit product: takes longer than the age of the universe.

Foundation: Modular Arithmetic

RSA relies on modular arithmetic: computing remainders. We write \( a \equiv b \pmod{n} \) to mean \( n \) divides \( (a - b) \).

1
Clock analogy: \( 17 \equiv 5 \pmod{12} \) because 17 and 5 differ by 12.
2
Modular exponentiation: \( 3^5 \pmod{7} = 243 \pmod{7} = 5 \).
3
Key property: We can compute \( a^k \pmod{n} \) efficiently even for huge \( k \) using repeated squaring.

Euler's Totient Function \( \phi(n) \)

\( \phi(n) \) counts the integers from 1 to \( n \) that are coprime to \( n \) (share no common factor except 1). This function is the mathematical engine of RSA.

Prime \( p \)
\( \phi(p) = p - 1 \) because every number from 1 to \( p-1 \) is coprime to a prime.
Product \( n = pq \)
\( \phi(n) = (p-1)(q-1) \). This is the value RSA uses to build keys.

The RSA Algorithm

Step 1: Choose Primes Pick large primes p, q
\( n = p \cdot q \)
Step 2: Compute Totient
\( \phi(n) = (p-1)(q-1) \)
Keep \( \phi(n) \) secret
Step 3: Choose e
\( 1 < e < \phi(n) \)
\( \gcd(e, \phi(n)) = 1 \)
Step 4: Compute d
\( d \cdot e \equiv 1 \pmod{\phi(n)} \)
d = modular inverse of e
Public Key: \( (e, n) \)
Share openly with everyone
Private Key: \( (d, n) \)
Keep secret: never share
Encrypt: \( C = M^e \pmod n \)
Decrypt: \( M = C^d \pmod n \)

Why RSA Works

The correctness of RSA follows from Euler's Theorem. Starting from ciphertext \( C = M^e \pmod{n} \):

1
Decrypt: Compute \( C^d \pmod{n} = (M^e)^d \pmod{n} = M^{ed} \pmod{n} \).
2
Key relationship: Since \( ed \equiv 1 \pmod{\phi(n)} \), we can write \( ed = 1 + k\phi(n) \) for some integer \( k \).
3
Euler's Theorem: \( M^{ed} = M^{1+k\phi(n)} = M \cdot (M^{\phi(n)})^k \equiv M \cdot 1^k = M \pmod{n} \).

Public vs Private Key

Public Key \((e, n)\) Private Key \((d, n)\)
Visibility Shared with everyone Known only to owner
Used for Encrypting messages Decrypting messages
Also used for Verifying signatures Creating signatures
If compromised No harm: it is already public All security is lost
Derivation Choose freely (\(e = 65537\) common) Computed from \( e, \phi(n) \)

How to Apply RSA

1
Pick two distinct primes \( p \) and \( q \). Compute \( n = pq \).
2
Compute the totient: \( \phi(n) = (p-1)(q-1) \).
3
Choose public exponent \( e \): pick \( e \) with \( 1 < e < \phi(n) \) and \( \gcd(e, \phi(n)) = 1 \).
4
Find private exponent \( d \): solve \( de \equiv 1 \pmod{\phi(n)} \) using the Extended Euclidean Algorithm.
5
Encrypt: \( C = M^e \pmod{n} \). Decrypt: \( M = C^d \pmod{n} \).

Example 1: Tiny RSA

Let's build RSA with the smallest primes possible and encrypt the message \( M = 4 \).

1
Choose primes: \( p = 5, \; q = 11 \). So \( n = 5 \times 11 = \) 55.
2
Totient: \( \phi(55) = (5-1)(11-1) = 4 \times 10 = \) 40.
3
Choose \( e = 3 \): \( \gcd(3, 40) = 1 \). Public key: \((3, 55)\).
4
Find \( d \): \( 3d \equiv 1 \pmod{40} \). Since \( 3 \times 27 = 81 = 2 \times 40 + 1 \), \( d = 27 \). Private key: \((27, 55)\).
5
Encrypt \( M = 4 \): \( C = 4^3 \pmod{55} = 64 \pmod{55} = \) 9.

Example 1: Decryption

We received ciphertext \( C = 9 \) and hold private key \( (d, n) = (27, 55) \). Let's recover the original message.

1
Compute: \( M = C^d \pmod{n} = 9^{27} \pmod{55} \).
2
Repeated squaring: \( 9^2 = 81 \equiv 26 \), \( 9^4 \equiv 26^2 = 676 \equiv 16 \), \( 9^8 \equiv 16^2 = 256 \equiv 36 \), \( 9^{16} \equiv 36^2 = 1296 \equiv 31 \pmod{55} \).
3
Combine: \( 27 = 16 + 8 + 2 + 1 \), so \( 9^{27} = 9^{16} \cdot 9^8 \cdot 9^2 \cdot 9^1 \equiv 31 \cdot 36 \cdot 26 \cdot 9 \pmod{55} \).
4
Multiply step by step: \( 31 \cdot 36 = 1116 \equiv 1116 - 20 \times 55 = 16 \), then \( 16 \cdot 26 = 416 \equiv 416 - 7 \times 55 = 31 \), then \( 31 \cdot 9 = 279 \equiv 279 - 5 \times 55 = 4 \).

Example 2: Larger Primes

Now with \( p = 7, \; q = 13 \), encrypt \( M = 10 \).

1
\( n = 7 \times 13 = \) 91, \( \phi(91) = 6 \times 12 = \) 72.
2
Choose \( e = 5 \): \( \gcd(5, 72) = 1 \). Public key: \((5, 91)\).
3
Find \( d \): \( 5d \equiv 1 \pmod{72} \). Since \( 5 \times 29 = 145 = 2 \times 72 + 1 \), \( d = 29 \). Private key: \((29, 91)\).
4
Encrypt: \( C = 10^5 \pmod{91} = 100000 \pmod{91} \). Since \( 100000 = 1098 \times 91 + 82 \), \( C = \) 82.
5
Decrypt: \( 82^{29} \pmod{91} = 10 \). The original message is recovered. ✓

The Trapdoor Function

Plaintext M Message: 4 EASY Modular Exp.
\( C = M^e \pmod n \)
\( 4^3 \pmod{55} = 9 \)
Cipher C = 9 Without d: INFEASIBLE
Must factor \( n \) to find \( \phi(n) \)
With d: EASY
\( M = 9^{27} \pmod{55} = 4 \)

Common Mistakes

✕ Using Small Primes
Primes like 5 and 11 are for learning only. Real RSA uses primes with 300+ digits. Small \( n \) can be factored instantly.
✕ Sharing the Private Key
The private key \( d \) must never be transmitted. Only the public key \( (e, n) \) is shared. Revealing \( d \) breaks all security.
✕ Confusing \( e \) and \( d \)
Students often swap the exponents. Remember: \( e \) encrypts (public), \( d \) decrypts (private). The mnemonic: encrypt, decrypt.

RSA Summary Table

Component Symbol Role
Two large primes \( p, q \) Secret: generate keys, then discard
Modulus \( n = pq \) Part of both keys
Totient \( \phi(n) = (p-1)(q-1) \) Secret: used to compute \( d \)
Public exponent \( e \) Encrypt & verify signatures
Private exponent \( d \) Decrypt & create signatures
Encryption \( C = M^e \pmod n \) Anyone can do this
Decryption \( M = C^d \pmod n \) Only key owner can do this

Question 1 of 5

In RSA, the public key consists of which pair of values?

Question 2 of 5

If \( p = 3 \) and \( q = 11 \), what is \( \phi(n) \)?

Question 3 of 5

RSA's security relies on the difficulty of which mathematical problem?

Question 4 of 5

Given \( n = 55 \), \( e = 3 \), and plaintext \( M = 2 \), what is the ciphertext \( C \)?

Question 5 of 5

What relationship must \( e \) and \( d \) satisfy for RSA to work correctly?