Skip to content

← Math Further 6–8

Can You Write a Message Only a Friend Can Read?

Topic 7 · Grades 6–8 · Ciphers and remainders

What this builds on from class
  • Grade 6: Whole Number (division)
In this unit 7 sections
  1. Try it
  2. Clock arithmetic
  3. A cipher that multiplies
  4. Where the rule breaks
  5. Where this is used
  6. Questions
  7. Where to go next

In the middle of a maths class in Bhaktapur, a folded note lands on your desk. It is from your friend Anisha, and it says:

PHHW PH DW VLGGKD SRNKDUL DIWHU VFKRRO

Nonsense, to anyone who picks it up. But you and Anisha agreed on a rule last week: every letter is moved three places along the alphabet. A became D, B became E, M became P. To read it, you move every letter three places back. Turn the wheel below and read the note.

Try it

Read Anisha’s note

Each letter on the outer ring is a plain letter; the letter beside it on the inner ring is its secret letter. Slide to shift 3, then read the note: for each secret letter, the plain letter just outside it.

AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZShift 0A ↔ A
The note
PHHW PH DW VLGGKD SRNKDUL DIWHU VFKRRO
Read with shift 0
PHHW PH DW VLGGKD SRNKDUL DIWHU VFKRRO

Shift 0: every letter stands for itself, so this is the note as it was written. Turn the wheel.

Shift 0: not words. Keep turning: there are only 26 places the wheel can stop.

Shift 0: words! Each secret letter on the inner ring has its plain letter just outside it.

Answer

Now the question that matters. Suppose your teacher picks up the note instead of you, and doesn’t know your rule. Can she read it?

Guess first. A stranger finds a note written this way, but doesn’t know how many places the letters were moved. How hard is it for her to read it?

Pick an answer to see why.

Try it

A note a stranger finds

A message written in a hidden way is a cipher (the secret rule) turned into ciphertext (the scrambled message). Anisha’s rule, moving every letter the same number of places, is the shift cipher, and the number of places is its key. Your teacher has the ciphertext but not the key.

Here is the stranger’s problem for you. On the way home you find a note on the bus stop bench:

SIZEX KYV TIZTBVK SRK FE JRKLIURP

You don’t know the shift. Turn the wheel until it reads.

Try it

Crack the note on the bench

The same wheel, a different note. Slide from 1 upwards and watch the bottom line. How many tries did you need?

AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZShift 0A ↔ A
The note
SIZEX KYV TIZTBVK SRK FE JRKLIURP
Read with shift 0
SIZEX KYV TIZTBVK SRK FE JRKLIURP

Shift 0: every letter stands for itself, so this is the note as it was written. Turn the wheel.

Shift 0: not words. Keep turning: there are only 26 places the wheel can stop.

Shift 0: words! Each secret letter on the inner ring has its plain letter just outside it.

Hint
Answer

So the stranger needs at most 25 tries, and with a little cleverness, one. That is the first lesson of secret writing: a cipher with few keys is no secret at all.

Going round the end

Something odd happens near the end of the alphabet. Move X three places: Y, Z, and then? There is no letter after Z, so the alphabet starts again: X becomes A, Y becomes B, Z becomes C. The alphabet is not a line but a circle, like the numbers on a clock.

To do sums with it, give each letter a number. Start at 0, as a clock’s hand starts at the top: A is 0, B is 1, C is 2, and so on to Z, which is 25.

A B C D E F G H I J K L M
0 1 2 3 4 5 6 7 8 9 10 11 12
N O P Q R S T U V W X Y Z
13 14 15 16 17 18 19 20 21 22 23 24 25

Now a shift of 3 is just adding 3. M is 12, and 12 + 3 = 15, which is P. But X is 23, and 23 + 3 = 26: there is no letter 26.

A0B1C2D3E4F5G6H7I8J9K10L11M12N13O14P15Q16R17S18T19U20V21W22X23Y24Z25X moved 3 places:23 + 3 = 2626 ÷ 26 leaves 0,and 0 is A
Figure 7.1 The alphabet as a clock of 26. From X (23), three steps on pass Y and Z and reach A: 23 + 3 = 26, and 26 divided by 26 leaves 0, which is A.

Go round the clock and look where you land. After 25 (Z) comes 0 (A) again, so 26 lands on 0, 27 on 1, 28 on 2. Grade 6’s division says exactly where: divide by 26 and keep the remainder. 26 = 26 × 1 + 0, so 26 lands on A. 28 = 26 × 1 + 2, so Z + 3 is C. And 30 = 26 × 1 + 4 lands on E, however you got to 30.

The rule for any shift, then, is: change the letter to its number, add the key, divide by 26 and keep the remainder, and change it back to a letter.

Shift the letter W (number 22) by 7. Which letter do you get?

Pick an answer to see why.

Undoing a shift

To read Anisha’s note you moved every letter 3 places back. But there is another way, with no going backwards at all: move every letter 23 places forward. The two are the same, because 3 + 23 = 26, a full turn of the clock, which brings every letter back where it started.

Check it on D: D is 3, and 3 + 23 = 26, which leaves 0: A, the letter D came from. Every shift has an undo shift that adds up with it to 26: 1 and 25, 2 and 24, 10 and 16. And one shift is its own undo: 13, since 13 + 13 = 26.

Clock arithmetic

Remainders do the counting

What you have been doing has a name. Arithmetic where you divide by a fixed number and keep only the remainder is modular arithmetic, or clock arithmetic, and the fixed number is the modulus. Topic 4, Why Do the Divisibility Tests Work?, used it with modulus 9; here the modulus is 26, the number of letters. Mathematicians write “30 mod 26 = 4” for “30 leaves 4 when divided by 26”, and say “30 and 4 are the same, mod 26”.

An ordinary clock is the same arithmetic with modulus 12. If the hour hand points to 7 now, then in 8 hours it points to 3, because 7 + 8 = 15, and 15 leaves 3 when divided by 12.

Why does keeping only the remainder never go wrong? Because a full turn of the clock changes nothing. Any number is some whole turns of 26 plus a remainder: 30 is one turn and 4 more. The whole turns bring you back to where you started, so only the remainder decides where you land. That is also why two shifts add up: 20 places, then 10 more, is 30 places, which is one turn and 4.

A shift cipher moves every letter by 1,000 places. Which shift is that the same as?

Pick an answer to see why.

Why only 25 keys?

Now the stranger’s easy job makes sense. A shift of 26 is a full turn, the same as a shift of 0. A shift of 27 is the same as 1, a shift of 28 the same as 2. Every whole number lands on one of the 26 remainders, 0 to 25, so there are only 26 different shift ciphers, and one of them (0) leaves the note as it was. Twenty-five keys that hide anything, and a stranger can try them all before her tea gets cold.

A cipher that multiplies

Times instead of plus

If adding gives only 25 keys, what about multiplying? Here is a new cipher: change each letter to its number, multiply by 3, divide by 26 and keep the remainder.

Letter Number × 3 Remainder on dividing by 26 Secret letter
N 13 39 13 N
E 4 12 12 M
P 15 45 19 T
A 0 0 0 A
L 11 33 7 H

So NEPAL becomes NMTAH. Not a shift: N stays N, E jumps 8 places, P only 4. The whole secret alphabet, with A to Z on top and their secret letters underneath, is jumbled:

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
A D G J M P S V Y B E H K N Q T W Z C F I L O R U X

Look along the bottom row: every letter of the alphabet is there, each exactly once. That matters. When Anisha’s friend sees a secret T, only one plain letter could have made it (P), so the note can be read back.

Now try multiplying by 2 instead. Then B (1) becomes 2, C. But O (14) becomes 28, which leaves 2: C again. B and O have the same secret letter, and when your friend sees a C, she can’t tell which one you meant. Worse, CAT and PNG both come out as EAM. A cipher your friend can’t read back is no use.

Try it

Which multipliers work?

Under each plain letter is its secret letter for the multiplier on the slider. A secret letter that two plain letters share is red. Slide through every multiplier from 1 to 25 and watch the strip of multipliers fill in. Which ones work?

Plain letters, their secret letters under themAABDCGDJEMFPGSHVIYJBKELHMKNNOQPTQWRZSCTFUIVLWOXRYUZXMultipliers tried12345678910111213141516171819202122232425workstwo letters clash
Different secret letters
26 of 26

Every one of the 26 letters gets a secret letter of its own, so your friend can undo the cipher.

Answer

Why the odd ones, but not 13?

The multipliers that fail are the even ones and 13. And 26 = 2 × 13. So a multiplier fails exactly when it shares a factor with 26. Why?

When it shares a factor, two letters clash. Take any even multiplier, say 2. Multiply N (13) by it: 2 × 13 = 26, a full turn, so N lands on 0, the same place as A. Every even number times 13 is a multiple of 26 (4 × 13 = 52 = 2 × 26), so for every even multiplier, N lands on A. For 13, C (2) does it: 13 × 2 = 26, so C lands on A too. A shared factor lets some letter other than A make a whole number of turns, and land on A.

When it shares no factor, nothing clashes. Suppose two different letters, numbered a and b, did land in the same place when multiplied by k. Then k × a and k × b leave the same remainder, so their difference, k × (a − b), is a whole number of turns: a multiple of 26 = 2 × 13. 2 and 13 are primes, and a prime that divides a product of two numbers divides one of them (a rule Euclid proved, which topic 5, Is There a Biggest Prime Number?, meets), so the 2 and the 13 must each come from k or from a − b. If k has no 2 and no 13 in it, they must both be in a − b, so a − b is a multiple of 26. But a and b are numbers from 0 to 25, so they are less than 26 apart. The only multiple of 26 that small is 0, so a = b: it was the same letter after all. No two different letters can clash.

Two numbers that share no factor except 1 are called coprime. The rule is: multiplying by k makes a cipher, mod 26, exactly when k and 26 are coprime.

Which of these multipliers makes a cipher your friend can read back?

Pick an answer to see why.

Undoing a multiplication

To undo a shift you add the undo shift. To undo multiplying by 3, you multiply again, by a number that brings every letter home. Which number? One where 3 × it is one more than a whole number of turns: 3 × 9 = 27 = 26 + 1. So multiplying by 3 and then by 9 is the same as multiplying by 27, which leaves 1: every letter back where it was. Check it on NMTAH: N (13) × 9 = 117 leaves 13, N; M (12) × 9 = 108 leaves 4, E. NEPAL comes back.

Each of the twelve good multipliers has its own undo number. For 7 it is 15, since 7 × 15 = 105 = 4 × 26 + 1. An even multiplier has none: an even number times anything is even, and an even number can never be one more than a multiple of 26, which is even too. Nor has 13: 13 times anything is a multiple of 13, and so is every multiple of 26, so the two are never just 1 apart.

Multiply, then shift

Put the two ciphers together: multiply by one of the 12 good numbers, then shift by any of 26. That makes 12 × 26 = 312 keys, more than twelve times as many as the shift cipher. Better, but a patient stranger with a pencil could still try them all in an afternoon, and a computer does it in less than a blink.

Where the rule breaks

Add a space, and 3 stops working

Anisha decides that spaces give too much away (a three-letter word is probably THE), so she hides them too: the space becomes a 27th symbol, numbered 26, and her modulus becomes 27. Is multiplying by 3 still safe?

No. Now 27 = 3 × 3 × 3, and 3 shares a factor with it. A (0) gives 0. J (9) gives 27, a full turn: 0 again. S (18) gives 54, two full turns: 0 again. Three letters on one place. With 27 symbols the good multipliers are the 18 numbers from 1 to 26 that 3 doesn’t divide, and 2, which failed with 26 letters, now works.

A different shift for every letter

The shift cipher fails because every letter moves the same way: E always becomes the same secret letter, and a word guessed opens the whole note. So move each letter by a different shift, taken in turn from a key word. With the key word MOMO, the shifts are M (12), O (14), M (12), O (14), then round again:

Plain S E E Y O U A T N I N E
Shift 12 14 12 14 12 14 12 14 12 14 12 14
Secret E S Q M A I M H Z W Z S

SEE YOU AT NINE becomes ESQ MAI MH ZWZS. The two E’s of SEE have become different letters, S and Q, and the S at the end of the secret note came from an E, not an S. This is the idea of the Vigenère cipher (named after the Frenchman Blaise de Vigenère, though the Italian Giovan Battista Bellaso described it first, in 1553), and it is much harder to crack than any shift. Still, a short key word repeats, and its repeating pattern can be found (Grade 9 Computing shows how ciphers are broken by counting letters). But with a key as long as the message, chosen at random, kept secret and used only once, it becomes the one-time pad, which can’t be broken at all.

Where this is used

Questions

What does CAT become with a shift of 3?

Pick an answer to see why.

a.★

Your cousin in Pokhara sends you this, with a shift of 5: IFXFNS MTQNIFDX XYFWY TS KWNIFD. What does it say?

Hint
Answer

On the alphabet clock, which letter is Z (25) + 4?

Pick an answer to see why.

b.★★

Anisha shifts a note by 7, and then shifts the result by 22. What single shift has she used? And which shift would undo a shift of 10?

Answer
c.★★

The words RIVER and ARENA are shifts of each other. Find the shift that turns RIVER into ARENA. Why does that make a five-letter secret word risky to guess from?

Hint
Answer

Multiplying every letter’s number by 5 (mod 26) makes a good cipher. Which number undoes it?

Pick an answer to see why.

d.★★

Explain why multiplying by 4 can’t make a cipher your friend can read, by finding two letters that get the same secret letter.

Answer
e.★★★

Anisha uses 27 symbols: the 26 letters and the space (numbered 26). How many multipliers from 1 to 26 make a cipher she can undo? Which number undoes multiplying by 2?

Hint
Answer
f.★★★

A spy uses the multiply-then-shift cipher with 26 letters: multiply by some k, then shift by some b. You learn that in her notes, E always becomes F and T always becomes G. Find k and b.

Hint
Answer
g.★★★

ROT13 is its own undo: doing it twice gives the message back. Is any other shift from 1 to 25 its own undo? What about the multiplying cipher: which good multipliers are their own undo numbers?

Answer

Where to go next

Where these facts come from
  • The shift cipher as (x + n) mod 26 with A = 0, the 25 shifts that can be tried, two shifts making one, Augustus’s shift of 1 without going round the end, RIVER and ARENA as shifts of each other, the Vigenère cipher and the one-time pad, al-Kindi’s 9th-century works as the earliest known breaking of such ciphers: Wikipedia, “Caesar cipher”, read October 2026.
  • Caesar’s cipher (D in place of A), Life of Julius Caesar 56, and Augustus’s (B for A, C for B, AA for X), Life of Augustus 88: Suetonius, The Lives of the Twelve Caesars, J. C. Rolfe’s translation (Loeb Classical Library, 1914, public domain), on Bill Thayer’s LacusCurtius: Julius and Augustus, read October 2026; retold in our words.
  • The multiply-then-shift (affine) cipher, the 12 multipliers coprime to 26, 312 keys, its undoing by the multiplicative inverse, its breaking from two known letters, Atbash as multiplier −1, and the same formula in linear congruential generators: Wikipedia, “Affine cipher”, read October 2026; linear congruential generators as one of the oldest and best-known ways of making random-looking numbers, and not for cryptography: Wikipedia, “Linear congruential generator”, read October 2026.
  • Atbash, its name, and Sheshach for Babel in Jeremiah 25:26 and 51:41: Wikipedia, “Atbash”, read October 2026.
  • The Vigenère cipher first described by Giovan Battista Bellaso in 1553 and later misattributed to Blaise de Vigenère: Wikipedia, “Vigenère cipher”, read October 2026.
  • ROT13, its own inverse because 26 = 2 × 13, and its use on Usenet from the early 1980s for jokes, puzzle answers and spoilers: Wikipedia, “ROT13”, read October 2026.
  • Al-Kindi’s A Manuscript on Deciphering Cryptographic Messages (9th century), and E, T, A and O as the commonest English letters: Wikipedia, “Frequency analysis”, read October 2026.
  • Modular arithmetic, the 12-hour clock example (7 + 8 ≡ 3), and Gauss’s Disquisitiones Arithmeticae (1801) as the start of its modern form: Wikipedia, “Modular arithmetic”, read October 2026.
  • The Disquisitiones written in 1798, when Gauss was 21, published in 1801: Wikipedia, “Disquisitiones Arithmeticae”, read October 2026; Gauss born in Brunswick on 30 April 1777, the book published in the summer of 1801: MacTutor, “Carl Friedrich Gauss”, read October 2026.
  • Article 1’s definition of congruent numbers and modulus, Article 2’s sign ≡, and the footnote on its analogy with equality and on Legendre’s use of = : the Disquisitiones Arithmeticae, First Section, Wikisource’s translation, read October 2026; retold in our words. The sign first in print in 1801: Jeff Miller, “Earliest Uses of Symbols of Number Theory”, MacTutor, read October 2026.
  • RSA, described by Rivest, Shamir and Adleman in 1977, its encryption c ≡ mᵉ (mod n), and its wide use for sending data safely: Wikipedia, “RSA cryptosystem”, read October 2026.
  • Every ciphertext, remainder, count and key in this topic (the two notes, the 26 tries on the bench note, NEPAL, the multipliers that work for 26, 27 and 29 symbols, the undo numbers, 312 keys, the MOMO table, the spy’s key, the questions’ answers) computed in checks/textbooks/math-further-6-8/07/tmp/numbers.py.