Please enable JavaScript to use CodeHS

Cryptography Tutorial

Caesar and Vigenere Ciphers

Two thousand years before computers, generals and spies were already scrambling messages. Learn the two most famous classic ciphers, encrypt your own secret messages, and then crack them like a codebreaker.

cryptography ~25 min Beginner friendly
The Big Idea

What is a cipher?

Imagine you want to pass a note to a friend, but you know someone else might read it on the way. Encryption is the art of scrambling a message so that only someone who knows the secret can unscramble it. A cipher is the recipe you use to do the scrambling.

Codebreakers use three words all the time, so let's learn them first:

Plaintext

The original, readable message. Example: HELLO

Ciphertext

The scrambled message that others cannot read. Example: KHOOR

Key

The secret that turns one into the other. For a shift cipher, the key is just a number.

Encrypting turns plaintext into ciphertext. Decrypting turns it back. If your friend knows the key and the eavesdropper does not, your secret is safe. At least, that is the plan.


Ancient Rome, ~58 BC

The Caesar cipher

One of the oldest ciphers we know about was used by Julius Caesar, the famous Roman general. When Caesar sent orders to his armies, he did not want enemies who captured a messenger to read his plans. So he shifted every letter of the alphabet by the same amount.

Here is how a shift of 3 works. To encrypt, replace each letter with the letter 3 positions later in the alphabet:

  • A becomes D, B becomes E, C becomes F, and so on
  • At the end of the alphabet, wrap around: X becomes A, Y becomes B, Z becomes C

Let's encrypt the word HELLO with a shift of 3, one letter at a time: H moves 3 letters to K, E moves to H, each L moves to O, and O moves to R. The ciphertext is KHOOR.

The key is the shift. To read KHOOR, your friend just shifts every letter 3 positions backward. Anyone who does not know the key sees only gibberish.

Interactive

Try it: the Caesar cipher machine

Type a short message and pick a shift. The machine shows the alphabet mapping (top row is plaintext, bottom row is ciphertext) and encrypts your message live. Letters used in your message light up in the mapping.

Caesar Cipher Machine

3
Plain
Cipher
Ciphertext
KHOOR

Try this: set the shift to 0. What happens? Now try 26 by counting up. There is no 26, because shifting by 26 brings every letter right back where it started. That is why there are only 25 useful shifts.


Codebreaking

Cracking a Caesar cipher

Here is the Caesar cipher's fatal weakness: there are only 25 possible keys. A codebreaker does not need to be clever. They can simply try every single shift and see which one produces real words. Trying every possibility is called a brute force attack, and with 25 options it takes a computer almost no time at all.

We intercepted this secret message. Press the button to brute force it:

WKH VHFUHW PHHWLQJ LV DW QRRQ

The 25 candidate decryptions will appear here...

Every shift produces one candidate. Only one of them will look like English.

The frequency shortcut

Codebreakers have an even faster trick than brute force: letter frequency. In English, the letter E shows up more than any other letter. So in a long Caesar-encrypted message, whichever letter appears most often is probably a shifted E. Count the letters, find the most common one, measure its distance from E, and you have the key in one guess.

Code It

Caesar cipher in Python

The wrap-around from Z back to A is a perfect job for the modulus operator %. Each letter gets a position from 0 (A) to 25 (Z). Add the shift, take the result % 26, and you always land back inside the alphabet. Press Run:

caesar.py
def caesar_encrypt(message, shift):
    result = ""
    for letter in message:
        if letter.isalpha():
            # A=0, B=1, ... Z=25
            position = ord(letter) - ord("A")
            # shift, wrapping past Z back to A
            new_position = (position + shift) % 26
            result = result + chr(new_position + ord("A"))
        else:
            result = result + letter
    return result

print(caesar_encrypt("HELLO", 3))
print(caesar_encrypt("VENI VIDI VICI", 3))
Console
KHOOR
YHQL YLGL YLFL

Decrypting is the same trick in reverse. To decrypt a shift of 3, you can shift backward by 3, or forward by 23, since 26 minus 3 equals 23. In code: caesar_encrypt(ciphertext, 26 - shift).


France, 1586

The Vigenere cipher

For about 1,600 years, shift ciphers were state of the art. Then in the 1500s, a stronger idea appeared, named after the French diplomat Blaise de Vigenere. His fix for the Caesar cipher's weakness: stop using the same shift for every letter.

In a Vigenere cipher, the key is a keyword instead of a number. Each letter of the keyword names a shift: A means shift 0, B means shift 1, C means shift 2, and so on. You line the keyword up under your message, repeating it as many times as needed, and shift each message letter by its own key letter.

Here is HELLO encrypted with the keyword CODE. Notice how the keyword repeats: the fifth letter uses C again.

  • H + shift 2 (from C) = J
  • E + shift 14 (from O) = S
  • L + shift 3 (from D) = O
  • L + shift 4 (from E) = P
  • O + shift 2 (from C again) = Q

So HELLO becomes JSOPQ. The two L's in HELLO turned into two different letters, O and P. That is exactly what makes frequency counting so much harder.

Try it: the Vigenere encoder

Type a message and a keyword. The table shows each plaintext letter, the key letter lined up under it, and the shift it applies, so you can watch the keyword repeat.

Vigenere Encoder

Plain
Key
Shift
Cipher
Ciphertext
JSOPQ

The keyword CODE repeats every 4 letters. A key letter of A would mean shift 0.

Head to Head

Caesar vs. Vigenere

Both ciphers shift letters, but the repeating keyword changes everything about how hard they are to break:

Caesar cipherVigenere cipher
The key One number from 1 to 25 A keyword, like CODE or LEMON
Possible keys Just 25 Huge. A 5-letter keyword alone has almost 12 million possibilities
Same letter encrypts to... Always the same letter Different letters, depending on its position
Main weakness Brute force all 25 shifts, or count letter frequencies The keyword repeats, and patterns in long messages give away its length

History

Breaking the "unbreakable" cipher

The Vigenere cipher was so strong for its time that the French called it le chiffre indechiffrable, "the indecipherable cipher." For about 300 years, nobody published a way to break it. Frequency counting failed because the same letter could encrypt to many different letters.

But it had one crack in its armor: the keyword repeats. In a long message, common words like THE eventually get encrypted by the same part of the keyword more than once, producing repeated chunks in the ciphertext. In the 1800s, codebreakers including Charles Babbage and Friedrich Kasiski realized that measuring the distance between those repeats reveals the keyword's length. Once you know the keyword is, say, 4 letters long, the message splits into 4 separate Caesar ciphers, and you already know how to crack those!

Connecting to today: modern encryption, like the kind protecting your messages and passwords online, still uses the same three ideas of plaintext, ciphertext, and keys. The difference is scale: instead of 25 keys, modern keys have more possibilities than there are atoms in the known universe, so brute force is hopeless. The history of ciphers is an arms race between code makers and code breakers, and it is still running.


Code

A Python cipher

This Python function shifts every letter by a fixed amount, the same idea as a Caesar cipher.


Check Yourself

Quick quiz

1. Using a Caesar cipher with a shift of 1, what does CAT encrypt to?

2. Why is a Caesar cipher easy to crack by brute force?

3. You encrypt a 6-letter message with the Vigenere keyword CODE. Which key letter shifts the 5th letter of the message?

Keep learning cryptography

You can now encrypt like a Roman general and crack codes like a Victorian mathematician. Next, explore more cipher systems, or dig into the modulus math that powers the wrap-around.