How RSA Encryption Works: The Math Behind Public-Key Cryptography

Your browser just encrypted this page for you without ever meeting the server in person. Here's the number theory trick, multiplying primes is easy, undoing it is brutally hard, that makes it possible.

By Petrus Sheya

August 3, 2026 · 8 min read

You want to buy something online. Your card number needs to travel across a network that anyone could be listening to, to a server you've never met and share no secret code with.

How do you lock a message so that only one specific person can open it, when you've never exchanged a key with them and every step of your conversation is public?

The answer is sitting behind the lock icon in your browser right now. It's called RSA, and it works because of one strange fact about numbers: multiplying two big primes together is fast. Taking that answer apart again is, for all practical purposes, impossible.


A padlock anyone can close, but only one person can open

Picture an open padlock sitting on a table in public. Anyone can pick it up and snap it shut around a box. That takes no skill and no secret.

But only one person, the one holding the actual metal key, can open it again.

That's the entire idea behind public-key cryptography. The open padlock is your public key. Anyone can grab it and lock a message for you. The metal key is your private key. Only you have it, and only it can undo what the padlock did.

RSA is one specific, very clever way to build a padlock and key pair out of pure arithmetic. And hey, notice what that requires: an action that's trivial to do in one direction, and something close to impossible to undo without the key. Multiplication turns out to be exactly that action.


Multiplying is easy. Undoing it is brutally hard

Take two prime numbers, say 61 and 53. Multiply them: 3233. That took one step.

Now suppose someone hands you just the number 3233 and asks: "which two primes multiply together to make this?" You'd have to start guessing divisors: 2, 3, 5, 7, 11... and check each one. Eventually you'd land on 53. For a four-digit number that's a minor annoyance.

But watch what happens as the number of digits grows.

Multiplying p and q takes one step, always. Watch how many steps it takes to undo it, as the numbers get bigger.

n = p × q = 53 × 89 = 4717multiply: 1 stepfactor: trying divisor 2...searching up to sqrt(n) ≈ 68...
Multiply steps1
Factor steps...
Ratio...

Slide it up a few notches. The multiply step stays exactly as fast, always one step, no matter how big p and q get. But the factoring bar creeps, then crawls, then barely moves at all. The number of divisors you need to check grows roughly with the square root of n, which means it grows exponentially with the number of digits.

Real RSA keys use primes with over 300 digits each. Multiplying them together still takes a computer a fraction of a millisecond. Factoring the result back apart, with every computer on Earth working together using the best known algorithms, would take far longer than the universe has existed.

That gap, easy one way, impossible the other, is the entire security of RSA. It's called a one-way function, and it's the raw material everything else is built from.


Turning two primes into a padlock and a key

So we have a one-way function. Now we need to turn it into an actual lock and key. Here's the recipe.

Pick two prime numbers, pp and qq. Multiply them to get n=p×qn = p \times q. This nn becomes part of your public padlock, the part anyone can see.

Next, compute φ(n)=(p1)(q1)\varphi(n) = (p-1)(q-1). This counts something specific: how many numbers less than nn share no common factor with it. You never publish this number. It only exists to help build the key, and it depends on knowing pp and qq separately, which is exactly the information factoring is supposed to protect.

Now pick a small number ee that shares no common factor with φ(n)\varphi(n). This becomes your public exponent. The pair (n,e)(n, e) together is your public key, your open padlock.

Finally, find the number dd that satisfies:

e×d1(modφ(n))e \times d \equiv 1 \pmod{\varphi(n)}

In words: multiply ee by dd, divide by φ(n)\varphi(n), and the remainder is 1. This dd is computed using the extended Euclidean algorithm, a fast, direct method, not a search. And dd is your private key. It only comes out of the arithmetic if you know φ(n)\varphi(n), which only comes out of the arithmetic if you know pp and qq.

Pick a prime pair and watch the entire key, public and private, fall out of two numbers.

p11primeq13primen = p×q143public modulusφ(n) = (p-1)(q-1)120kept secrete7public exponentd = e⁻¹ mod φ(n)103private exponentpublic key = (n, e) private key = (n, d)
n143
e × d mod φ(n)1

Try a few different prime pairs. Every single number in that chain, nn, φ(n)\varphi(n), ee, dd, falls out of just two starting primes. Whoever generated the key is the only one who ever saw pp and qq directly. Everyone else only sees nn, and cracking nn back into pp and qq is the hard problem from the last section.


What "encrypt" actually computes

We have a public key (n,e)(n, e) and a private key (n,d)(n, d). But what does locking a message actually look like as arithmetic?

Take your message, turned into a number mm smaller than nn. Encryption computes:

c=memodnc = m^e \bmod n

That "mod n" part means: do the exponentiation, then keep only the remainder after dividing by nn. Picture a clock face with nn positions instead of 12. Every time your running total would go past nn, it wraps back around to zero and keeps going.

Computers don't multiply mm by itself ee times in a row, that would be slow for large ee. Instead they use a trick called repeated squaring: square the current value, and if the current bit of the exponent is 1, multiply in one more copy of mm. Repeat once per bit of ee. A handful of squarings gets you memodnm^e \bmod n in a tiny fraction of the steps a naive approach would need.

Encrypting is repeated squaring mod n. Each bit of the exponent either just squares the current value, or squares and multiplies it. Press play to watch the point walk around the circle.

3 squaring steps for e = 71mod 143
Step0 / 3
Current value1
m^e mod n6

Change ee and press play. Watch the dot walk around the circle, one squaring at a time, sometimes with an extra multiply thrown in when a bit is set. That walk, landing on a final point, is the entire encryption step.

Decryption is the same operation with the private exponent: m=cdmodnm = c^d \bmod n. It works and lands you back on the original message because of a fact from number theory called Euler's theorem, which guarantees that raising mm to the power e×de \times d and reducing mod nn gets you back to mm, precisely because ee and dd were built to satisfy e×d1(modφ(n))e \times d \equiv 1 \pmod{\varphi(n)}.


Locking and unlocking a real message

Let's run the whole thing end to end with small, real numbers: p=11p = 11, q=13q = 13, so n=143n = 143 and φ(n)=120\varphi(n) = 120. That gives public exponent e=7e = 7 and private exponent d=103d = 103.

Send a message through the public key, then unlock it with the private key. An eavesdropper only has (n, e, c) and has to factor n to follow.

m = 20encrypt (n,e)m^e mod nc = 136decrypt (n,d)c^d mod nm' = 20Attacker sees: n = 143, e = 7, c = 136To get d, they must factor n back into p × q first.click "Try to break it" to simulate the attacker
Ciphertext c136
Decrypted m'20
Round tripMATCH

Slide mm around. Every value takes the same round trip: lock it with (n,e)(n, e), get back a scrambled cc, unlock it with (n,d)(n, d), and land exactly back on mm. The two operations are inverses of each other by construction.

Now click "try to break it." An eavesdropper watching the wire sees nn, ee, and cc. Not dd. To read your message the way you can, they'd need dd, and to get dd they need φ(n)\varphi(n), and to get φ(n)\varphi(n) they need pp and qq separately. Their only path in is factoring nn back apart.

For n=143n = 143 that takes a handful of tries. For a real key with a 300-digit nn, that same path is the wall from the second section: technically possible, practically never.


Why 2048 bits is the number you keep seeing

You'll see RSA described as "2048-bit" or "4096-bit." That's just the size of nn in binary digits. Bigger nn means bigger primes, which means a bigger gap between how fast multiplying is and how slow factoring is.

2048 bits was chosen because it sits comfortably past the point where factoring becomes infeasible with current classical computers and known algorithms, while still being fast enough to compute with billions of times a day across the internet. It's a moving target: as computers get faster, the safe minimum size creeps up, which is why 1024-bit keys, standard twenty years ago, are considered too weak today.

There's one asterisk worth knowing about. A quantum computer running Shor's algorithm could factor large numbers efficiently, which would break RSA's one-way function entirely. Large enough quantum computers don't exist yet, but it's the reason cryptographers are already rolling out post-quantum replacements. The math in this post isn't going anywhere. The specific hardness assumption it leans on might, eventually, need a successor.


The short version

RSA works because multiplying two large primes is fast, and factoring the result back apart is, for large enough primes, beyond the reach of any computer we know how to build. That asymmetry lets you publish a "lock" (the pair nn and ee) that anyone can use to scramble a message to you, while only you hold the "key" (the exponent dd) built from the two primes nobody else ever saw.

Every encrypted page you've ever loaded ran exactly this arithmetic, just with primes hundreds of digits longer than the toy examples here. Same padlock. Same key. Just numbers too big to crack.


All visualizations are interactive React components running entirely in your browser. The factoring race and the attacker simulation use real trial division on the actual numbers shown, not simulated timings. No libraries beyond React.