Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 40 ยท Getting Started

Classical Ciphers and Why They Break

Build the Caesar, substitution and Vigenere ciphers in Python, then break each one with brute force, frequency analysis and the index of coincidence.

Before computers, secret writing was done with pen and paper, using ciphers simple enough to apply by hand. These classical ciphers are no longer used to protect anything, but they are the best way to start learning cryptography: they are easy to implement, easy to understand, and, most importantly, easy to break. Breaking them teaches you the instincts every cryptanalyst needs: count things, look for patterns, and measure how many keys an attacker would have to try.

In this lesson you will implement three ciphers (Caesar, simple substitution and Vigenere) and then attack each one with a short Python program. Each cipher fails for a different reason, and each reason points toward what a modern cipher must do better.

Vocabulary

  • Plaintext is the readable message; ciphertext is the scrambled result.
  • A key is the secret that selects one particular transformation from the cipher's family.
  • The key space is the set of all possible keys. If an attacker can try every key, the cipher is broken by brute force, no matter how clever its design.
  • Cryptanalysis is the art of recovering plaintext or keys without being given the key.

All examples in this lesson use only the letters a to z in lowercase, which keeps the arithmetic simple.

The Caesar cipher

Julius Caesar reportedly shifted each letter of a message three places along the alphabet: a becomes d, b becomes e, and so on, wrapping z around to c. In general, the shift (a number from 0 to 25) is the key. Decrypting is shifting by the opposite amount. In Python, letters become numbers 0 to 25 with ord(c) - 97, and the wrap-around is just % 26, a first taste of modular arithmetic.

The weakness is plain: there are only 26 keys (25 useful ones). An attacker does not need cleverness; she tries them all and looks for the one that gives readable English. A computer can even do the judging, by counting how closely the letter frequencies of each candidate match English.

Python Caesar cipher and brute force

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Scan the 26 lines and exactly one is English. The scoring function finds it automatically. The lesson: a cipher with a tiny key space is broken before you even look at its structure. Keep the number 26 in mind; modern keys have 2128 or more possibilities.

Simple substitution

A natural fix is to scramble the alphabet completely. The key is now a permutation of the 26 letters (for example a becomes q, b becomes w, c becomes e, and so on, with no pattern). The number of keys is 26 factorial.

Python How big is the key space?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

That is about 4 x 1026 keys, around 288, far too many to try one by one. For centuries this seemed unbreakable. Yet it falls quickly, because it leaves a fingerprint behind.

Frequency analysis

In any language, letters do not occur equally often. In English, e is the most common letter (about 12.7% of text), followed by t, a, o, i and n. A substitution cipher replaces every e with the same stand-in letter, so the most frequent ciphertext letter very probably stands for e. Arab scholar Al-Kindi described this attack over a thousand years ago.

flowchart LR
    A["Ciphertext"] --> B["Count letter<br/>frequencies"]
    B --> C["Match the most frequent<br/>letters to e, t, a, o..."]
    C --> D["Check the partial text,<br/>fix guesses using words"]
    D --> E["Plaintext"]

Frequency analysis turns a hopeless search of 26! keys into an afternoon of pattern matching.

The program below encrypts a passage with a random substitution key (fixed seed so the output never changes), then recovers most of it by simply lining up letters by rank.

Python Frequency analysis of a substitution cipher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pure rank matching is weaker than you might expect: only about one letter in five is right. The passage is short (441 letters), and several letters have nearly equal counts, so their order is a coin flip (here t and e tie at 56). Adding one more clue, that the most common three-letter chunk is almost certainly "the", lifts the result to nearly half. A human, or a program that scores whole words, pair frequencies and word shapes, closes the rest of the gap quickly, and longer ciphertext makes the statistics sharper still.

Note: Frequency analysis is a statistical attack on the structure of a cipher. It works because each plaintext letter always maps to the same ciphertext letter. Modern ciphers are designed so ciphertext statistics look random, whatever the plaintext is.

The Vigenere cipher

For about three hundred years the Vigenere cipher was called "le chiffre indechiffrable" (the indecipherable cipher). Its idea is to use several Caesar shifts in turn. The key is a word, such as orbit. The first letter of the message is shifted by the position of o, the second by r, the third by b, and so on, repeating the keyword when it runs out. Because the same plaintext letter can now become different ciphertext letters, simple frequency counting no longer works.

In 1863 Friedrich Kasiski, and independently Charles Babbage earlier, found the weakness: a Vigenere cipher with a key of length L is just L Caesar ciphers interleaved. If you can find L, you split the ciphertext into L columns, and each column is a plain Caesar cipher that falls to frequency analysis.

How do you find L? Use the index of coincidence (IC): the probability that two letters picked at random from a text are the same. For English it is about 0.066 to 0.072. For uniformly random letters it is about 0.038. When you split a Vigenere ciphertext into columns with the right L, each column is a shifted English text, and the IC jumps up toward 0.066.

flowchart TD
    C["Vigenere ciphertext"] --> L["Try key lengths 1, 2, 3...<br/>Compute average IC of the columns"]
    L --> P["Pick the length where<br/>IC jumps to about 0.07"]
    P --> S["Split into columns:<br/>each is a Caesar cipher"]
    S --> F["Solve each column with<br/>frequency analysis"]
    F --> K["Key and plaintext recovered"]

The attack reduces one hard problem to several easy ones.

Python Breaking Vigenere

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The ciphertext IC is about 0.041, close to random, but the IC for length 5 jumps to about 0.070, and the recovered key is orbit, exactly the secret. Note that a multiple of the true length (10 here) also scores high, so we take the smallest length that passes the threshold. The attack needs enough ciphertext per column (a few dozen letters) to be reliable, which is why short messages sometimes survive.

More classical ciphers

Ciphers of this era fall into two families. A substitution cipher replaces symbols (Caesar, simple substitution, Vigenere). A transposition cipher keeps the symbols but rearranges their order. Substitution ciphers that use one fixed alphabet for the whole message are called monoalphabetic; those that switch alphabets (like Vigenere) are polyalphabetic.

Special cases of monoalphabetic substitution

  • The additive (shift) cipher is the Caesar cipher: c = (p + k) mod 26. It has 26 keys.
  • The multiplicative cipher uses c = (p * a) mod 26, where a must be coprime with 26 so the mapping can be reversed. Only 12 values of a work.
  • The affine cipher combines both: c = (a * p + b) mod 26, giving 12 x 26 = 312 keys. All of them can be tried in an instant.
  • Atbash reverses the alphabet (a becomes z, b becomes y). It has no key at all, which makes it an encoding rather than real protection.
  • ROT13 is Caesar with a shift of 13. Applying it twice returns the original text, so one function both encrypts and decrypts. It is used to hide spoilers, never secrets.

Their advantage is simplicity: you can run them with pencil and paper. Their drawback is fatal: the letter frequencies of the plaintext shine straight through, and the key space is tiny or, for a full substitution, defeated by frequency analysis as shown above.

Playfair: substituting pairs of letters

The Playfair cipher (1854) encrypts two letters at a time using a 5x5 square built from a keyword (I and J share a cell). For each pair of plaintext letters:

  1. If they are in the same row, take the letter to the right of each (wrapping around).
  2. If they are in the same column, take the letter below each.
  3. Otherwise, form a rectangle and take the letters in the same row at the opposite corners.

A repeated pair such as ll gets a filler letter (x) between them, and an odd length gets a filler at the end. Because it works on digraphs, Playfair flattens single-letter frequencies, but frequency analysis on letter pairs still breaks it with enough text.

Columnar transposition

In a columnar transposition you write the plaintext in rows under a keyword, then read the columns off in the alphabetical order of the keyword's letters. Nothing is substituted, so the ciphertext has exactly the letter frequencies of the plaintext; that is the clue that tells an analyst it is a transposition and not a substitution. Combining a substitution with a transposition (a "product cipher") is the seed of modern block ciphers, which repeat both operations for many rounds. Claude Shannon called these two goals confusion (hide the relation between key and ciphertext) and diffusion (spread each plaintext bit over many ciphertext bits).

Python Atbash, ROT13, Playfair and columnar transposition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All of these ciphers are insecure. They are worth knowing because they introduce the vocabulary of modern ciphers and because puzzles, capture-the-flag contests and history use them.

What the failures teach

Cipher Key space Why it breaks
Caesar 26 Too few keys: brute force
Substitution about 288 Each plaintext letter always maps to the same ciphertext letter: frequency analysis
Vigenere 26L Repeats every L letters: IC finds L, then each column is a Caesar cipher

Modern ciphers answer these in turn: huge key spaces (at least 2128), output that hides statistics of the plaintext, and a key stream that does not repeat. The rest of this tutorial shows how.

The only classical-style cipher that cannot be broken is the one-time pad, which uses a truly random key as long as the message and never reuses it. You will build it in XOR and the one-time pad.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can a Caesar cipher be broken by brute force in seconds?

    With only 26 shifts (25 useful), an attacker tries every key and picks the readable result.
  2. 2. A substitution cipher has about 2^88 keys, yet it is easy to break. Why?

    Each plaintext letter always becomes the same ciphertext letter, so the most frequent ciphertext letters correspond to e, t, a and so on. A large key space does not help if the structure leaks.
  3. 3. What does the index of coincidence tell you when attacking Vigenere?

    When the text is split using the correct key length, each column is just a Caesar cipher of English, so its IC rises to about 0.066 instead of the random 0.038.
  4. 4. Which change would make a Vigenere cipher safe against the key-length attack?

    A random key as long as the message never repeats, so there are no columns to attack. This is the one-time pad. A longer English-word key still leaves patterns.

Summary

  • Classical ciphers (Caesar, substitution, Vigenere) are easy to write and easy to break.
  • A key space that is too small falls to brute force; one that is large but leaves structure falls to analysis.
  • Frequency analysis recovers a substitution cipher because letter statistics survive encryption.
  • Vigenere is several Caesar ciphers interleaved; the index of coincidence finds the key length, then each column is solved separately.
  • Modern ciphers need huge key spaces and output that looks random, whatever the plaintext.

Next: Cryptanalysis and Attack Models, the vocabulary for describing what an attacker can do. After that comes Modular Arithmetic, the mathematics behind almost everything that follows.