Skip to content
Programming Computational thinking
All classes Intermediate About 15 min Premium

A secret code: the Caesar cipher

Hide a message by moving every letter the same number of places along the alphabet, then read it back by moving the other way. Watch the program code a word letter by letter, see what happens after Z, and crack a coded word.

Core concept

A Caesar cipher replaces each letter with the letter a fixed number of places (the shift) further along the alphabet, going round from Z back to A; shifting back by the same number decodes it

Topic: Computational thinking

More than two thousand years ago, Julius Caesar sent secret messages by moving every letter three places along the alphabet: A became D, B became E, and HELLO became KHOOR. Anyone who knew the shift could move each letter back and read it. A program does the same thing one character at a time. It keeps a letter's place in the alphabet as a number (A is 0, B is 1, … Z is 25), adds the shift, and uses MOD 26 so that counting past Z goes round to A again. Spaces and other characters are kept as they are. To decode, the program uses the same steps with the shift taken away. A Caesar cipher is easy to crack, because there are only 25 shifts to try, but it shows the idea behind the codes that protect messages on phones today.

Key words explained

What you will learn

  • Code a word with a Caesar cipher for a given shift.
  • Explain how MOD 26 makes the alphabet go round from Z to A.
  • Decode a message by shifting back.
  • Explain why a Caesar cipher is easy to crack.

Before you start

  • Know the order of the letters of the English alphabet.

Materials

  • A strip of paper with the alphabet written on it

Equipment

  • None

Procedure

  1. Code HELLO with a shift of 3 and read the result.
  2. Decode KHOOR with a shift of −3.
  3. Code a message with spaces and see what happens to them.
  4. Answer the practice challenges.
Premium experiment Create a free account