How Does Encryption Work with Prime Numbers? RSA, Trapdoor Functions & HTTPS Explained
Understand exactly how prime numbers protect online banking, HTTPS, and messaging apps—complete with a step-by-step RSA encryption walkthrough using real prime numbers.
The Core Idea: A Mathematical One-Way Trapdoor
Every time you log into a bank, load an https:// website, or verify a software update, your browser relies on a mathematical asymmetry built from prime numbers.
Imagine mixing a can of yellow paint and a can of blue paint together. Mixing them into green takes two seconds. Un-mixing that green paint back into pure yellow and pure blue is practically impossible unless you know a hidden secret.
In cryptography, this is called a Trapdoor One-Way Function:
- Easy direction: Pick two huge prime numbers
pandq, and multiply them:N = p × q. A smartphone does this in less than a millisecond. - Hard direction: Given only
N, figure out whatpandqwere. Even with every supercomputer on Earth working together, factoring a 617-digit number (RSA-2048) would take longer than the age of the universe.
That gap in difficulty is what makes prime-number encryption work.
1. Why Prime Numbers Specifically?
Under the Fundamental Theorem of Arithmetic, every integer greater than 1 is either prime or can be expressed as a unique product of prime factors.
If you chose two composite (non-prime) numbers—say 12 × 15 = 180—the product 180 has many divisor pairs (2×90, 4×45, 6×30, 9×20, 10×18, 12×15). Small divisors are easy to find by trial division.
By contrast, when p and q are both prime, N = p × q is a semiprime: it has only two non-trivial factors in the entire universe of integers. If p and q each have 300+ digits, an attacker cannot rely on small divisors—they face a needle-in-a-cosmic-haystack search.
2. How RSA Encryption Works: Step-by-Step Example with Small Primes
To see the exact math in action, let us walk through a real RSA encryption and decryption cycle using two small prime numbers so you can verify every line by hand.
Step 1: Choose Two Distinct Prime Numbers (p and q)
Let us pick:
p = 61q = 53
(You can verify both are prime on our *Prime Number Checker*.)
Step 2: Compute the Modulus (n)
Multiply them together:
n = p × q = 61 × 53 = 3,233
This number 3,233 becomes part of the Public Key. Everyone in the world is allowed to see n.
Step 3: Compute Euler's Totient φ(n) (Keep Secret!)
Euler's totient counts how many numbers smaller than n share no common factor with n. Because p and q are prime, the formula is simple:
φ(n) = (p - 1) × (q - 1) φ(3,233) = (61 - 1) × (53 - 1) = 60 × 52 = 3,120
Notice that computing `φ(n)` requires knowing `p` and `q`. Without factoring 3,233, an attacker cannot easily compute 3,120.
Step 4: Choose the Public Exponent (e)
Pick an integer e (between 1 and 3,120) that shares no common factors with 3,120. A standard choice in real-world RSA is e = 17 (or 65537 in production).
- Public Key (Lock):
(n = 3,233, e = 17)— shared openly!
Step 5: Compute the Private Key (d)
Find the modular multiplicative inverse d such that:
(d × e) mod φ(n) = 1 (d × 17) mod 3,120 = 1
Using the Extended Euclidean Algorithm, we find:
d = 2,753 (Check: 2,753 × 17 = 46,801 = 15 × 3,120 + 1)
- Private Key (Key):
(n = 3,233, d = 2,753)— kept strictly secret on the owner's device!
Step 6: Encrypt a Message (m = 65)
Suppose someone wants to send you the secret number m = 65 (for example, ASCII 'A'). Using your Public Key (3,233, 17), they compute:
Ciphertext c = m^e mod n c = 65^17 mod 3,233 = 2,790
The intercepted message 2,790 looks like meaningless noise.
Step 7: Decrypt the Message Using the Private Key
You apply your secret Private Key d = 2,753 to the ciphertext 2,790:
Original m = c^d mod n m = 2,790^2,753 mod 3,233 = 65
Because (m^e)^d ≡ m (mod n) by Euler's theorem, the original value `65` snaps right back out!
3. Modular Arithmetic: The "Clock Math" Behind Encryption
Why does mod (modulo) make reversing encryption so hard without d?
Think of a standard 12-hour analog clock. If it is 10:00 and you add 5 hours, you land on 3:00 (15 mod 12 = 3). If someone walks into the room and sees the clock pointing at 3:00, they have no way to tell whether the hand spun around the clock 1 time, 10 times, or 50,000 times.
In RSA, raising m to a massive power modulo n wraps the number around a "clock" of size 3,233 (or 2^2048 in real life) trillions of times. Without the private trapdoor d—which can only be derived from the secret primes p and q—unwinding those wraps is computationally hopeless.
4. Beyond RSA: Prime Numbers in Diffie-Hellman & HTTPS
RSA is not the only place primes protect your data:
- Diffie-Hellman Key Exchange: Uses a single massive prime
pand modular exponentiation (g^a mod p) to let two strangers agree on a shared secret key over a public Wi-Fi network without ever sending the secret itself. - Elliptic Curve Cryptography (ECC): Modern TLS 1.3 and cryptocurrencies use elliptic curves defined over finite prime fields, achieving RSA-3072-level security with much smaller 256-bit keys.
- CSPRNG & Key Generation: When generating SSH keys or HTTPS certificates, your system tests hundreds of random 1,024-bit odd numbers using probabilistic primality tests (such as Miller-Rabin) until two massive primes are found.
5. How Does HTTPS Use Both Prime Numbers and Symmetric Encryption?
In practice, encrypting an entire 4K video stream with RSA prime multiplication would be far too slow. Instead, HTTPS uses a hybrid handshake:
- Asymmetric Phase (Primes / ECC): Your browser verifies the website's digital signature and uses prime/curve math to securely exchange a temporary 256-bit session key.
- Symmetric Phase (AES / ChaCha20): Once both sides hold the same session key, all webpage data is encrypted using high-speed symmetric ciphers and verified with cryptographic hashes like SHA-256.
6. Will Quantum Computers Break Prime Encryption?
In 1994, mathematician Peter Shor discovered Shor's Algorithm, proving that an ideal quantum computer could find the period of modular exponentiation and factor large semiprimes exponentially faster than classical supercomputers.
Does that mean your bank password is exposed today? No. Factoring RSA-2048 reliably requires millions of logical, error-corrected qubits, whereas current hardware has only hundreds to low thousands of noisy physical qubits. Meanwhile, NIST has already finalized Post-Quantum Cryptography (PQC) standards based on lattice problems (ML-KEM and ML-DSA) so the internet transitions safely before large-scale quantum computers arrive.
Explore prime factorization yourself with our **Prime Number Checker, verify data fingerprints with our SHA-256 Hash Generator, or read our companion overview on Prime Numbers in Cryptography**.
Frequently Asked Questions
Published by Use All Tools's engineering and research desk. All calculations, privacy guarantees, and algorithms referenced in this article are open-source and run client-side in the browser.
Recommended Further Reading
Explore how the fundamental building blocks of arithmetic secure global financial transactions, HTTPS TLS connections, and military cryptosystems.
Read GuideAn in-depth cybersecurity breakdown of modern brute-force hash-cracking speeds, GPU clusters, Shannon entropy formulas, and the anatomy of a truly uncrackable password.
Read GuideWhy the cybersecurity industry abandoned forced 30-day password resets: explore NIST's modern guidelines, credential stuffing risks, and how to track rotation cycles.
Read Guide