Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Now that we’ve covered data types, loops, functions, and modules, we’re well equipped to leverage the power of Python to start analyzing ciphertext messages for patterns that will help us determine the keys to a cipher or reveal their plaintext messages even if we don’t have the key. This practice is called cryptanalysis, or conversationally cracking a cipher.

A Short History

Cryptanalysis is much older than computers. The earliest known description of frequency analysis, the method behind Frequency Analysis, comes from Abū Yūsuf Yaʻqūb ibn ʼIsḥāq al-Kindī (c. 801 to c. 873), a scholar who worked in Baghdad. In his Manuscript on Deciphering Cryptographic Messages he explained that letters don’t all appear equally often in a language, and that a cipher which swaps each letter for another one keeps those patterns. (Wikipedia: Al-Kindi)

For centuries after that, cryptanalysis was done by hand, often by large teams. By the 1700s, several European governments ran “black chambers” that secretly opened and copied diplomatic mail. The best known was the Geheime Kabinettskanzlei in Vienna. Letters addressed to foreign embassies were routed through it early each morning. Clerks melted the seals, copied the letters, resealed them and sent them on to the post office within a few hours, so the delivery wasn’t noticeably late. The intelligence went to the Austrian emperors, and some of it was even sold to other countries. (Simon Singh, The Code Book, 1999, Chapter 2; Wikipedia: Cabinet noir)

The need to break ciphers has also pushed technology forward. In 1938, Polish cryptanalysts led by Marian Rejewski built electro-mechanical machines called bomby to help find the daily settings of the German Enigma machine. Building on the Polish work, Alan Turing designed the British bombe, and the first one went into service in 1940. A bombe was not a computer: it was an electro-mechanical device that imitated many Enigma machines at once to rule out settings. At the same codebreaking center, Bletchley Park, engineer Tommy Flowers built Colossus (1943 to 1944) to attack a different German cipher, the Lorenz cipher. Colossus was electronic and could be reconfigured for different tasks, and it is often called the first programmable electronic digital computer. It was kept secret for decades, so it had little direct influence on later computers, but it showed what electronic machines could do. (Wikipedia: Bombe, Wikipedia: Colossus computer)

Types of Attacks

Following Kerckhoffs's principle, we’ll always assume the attacker knows which cipher was used, but not the key. There are three broad ways to attack a cipher:

  1. Brute force. Try every possible key and look for the one that produces a readable message. This only works when there aren’t too many keys. See Brute Force.

  2. Mathematically reverse engineering the key. Use the cipher’s math to solve for the key. In a known-plaintext attack, the attacker knows (or correctly guesses) some of the plaintext that matches part of the ciphertext, and solves equations for the key. See Solving Congruences. Later, for public key ciphers, you’ll see attacks that work out a private key from a public key.

  3. Statistical analysis. Use patterns in the language, such as how often each letter appears, to find the most likely key. See Frequency Analysis and Chi-Squared Scoring.

Attacks are also described by what the attacker has to work with. In a ciphertext-only attack the attacker has nothing but the ciphertext. Brute force and statistical analysis can work with ciphertext alone. A known-plaintext attack needs a matching piece of plaintext, or a good guess at one, like a greeting or a name that probably appears in the message.

In This Chapter