Codes & Puzzles October 28, 2026
Code Basics for Beginners: Caesar Shifts and Modular Arithmetic
A Caesar cipher turns each letter into a number, adds a fixed shift, and wraps around when it reaches the end of the alphabet. That small wraparound rule is modular arithmetic—and it is the key to both solving the cipher and understanding why it offers little protection.
Start with a shift of three
Write the alphabet in a line, then move every letter three places forward. A becomes D, B becomes E, and C becomes F. Near the end of the alphabet, the same rule continues: X becomes A, Y becomes B, and Z becomes C. The alphabet does not stop at Z; it loops back to its beginning.
That is the basic Caesar cipher. To encrypt a message, replace each letter with the one a fixed number of positions later. To decrypt it, move the same number of positions backward. The two directions are inverse operations: a forward shift of three is undone by a backward shift of three.
Try the short message KHOOR. Move each letter backward three places:
- K moves back to H.
- H moves back to E.
- O moves back to L.
- O moves back to L.
- R moves back to O.
The plaintext is HELLO. The last step is ordinary movement through a loop, not a special exception. Once the alphabet is treated as a cycle, every letter follows the same rule.
Turn letters into numbers
Arithmetic is easier to describe if each letter receives a number. Use the convention common in introductory cryptography: A = 0, B = 1, C = 2, and continue until Z = 25. The letter X, for example, is 23.
For a forward shift of three, add 3 to the number for each letter. If the result is between 0 and 25, convert it back to a letter. If it reaches 26 or more, subtract 26 until it is back in range. In symbols, the encryption rule is:
E(x) = (x + 3) mod 26
The word mod means “remainder after division.” For example, 26 divided by 26 leaves a remainder of 0, so 26 mod 26 = 0. Since A is 0, a letter shifted to numerical value 26 wraps to A. Likewise, 27 mod 26 = 1, which represents B.
Consider X. Under our numbering, X = 23. Add the shift: 23 + 3 = 26. Reduce modulo 26: 26 mod 26 = 0. And 0 maps back to A. So X becomes A, exactly as the alphabet strip showed.
Modulo 26 is not a code-specific trick. It is a way to keep numbers within a cycle of 26 positions. A clock uses a related idea: after 12 comes 1 on a 12-hour dial. The Caesar cipher uses a 26-position dial, with A at 0 and Z at 25.
Why the remainder matters
Without the modulo step, a shift would fail as soon as it passed Z. For instance, adding three to Y’s value gives 27, which is not one of the assigned letter numbers. Modulo 26 folds that result back into the allowed range: 27 mod 26 = 1, or B.
The same calculation works for every letter, including those close to the start of the alphabet. Shift A backward by one and its value becomes −1. A negative number can still be reduced modulo 26: −1 corresponds to 25 in the cycle, which is Z. In practical terms, move one place backward from A and you land on Z.
For a shift of k, the general encryption and decryption rules are:
- Encrypt: E(x) = (x + k) mod 26
- Decrypt: D(y) = (y − k) mod 26
Here, x is the original letter’s number, y is the encrypted letter’s number, and k is the shift. For the familiar shift of three, decrypting D means calculating (3 − 3) mod 26 = 0, which gives A. Decrypting A means (0 − 3) mod 26. The result wraps to 23, which gives X.
Some explanations number A as 1 and Z as 26. That convention can describe the same shifting process, but it makes the formulas less tidy: the values have to be adjusted before and after the calculation. Starting at A = 0 lets the alphabet correspond directly to the numbers from 0 through 25.
A second worked example: encrypting MATH
Use the same shift, k = 3, to encrypt MATH. First convert its letters to numbers, add three, and reduce modulo 26 when needed:
- M = 12; 12 + 3 = 15, which is P.
- A = 0; 0 + 3 = 3, which is D.
- T = 19; 19 + 3 = 22, which is W.
- H = 7; 7 + 3 = 10, which is K.
The encrypted result is PDWK. To check the work, shift each letter in PDWK backward three places. P returns to M, D to A, W to T, and K to H. The original word reappears.
Spaces and punctuation are usually left unchanged in a beginner’s version of the cipher. They make a message easier to read and do not affect the letter calculations. Some historical and puzzle variants remove spaces, group letters into blocks, or use a different alphabet. The rule must be stated before a solver can know what to preserve.
Try a puzzle
A message has been encrypted with a Caesar shift of five. Decode YMJ VZNHP GWTBS KTC. Move each letter backward five positions, wrapping around at A.
Before reading the solution, test a few letters. Y moves back to T. M moves back to H. J moves back to E. Those first three letters make THE, a useful clue that the shift is working.
Continue through the message:
- YMJ becomes THE.
- VZNHP becomes QUICK.
- GWTBS becomes BROWN.
- KTC becomes FOX.
The decoded message is THE QUICK BROWN FOX. It is a familiar sentence used to contain many different letters of the alphabet, making it handy for testing fonts and, here, checking that a cipher operation has been applied consistently.
Now make the puzzle more independent. Suppose you receive FDHVDU but no shift is given. You can test the 25 meaningful nonzero shifts. Moving backward three gives C-A-E-S-A-R: CAESAR. The repeated A and the recognizable result confirm the answer.
Why guessing is enough to break it
A Caesar cipher has only 25 nontrivial shifts to try. A shift of zero changes nothing, so it is not a useful secret key. That leaves a small list of possibilities: shift the message backward by one, then two, then three, and so on through 25. A person can test those options quickly; a short computer program can test them almost instantly.
There is another weakness. English letters do not occur equally often. E is common, while letters such as Q and Z are much less frequent. A Caesar shift moves those patterns without removing them. If a long encrypted message contains one letter far more often than the others, a solver can suspect that it stands for E, T, or another common letter, then test the implied shift.
For a short message, frequency clues can mislead. A three-letter code might contain an uncommon letter by chance, and several shifts may produce fragments that look plausible. That is why cryptanalysts use multiple clues: letter frequencies, common two- and three-letter words, repeated patterns, and the sense of the entire message.
For a Caesar cipher, frequency analysis is not even necessary in most cases. Trying every shift is called a brute-force attack: the solver systematically checks the available keys rather than relying on a clever guess. With just 25 possibilities, the search is small enough to do by hand. The cipher is useful for learning substitution and modular arithmetic, not for protecting private information.
Do not use a Caesar shift to secure passwords, messages, or personal data. Modern encryption is built around far more complex mathematical systems and careful implementation. A classroom cipher can demonstrate how a key changes a message; it cannot provide meaningful confidentiality.
A little history, with a careful claim
The cipher takes its name from Julius Caesar. The Roman historian Suetonius, writing in the early second century CE, described Caesar’s use of a simple substitution in correspondence. In The Life of Julius Caesar, Suetonius says letters could be shifted so that one letter stood in place of another. The account is an important historical reference, though it is not a surviving encryption manual written by Caesar himself.
The modern label “Caesar cipher” often points specifically to the shift of three. That makes the name easy to remember, but the method is broader: choose any fixed shift from 1 to 25 and apply it to every letter. The arithmetic remains the same; only the key, k, changes.
It is tempting to treat the cipher as a relic with no present-day value. Its security is obsolete, but its teaching value is not. The method gives learners a clean example of a rule, a key, an inverse operation, and a finite number system. It also makes an abstract expression—addition modulo 26—visible in a result such as X becoming A.
Use a letter strip to check your arithmetic
Draw two rows of the alphabet, one above the other. Keep the standard alphabet on the bottom row. On the top row, write the alphabet shifted three places to the left, so that the top-row A aligns with bottom-row D. The two rows then show the encryption mapping at a glance:
Plain: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Shifted: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
To encrypt, find a plaintext letter on the top row and read the aligned letter below it. To decrypt, start with the ciphertext letter on the bottom row and look up to the top. The strip is a physical model of the modular rule: the row wraps around rather than ending at Z.
For a different shift, rotate the top row by a different amount. A shift of one maps A to B; a shift of 13 maps A to N. The latter is called ROT13. Applying ROT13 twice restores the original text because moving 13 places forward on a 26-letter cycle and then another 13 returns to the start. It is a convenient way to hide a spoiler from a casual glance, but, like every Caesar shift, it is not secure encryption.
Common mistakes to watch for
- Changing direction midway: Decide whether you are encrypting forward or decrypting backward, then apply that direction to every letter.
- Forgetting the wrap: Moving backward from B gives A; moving backward from A gives Z. The alphabet is a cycle.
- Mixing numbering systems: If A = 0, then Z = 25. Keep that convention throughout the calculation.
- Subtracting without reducing: A negative result is not a failure. Reduce it modulo 26; for example, −2 corresponds to 24, or Y.
- Assuming the key is always three: Three is the classic example, but a Caesar cipher can use any fixed shift from 1 through 25.
A good check is to encrypt a short word and then decrypt the result with the same key. If the original does not return, inspect the direction, letter values, and wraparound step. This round trip is a simple way to catch an arithmetic slip.
One more challenge
Here is a ciphertext with an unknown shift: QEB NRFZH YOLTK CLU. It is not encrypted with a shift of three. Try moving the letters backward by one place at a time. When you reach a readable phrase, note the shift and verify it by encrypting the result in the opposite direction.
Solution: Move each letter backward three places. Q becomes N, E becomes B, and B becomes Y, so the first word is NBY—not readable. This is a useful reminder to check the direction as well as the shift. Move the ciphertext letters forward three places instead: Q becomes T, E becomes H, and B becomes E. The full message reads THE QUICK BROWN FOX.
For this puzzle, encryption used a backward shift of three, so decryption uses a forward shift of three. The phrase is the same one used earlier, but the direction has changed. “Decrypt by moving backward” is correct only when the original encryption moved forward; the reliable rule is to undo the operation that was applied.
In modular terms, a shift of −3 is undone by a shift of +3. Since adding and subtracting are inverse operations, the two rules cancel. This is the central mechanism: encode each letter with one modular shift, then recover it by applying the inverse shift.
What the cipher teaches
The Caesar cipher is small enough to solve on paper, yet it introduces several durable mathematical ideas. A letter can be represented by a number; a consistent rule can transform every item in a message; and a finite cycle makes values wrap instead of growing without limit. The shift is a key, and decryption is the inverse operation.
It also shows that a calculation can be perfectly correct while a system remains insecure. Modular arithmetic explains exactly how the cipher works, but the limited key space and preserved language patterns make it easy to break. Understanding the mechanism means seeing both its elegance and its limits.
For a final self-check, encrypt NUMBERS with a shift of seven, then decrypt your result. The first letter, N, has value 13; adding 7 gives 20, or U. The last letter, S, has value 18; adding 7 gives 25, or Z. If your encrypted word begins with U and ends with Z—and reversing the shift restores NUMBERS—you have applied the cycle correctly.