A Clock Is Already Doing This Math
Look at a clock face. It has 12 numbers on it, and when the hand passes 12 it does not keep climbing to 13, 14, 15. It starts over at 1. That is the whole idea you are here to learn, and you already knew it before you knew it had a name.
Try it. It is 9 o'clock, and you want to know what time it will be in 8 hours. In normal arithmetic, 9 plus 8 is 17. But a clock does not have a 17. The hand goes around past 12 and lands on 5.
Math has a word for the number you wrap at. It is called the modulus. A clock face is arithmetic with a modulus of 12. And the operation that does the wrapping is called mod, which is short for modulo.
Here is the definition, and it is shorter than you might expect. a mod n is the remainder you get when you divide a by n. Twelve fits into seventeen one time, and 5 is left over. That leftover 5 is the answer.
Wrap-around counting is everywhere once you start noticing it:
- Days of the week wrap at 7. Today is Wednesday, so in 10 days it is Saturday, because 10 mod 7 is 3, and 3 days past Wednesday is Saturday.
- Minutes and seconds wrap at 60. Your stopwatch never shows 63 seconds.
- Compass directions wrap at 360 degrees. Spin 400 degrees and you have really only turned 40.
- Even and odd is just mod 2. A number is even when it is 0 mod 2 and odd when it is 1 mod 2.
A real clock face is labeled 1 through 12, but math likes to count 0 through 11, so the top of the circle is 0 instead of 12. It is the same circle with a different sticker on one spot. Everything on this page uses the math version, where the positions on a mod 12 clock are 0, 1, 2, all the way up to 11.
Doing mod by Hand
You only need division with remainders, the kind you learned years ago. To compute a mod n:
- Ask how many whole times n fits inside a.
- Multiply n by that many times.
- Subtract. Whatever is left over is your answer.
Say you want 30 mod 7. Seven fits into thirty four times, because 7 times 4 is 28. Subtract: 30 minus 28 leaves 2. So 30 mod 7 = 2.
Now notice something that turns out to be the most important fact on this whole page. The answer to a mod n is always somewhere between 0 and n minus 1. Never bigger. Never negative. If you divide by 7, the leftover can only be 0, 1, 2, 3, 4, 5, or 6, because if the leftover were 7 or more you could have fit another whole 7 in.
That means mod is a machine that takes any number, no matter how enormous, and squeezes it into a small, fixed set of answers. Hold onto that. It is why encryption uses it.
In code, almost every programming language writes mod with the percent sign:
30 % 7 → 2
100 % 10 → 0
5 % 12 → 5 (12 does not fit into 5 at all, so all of it is left over)
That last line trips people up at first. If the number is already smaller than the modulus, nothing wraps, and the answer is just the number itself.
What is 45 mod 7?
Seven fits into forty-five six times, because 7 times 6 is 42. Then 45 minus 42 leaves 3. Notice that 6 is the count of how many times 7 fit, not the remainder, which is the most common mistake here. The answer to anything mod 7 has to land between 0 and 6.
Try It: The Modular Clock
Reading about wrap-around is fine. Watching it wrap is better. The clock below lets you choose how many positions it has (that is your modulus), where the hand starts, and how far to move it. The math updates as you go.
Some things worth trying: set the modulus to 12 and add 24, then notice you end up exactly where you started. Set the modulus to 2 and add numbers one at a time, and watch it flip between even and odd. Press Walk the steps to see the hand actually travel around the circle instead of jumping.
The number of full laps is the part people forget. When you compute 17 mod 12 and get 5, the answer does not tell you that you went around once. That lost information looks like a bug, but in section 7 you will see that it is exactly what makes encryption hard to crack.
Why Wrap-Around Math Is Perfect for Encryption
Encryption means turning a message into something unreadable, in a way that can be undone by whoever holds the right key. Under the hood, computers do that with arithmetic. Letters become numbers, the numbers get scrambled by a calculation, and the scrambled numbers become the coded message.
The instant you decide to scramble with arithmetic, you run into a problem: numbers grow. Multiply a few times and you have a monster. Raise something to a power and it explodes. A calculation that starts with a value under 100 can produce a result with hundreds of digits, and now you cannot store it, send it, or work with it easily.
Mod fixes that in one move. No matter how big the calculation gets, taking mod n at the end drops the answer back into the range 0 to n minus 1. Modular arithmetic is a fence around your numbers, and the numbers can never get out.
Three things make it the right tool:
- It keeps everything bounded. Working mod 26 means every result is one of 26 letters. Working mod a giant prime means every result fits in a fixed amount of memory, no matter how many steps the calculation takes.
- It is reversible if you know the key. Wrapping forward by a certain amount can be undone by wrapping backward by that same amount. Encryption is worthless if the real recipient cannot get the message back.
- It mixes things up. Two numbers that were far apart can land right next to each other after a mod, and two that were neighbors can end up on opposite sides of the circle. Predictable patterns in the original message get flattened out.
Picture a runner on a circular track instead of a straight road. On a straight road, running longer means getting farther away forever. On a circular track, you can run for a hundred miles and still be standing somewhere on the same small loop. Mod turns the number line into a track.
A Cipher Built on mod 26
Time to actually encrypt something. You may have seen a shift cipher before, where you slide every letter down the alphabet by a fixed amount. People usually explain it by counting on their fingers. Now you get to see it as math, which is how a computer sees it.
Step 1: turn letters into numbers
Give each letter a number, starting from zero. A is 0, B is 1, C is 2, and so on up to Z, which is 25. There are 26 letters, so the numbers run 0 through 25.
The alphabet with its numbers. Notice that Z is 25 and not 26, because we started counting at zero.
Step 2: add the key, then take mod 26
The key is just how far you shift. Pick 7, and the rule for encrypting one letter is:
Encrypt the word HELLO with a key of 7, letter by letter:
None of those letters had to wrap, so let us try one that does. Take Y, which is 24. Add the key: 24 plus 7 is 31. But there is no letter number 31, and that is precisely the problem mod solves. Since 26 fits into 31 one time with 5 left over, 31 mod 26 = 5, and letter number 5 is F. Y becomes F, sliding right off the end of the alphabet and reappearing at the front.
Step 3: undo it
Decrypting runs the same idea backward. Subtract the key instead of adding it, then take mod 26 again:
Check it on F, which is 5. Take 5 minus 7 and you get negative 2, so add 26 to reach 24, and 24 mod 26 is still 24. Letter 24 is Y, the letter you started with. The wrap undid itself perfectly.
Type your own message below and watch each letter get pushed around the 26-letter circle. Drag the key and the whole message re-encrypts instantly.
Letter by letter
A shift cipher has only 26 possible keys, and one of them does nothing at all. A person with a pencil can try every single one in a few minutes, and a computer does it faster than you can blink. This is a great way to see the math, not a way to protect anything real. Real systems keep the exact same wrap-around idea and swap in numbers so large that trying them all is impossible.
Modular Exponentiation: The Real Engine
Adding is easy to undo, which is why shift ciphers are weak. Modern encryption reaches for an operation that is much lumpier: raising a number to a power and then taking mod. It has a long name, modular exponentiation, and a simple shape.
Quick reminder on exponents: 3 to the 4th power means 3 times 3 times 3 times 3, which is 81. Now take mod 7 of that.
You might be thinking that 81 was fine, but real systems use exponents in the hundreds, and those numbers would have more digits than there are atoms in the sky. Here is the trick that saves everything: you can take the mod as you go. You do not have to compute the giant number first. Multiply, wrap, multiply, wrap.
3 x 3 = 9, and 9 mod 7 = 2
2 x 3 = 6, and 6 mod 7 = 6
6 x 3 = 18, and 18 mod 7 = 4
answer: 4, the same as 81 mod 7
Nothing in that column ever got above 18. That is why a computer can raise a number to a 2,048-bit power and still finish in a fraction of a second. The fence keeps the numbers small at every single step.
Two people agree on a secret, out loud
Here is where it gets genuinely surprising. Two people who have never met can shout numbers at each other across a crowded room, and end up sharing a secret that nobody else in the room knows. That is called Diffie-Hellman key exchange, and it is modular exponentiation doing the work.
Call them Ana and Ben. First they agree on two public numbers, and they do not care who hears: a base of 3, and a modulus of 17. Real systems use huge numbers here, but the steps are identical.
Ana
Secret number: 4 (never told to anyone)
Sends 34 mod 17 = 81 mod 17 = 13
Gets Ben's 5, computes 54 mod 17 = 625 mod 17 = 13
Ben
Secret number: 5 (never told to anyone)
Sends 35 mod 17 = 243 mod 17 = 5
Gets Ana's 13, computes 135 mod 17 = 13
Both of them end up holding 13, and they never once said 13 out loud. Anyone listening heard the base 3, the modulus 17, and the two public results 13 and 5. None of that is enough to work out the shared answer without knowing one of the secret exponents.
It works because of a rule about exponents you may already know: raising to the 4th and then the 5th is the same as raising to the 20th, in either order. Ana and Ben take different paths to the same number, and the eavesdropper is not on either path.
RSA, the other famous system, uses the same engine in a different arrangement. To encrypt a message m, you compute m raised to a public exponent, mod a very large number n. Only someone who knows how n breaks apart into its two prime factors can compute the exponent that undoes it. Different setup, same beating heart: exponent, then mod.
Easy One Way, Brutal the Other
So why can an eavesdropper not just work backward? They know the base, the modulus, and the result. What stops them?
Look at the two questions side by side. They look like the same question wearing different hats, but they are wildly different amounts of work.
I know the exponent is 5. What is 35 mod 17?
Multiply and wrap five times. A calculator does it instantly, and you could do it on paper in under a minute. The answer is 5.
I know the answer is 5. What exponent got me there?
There is no shortcut. You try 1, then 2, then 3, and keep going until something matches. With a modulus of 17 that is quick. With a 600-digit modulus, it is not.
This is called a one-way function: something simple to compute forward and painfully slow to compute backward. Cryptographers hunt for problems like this, because a one-way function is what lets you publish something in the open without giving away the secret behind it.
The reason mod makes it one-way goes right back to the clock. When you take a mod, you throw information away. If someone hands you the answer 5 on a mod 12 clock, the starting number could have been 5, or 17, or 29, or 41, or any of infinitely many others. The remainder does not remember how many laps you ran. Adding exponents on top scatters the results so there is no pattern to follow either, so guessing your way backward means checking possibilities one at a time.
And "one at a time" is the whole defense. The numbers used in real encryption are so large that a computer checking billions of possibilities every second would still be working long after the sun burns out. The math is not unbreakable in theory. It is just far too slow to break in practice, and that turns out to be good enough.
Because forward is easy and backward is hard, you can hand out a public key that anyone may use to lock a message to you, while keeping the private key that unlocks it entirely to yourself. That is a strange and powerful idea: a lock you can mail to strangers, with no risk that they can figure out the key from the lock.
Why can the answer to "some number mod 26" never be 30?
A remainder is what is left after you pull out every whole copy of the modulus you can. If 30 were left over, you could pull out one more 26 and be left with 4, so 30 was never the remainder. That bound, always 0 to n minus 1, is exactly the fence that keeps encryption math from running away.
The Padlock in Your Browser
Open any website and look at the address bar. If it starts with https, you just used everything on this page without noticing.
The s stands for secure, and it means your device and that website's server ran a quick conversation before a single word of the page came through. That conversation is called a handshake, and it goes roughly like this:
- Your browser and the server agree, in the open, on which math to use and which public numbers to start from.
- Each side picks a secret number and never sends it anywhere.
- Each side does a modular exponentiation and sends only the result.
- Both sides combine what they received with their own secret and land on the exact same shared key, the way Ana and Ben did.
- Everything after that, every message, password, and picture, is scrambled with that shared key.
All of it finishes in a fraction of a second, and it happens again for every site you open, every app that syncs, and every message you send. Anyone watching the traffic in between, on the coffee shop wifi or anywhere along the route, sees only scrambled numbers.
So the next time you see that padlock, you know what is behind it. Letters became numbers, the numbers ran laps around a very large circle, and the only thing that made it home was the remainder.
Mod is division's leftovers. It fences numbers into a fixed range no matter how big the math gets. It is easy to run forward and miserable to run backward, because the remainder forgets how many laps you ran. Those three sentences are the foundation under nearly every secure thing you touch online.