What is cryptography?
Cryptography is the science of hiding information. Any time you want to send a message that only one person should be able to read, whether it is a general's battle plan or the password to your game account, you have a cryptography problem. The solution is encryption: scrambling the message with a secret recipe, called a cipher, so that anyone who intercepts it sees only nonsense.
Every cipher in this tutorial, from ancient ones to the encryption protecting your phone right now, uses the same three ingredients:
The original readable message. Example: THE EAGLE HAS LANDED
The scrambled version an eavesdropper sees. Example: GSV VZTOV SZH OZMWVW
The secret needed to lock and unlock the message. It might be a number, a keyword, or a whole scrambled alphabet.
Encrypting turns plaintext into ciphertext. Decrypting turns it back. If your friend has the key and the spy reading your mail does not, the message stays secret. The whole history of cryptography is a battle between people inventing better ciphers and codebreakers finding clever ways to crack them.
Two families of ciphers
Classic ciphers scramble a message in one of two ways. Either they rearrange the letters, or they replace them. That single difference splits almost every cipher into one of two families:
Transposition ciphers
The letters stay exactly the same, but their order gets shuffled by a pattern. It is like cutting a note into pieces and gluing them back in a secret order.
Substitution ciphers
The letters stay in order, but each one is swapped for a different letter or symbol. Every A might become a Z, every B a Y, and so on.
Spot the family: if the ciphertext has the exact same letters as the message, just jumbled, you are looking at transposition. If the letters themselves changed, it is substitution. Codebreakers check this first.
The Atbash mirror machine
One of the oldest substitution ciphers is Atbash, used over 2,500 years ago in ancient Hebrew texts. The recipe could not be simpler: flip the alphabet like a mirror. The first letter trades places with the last, the second with the second-to-last, and so on. A becomes Z, B becomes Y, C becomes X, all the way to Z becoming A.
Type a message and watch the mirror do its work. The strip shows the normal alphabet on top and the reversed alphabet below it; letters in your message light up.
Atbash Mirror Machine
EYES ONLY| Plain |
| Mirror |
Did you catch that? Running Atbash twice gives back the original message. Since A and Z simply trade places, encrypting and decrypting are the exact same operation. Atbash is its own inverse: one mirror flip scrambles, a second flip unscrambles. It also means Atbash has no key to choose, so anyone who recognizes the pattern can read your mail.
The rail fence cipher
The rail fence cipher is the classic transposition cipher. To encrypt, you write your message in a zigzag over a number of rows, called rails, then read the letters off row by row. The key is the number of rails.
Here is HELLO WORLD on 2 rails. Write the letters zigzagging down and up:
rail 1: H . L . O . O . L . rail 2: . E . L . W . R . D
ciphertext: HLOOL ELWRD
Same letters, new order, so it is pure transposition. To decrypt, your friend rebuilds the zigzag with the right number of rails and reads it the normal way. More rails means a taller fence and a better scramble. Here is a secret message written on 5 rails:
rail 1: T . . . . . . . H . . . . . . . D rail 2: . H . . . . . E . A . . . . . E . rail 3: . . E . . . L . . . S . . . D . . rail 4: . . . E . G . . . . . L . N . . . rail 5: . . . . A . . . . . . . A . . . .
Reading the rails top to bottom gives the ciphertext THDHE AEELS DEGLN AA. Can you trace the zigzag and recover the plaintext? Check your answer with the program from the original tutorial:
print("Using the rail fence cipher, decrypt the message: THDHE AEELS DEGLN AA\n") answer = input("Enter your answer: ") if answer.upper() == "THE EAGLE HAS LANDED": print("\nYou got it right!") else: print("\nNot quite. Try again by clicking on the green Run button.")
Using the rail fence cipher, decrypt the message: THDHE AEELS DEGLN AA Enter your answer: THE EAGLE HAS LANDED You got it right!
The keyword substitution cipher
Atbash always mirrors the alphabet the same way, so it has no real key. A keyword substitution cipher (also called a mixed alphabet cipher) fixes that. You and your friend agree on a secret keyword, write it at the start of the alphabet with repeated letters removed, then fill in the rest of the unused letters in order. That scrambled alphabet becomes your cipher alphabet.
With the keyword CODEHS, the mixed alphabet looks like this:
plain: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
cipher: C O D E H S A B F G I J K L M N P Q R T U V W X Y Z
Every plaintext A becomes C, every B becomes O, and so on. Unlike a simple shift, the mapping is jumbled, and there are more possible mixed alphabets than anyone could ever try by brute force: about 403,000,000,000,000,000,000,000,000 ways to scramble 26 letters.
Try it yourself: using the mixed alphabet above, how would you encrypt the word ATTACK? Work it out letter by letter, then check your answer:
print("Using the mixed alphabet cipher, how would you encrypt the word \"ATTACK\"? Type in your answer and press Enter.\n") answer = input("Enter your answer: ") if answer.upper() == "CTTCDI": print("\nYou got it right!") else: print("\nNot quite. Try again by clicking on the green Run button.")
Using the mixed alphabet cipher, how would you encrypt the word "ATTACK"? Type in your answer and press Enter. Enter your answer: CTTCDI You got it right!
Why keywords? A whole scrambled alphabet is a great key, but it is hard to memorize 26 letters in a random order. A keyword like CODEHS lets both spies rebuild the exact same mixed alphabet from one easy-to-remember word.
Pigpen: hiding letters in symbols
A substitution cipher does not have to swap letters for other letters. The pigpen cipher, famously used by the Freemasons in the 1700s, swaps each letter for a little geometric symbol. Every letter lives in a "pen" drawn from tic-tac-toe grids and X shapes, and its symbol is simply the shape of the walls around it. A dot marks the letters from the second grid.
It looks mysterious, but it is still an ordinary substitution cipher: one symbol per letter, always the same. A spy intercepted this pigpen message. Use the decoder key above to read it:
print("Using the pigpen cipher, decrypt the message above. Type in your answer and press Enter.\n") answer = input("Enter your answer: ") if answer.upper() == "WE WILL MEET AT THE NEXT FULL MOON": print("\nYou got it right!") else: print("\nNot quite. Try again by clicking on the green Run button.")
Using the pigpen cipher, decrypt the message above. Type in your answer and press Enter. Enter your answer: WE WILL MEET AT THE NEXT FULL MOON You got it right!
The cipher field guide
Here is your one-page reference card for the classic ciphers, which family each belongs to, and how it scrambles the word CODE (or where to learn more):
Atbash
Mirror the alphabet: A trades with Z, B with Y. No key needed, and it decrypts itself.
Caesar
Shift every letter the same number of places down the alphabet. The key is the shift, so there are only 25 to try.
Rail fence
Write the message in a zigzag over several rails, then read it off row by row. The key is the number of rails.
Keyword
Build a scrambled cipher alphabet from a secret keyword. Billions of billions of possible keys.
Pigpen
Swap each letter for a symbol drawn from grids and X shapes. Looks alien, cracks like any substitution.
Vigenere
Uses a keyword to give every letter its own shift, defeating simple frequency counting for 300 years.
Cracking ciphers with letter frequency
A keyword cipher has more possible alphabets than there are grains of sand on Earth, so brute force is hopeless. And yet codebreakers have been cracking substitution ciphers for over a thousand years. Their weapon is letter frequency analysis, first described by the Arab scholar Al-Kindi in the 9th century.
The trick: English is predictable. The letter E is the most common, followed by T and A, while letters like Z and Q are rare. A substitution cipher changes what each letter looks like, but it cannot hide how often it appears. Whatever symbol shows up the most is probably an E wearing a disguise.
We intercepted this message, encrypted with an unknown substitution alphabet. Compare its letter counts to normal English, then follow the hints to unmask it:
This is the universal weakness. Atbash, Caesar, keyword, pigpen: every cipher that always swaps a letter the same way falls to frequency analysis. Even transposition ciphers give themselves away, because their letter counts look exactly like normal English. That is what pushed code makers to invent stronger systems like the Vigenere cipher, and eventually modern encryption.
The bridge to modern encryption
Every message you send today, every password you type, and every game you log into is protected by encryption. It still uses the same three ingredients you learned at the top of this file: plaintext, ciphertext, and a key. So what changed?
The size of the key. Classic ciphers fell because their keys were small enough to guess or their patterns leaked through. Modern ciphers like AES scramble messages with keys that are just enormous numbers, and the number of possible keys is beyond astronomical:
| Cipher | Possible keys |
|---|---|
| Atbash | 1. There is nothing to choose, so it offers no real secrecy. |
| Caesar shift | 25. A computer brute-forces them all in under a millisecond. |
| Keyword substitution | About 4 x 10^26 alphabets, but frequency analysis skips the guessing entirely. |
| Modern AES-256 | About 1.2 x 10^77, more keys than there are atoms in the observable universe's stars. Brute force would take billions of times the age of the universe. |
Modern ciphers also scramble patterns, not just letters, so frequency analysis finds nothing to count. But the arms race between code makers and codebreakers never stopped. It just moved from parchment and pigpens to mathematics and supercomputers, and it is one of the most exciting corners of cybersecurity you can study.
Quick quiz
1. Using the Atbash cipher, what does CAB encrypt to?
2. The rail fence cipher turns HELLO WORLD into HLOOL ELWRD. Which family does it belong to, and why?
3. In a long English message encrypted with a substitution cipher, the symbol # appears far more often than anything else. What is the codebreaker's best first guess?
Keep learning cryptography
Case closed. You can now sort ciphers into families, encrypt with mirrors, fences, keywords, and symbols, and crack a substitution cipher like a 9th-century codebreaker. Your next assignments are below.