Codes and Ciphers

Middle schoolComputer ScienceLetters A to Z are shifted or substituted; other characters pass through. Frequency analysis compares letter counts with English.

Unlock this lab with a pass

A pass opens every simulation on the site, including Present mode for class. The teaching notes below stay free for everyone.

One-time payment, no subscription.

About this experiment

For thousands of years people have hidden messages by scrambling letters. Julius Caesar shifted each letter three places along the alphabet; later codes swapped letters using a secret keyword or changed the shift from letter to letter. Every one of these ciphers was eventually broken, often by counting how often each letter appears. Encrypt and decrypt your own messages, step through the method letter by letter, then crack a secret text with frequency analysis.

Learning goals

  • Encrypt and decrypt messages with Caesar, keyword substitution and Vigenère ciphers
  • Explain the difference between an algorithm and a key
  • Compare how many possible keys each cipher has and what that means for a brute force attack
  • Use letter frequencies to break a Caesar cipher
  • Explain why modern encryption needs much more than letter substitution

How to use it

  1. 1Encrypt the sample message with a Caesar shift of 3. Step through the letters and follow each one round the wheel.
  2. 2Press Use result as input and decrypt it. Which shift undoes a shift of 3? What about a shift of 13 applied twice?
  3. 3A Caesar cipher has only 25 useful keys. How long would it take to try them all by hand? By computer?
  4. 4Switch to the keyword cipher. How is the cipher alphabet built from the keyword? Why does it have so many more possible keys?
  5. 5Encrypt a message with Vigenère and key LEMON. Find a repeated letter in the message that encrypts to two different letters. Why does this help against frequency analysis?
  6. 6Open Crack it. Slide the shift until the outline matches the bars. Check that the decrypted text makes sense.
  7. 7Paste a very short ciphertext into Crack it. Why does frequency analysis work less well on short texts?

The model

Caesar encryption, with letters numbered A = 0 to Z = 25
Caesar decryption
Vigenère: the shift changes with each letter of a key of length m
How far the letter counts are from normal English

Assumptions

  • Only the 26 letters A to Z are enciphered; case is kept and everything else is copied unchanged, as in classroom ciphers.
  • English letter frequencies are standard published values. Frequency analysis assumes the hidden text is ordinary English.
  • The keyword cipher writes the keyword without repeated letters, then the rest of the alphabet in order.
  • The number of keyword keys counts every possible rearrangement of the alphabet, 26! ≈ 4 × 10²⁶.

Try this in class

  1. Q1Decrypt KHOOR ZRUOG, a Caesar cipher with shift 3.
  2. Q2Why is a shift of 26 useless?
  3. Q3The keyword cipher has about 4 × 10²⁶ keys, yet it is easy to break. Explain how.
  4. Q4Why does the Vigenère cipher hide letter frequencies better than a Caesar cipher?
  5. Q5Explain the difference between a code and a cipher.

Frequently asked questions

Why does the lab only work with English text?

These historical ciphers work on the 26-letter Latin alphabet, and the crack tool compares letter counts with English. Other languages have different letter frequencies, and scripts such as Chinese or Japanese need completely different methods.

How was the Vigenère cipher broken?

In the 1850s and 1860s Charles Babbage and Friedrich Kasiski found that repeated groups of letters reveal the key length. Once the length is known, every m-th letter is a simple Caesar cipher that frequency analysis can break.

How is this different from the encryption used on the internet?

Modern ciphers such as AES work on bits rather than letters and mix them through many rounds, so letter counts give nothing away. Public key methods such as RSA also let two people agree on a key without ever meeting.