← CS Unplugged

CS Unplugged activity

Caesar Cipher: hiding a message in plain sight

CS Unplugged activity slides Lesson plan (and sub notes)

About 30 minutesSolo, or pair up to trade coded messagesPencil

Why hide a message?

More than two thousand years ago, Julius Caesar needed to send orders to his generals without an enemy who intercepted the letter being able to read it. His trick was simple: shift every letter in the message the same number of places through the alphabet. Anyone who did not know the shift just saw nonsense.

That is a cipher: a rule for turning a readable message into an unreadable one and back again. The rule here is called a shift cipher, or a Caesar cipher after the man who is said to have used it. It still shows up today, just as one small piece of much bigger, much harder ciphers, and as a classic first puzzle for learning how codebreaking works at all.

How a shift works

Pick a number from 1 to 25. That number is the key. To encode a message, slide every letter that many places later in the alphabet, wrapping back around to A after Z.

Here is key 3, worked out one letter at a time:

PlainHELLO
+3KHOOR

HELLO becomes KHOOR. H is the 8th letter, and 3 letters after H is K. E is the 5th letter, 3 letters after is H, and so on. Spaces do not shift; they just stay spaces.

To decode, run the same slide backward: shift every letter of KHOOR back 3 places and you land on HELLO again.

Your shift table

You do not need anything special to use a key, just the alphabet written out twice: once in order, and once shifted. Fill in the second row for whatever key you are using right now by counting forward that many letters for every blank, wrapping from Z back to A.

No key yet? Use the number of the month you were born in (a number between 1 and 12).

Key:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Once that row is filled in, encoding and decoding are the same move: find a letter in one row and read off whatever is below or above it in the other row. You will fill this row in again every time you use a new key, so do it in pencil.

Decode these

Here are five messages, all encoded with the same key.

Key: 7

Want a table for this key? Fill this one in first (it’s optional):

Key: 7

ABCDEFGHIJKLMNOPQRSTUVWXYZ
  1. AOL MPYZA WYVNYHTTLY DHZ HKH SVCLSHJL

  2. AOL MPYZA LCLY DLIZPAL PZ ZAPSS VUSPUL AVKHF

  3. DOF KV WYVNYHTTLYZ WYLMLY KHYR TVKL ILJHBZL SPNOA HAAYHJAZ IBNZ

  4. DOF KPK AOL SVVW ULCLY MPUPZO PA KPKUA RUVD DOLU AV ZAVW

  5. DOHA OHZ RLFZ IBA JHUUVA VWLU H ZPUNSL KVVY H RLFIVHYK

Encode one of your own

On a separate scrap of paper, pick your own key from 1 to 25 and write a short message in capital letters, no punctuation. Use your shift table to encode it letter by letter, checking each one as you go.

Copy just the finished, encoded message onto the line below, then swap papers with a partner.

Message to swap:

If you also tell your partner the key, they can decode it the same way you encoded it, just backward. For an extra challenge, do not tell them the key, and see the next part.

Crack it: no key given

This time, nobody tells you the key. All you get is a scrambled message.

That sounds harder, but think about what a key actually is: just a whole number from 1 to 25. There is no key 26, because shifting every letter by 26 places is the same as not shifting at all, and a key of 0 would not hide anything. That is only 25 possibilities in total. You can simply try every one of them until a real message falls out.

For example, suppose the message were KHOOR. Shift it back one key at a time:

Key KHOOR shifted back
1 JGNNQ (nonsense)
2 IFMMP (nonsense)
3 HELLO (a real word!)

The key was 3. If it hadn’t worked by 3, you would keep going: 4, 5, 6, all the way to 25 if you had to.

Now crack this message:

XFFU TFDDVEKJ VOGCRZE NYP EFK ALJK NYRK

Use the grid below to keep track. For each key, shift the message back that many places and jot down just enough of the result to tell whether it is real words or nonsense. You can stop as soon as one works.

12345
678910
1112131415
1617181920
2122232425

Learn more about the Caesar cipher.

Next step: try a Vigenère cipher, which uses a whole keyword instead of one shift.

Already comfortable with some Python? CS50’s Caesar problem set has you code this exact cipher, with the key typed on the command line.

Teachers: there is also an optional cut-out cipher wheel that does the same job as the shift table.

Check your answers

Decode these (key 7).

  1. The first programmer was ada lovelace
  2. The first ever website is still online today
  3. Why do programmers prefer dark mode because light attracts bugs
  4. Why did the loop never finish it didnt know when to stop
  5. What has keys but cannot open a single door a keyboard

Crack it. The key is 17. Shifting the message back that many places gives:

Good comments explain why not just what

If you found it with a different key than 17, check your alphabet count. Only one key turns that message into real words.