Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Caesar Cipher: Encrypting and Decrypting Messages

Recommended resources

Welcome back! In our last lesson, we explored the Scytale, an ancient Greek device that scrambled messages by rearranging letters. This was our first look at a transposition cipher. Before that, we saw the Atbash cipher, which substituted letters according to a fixed rule.

Today, we'll return to the world of substitution ciphers but with a crucial new element: a secret key. We will investigate what is arguably the most famous cipher in history: the Caesar cipher. This lesson will show you how a simple, systematic shift in the alphabet can hide a message. Your goal is to learn how to apply this shift to both encrypt and decrypt messages, which will be our first step toward understanding ciphers that rely on a shared secret.

A General's Secret

The Caesar cipher is named after Julius Caesar, who, according to the Roman historian Suetonius, used it to protect his military correspondence around 58 BC. The idea was simple but effective for its time: if a message fell into enemy hands, it would look like nonsense.

The method is a substitution cipher where each letter in the plaintext is "shifted" a certain number of places down the alphabet. The number of places you shift is the key. Caesar's preferred key was 3. So, to encrypt a message, he would replace every 'A' with a 'D', every 'B' with an 'E', and so on.

Caesar Cipher Tutorial: Complete Beginner's Guide with Examples

This guide provides an excellent historical overview of the Caesar cipher's origins and use.

Read the section titled Historical Origins and Context. Pay attention to the quote from Suetonius and the other historical examples, like its use in the Victorian era and its unfortunate failure during World War I.


The Shifting Alphabet

The core of the Caesar cipher is the consistent shift. If the key is 3, every letter moves three positions forward. What happens when you get to the end of the alphabet? It simply wraps around. With a key of 3, 'X' becomes 'A', 'Y' becomes 'B', and 'Z' becomes 'C'.

You can visualize this as two alphabets, one shifted relative to the other, or as the entire alphabet written in a circle.

This diagram shows a Caesar cipher with a shift of 3. The top alphabet is the plaintext, and the bottom is the ciphertext. Notice how 'A' maps to 'D', 'B' to 'E', and so on.

This short video from Khan Academy provides a clear, animated explanation of this process for both encryption and decryption.

The Caesar cipher Journey into cryptography Computer Science Khan Academy

This video gives a concise overview of how to use a Caesar cipher with a pre-agreed key.

Watch the entire video, which is just under one minute long. It covers the historical context, the process of encryption by shifting letters, and decryption by reversing the shift.


From Shifts to Mathematics

While you can always encrypt and decrypt by manually counting letters or using a cipher wheel, a more precise and powerful method is to use mathematics. This is where the concept of modular arithmetic, which you recognized from clocks and angles, becomes very useful.

First, we assign a number to each letter of the alphabet, starting with A=0, B=1, C=2, and so on, up to Z=25.

To encrypt, we use the following formula:

Where:

  • is the number of the ciphertext letter.
  • is the number of the plaintext letter.
  • is the key (the shift value).
  • is the modulus operator. It handles the "wrap-around" for us, just like a 12-hour clock wraps around after 12. It gives you the remainder of a division. For example, is 1.

To decrypt, we simply reverse the operation by subtracting the key:

Let's walk through an example. We'll encrypt the word "HELLO" with a key of 3.

  • H: Position 7. . The 10th letter is K.
  • E: Position 4. . The 7th letter is H.
  • L: Position 11. . The 14th letter is O.
  • L: Position 11. . The 14th letter is O.
  • O: Position 14. . The 17th letter is R.

So, "HELLO" becomes "KHOOR".

This image provides a great visual summary of these mathematical formulas and the "HELLO" example.

This infographic shows the encryption and decryption formulas for the Caesar cipher, defines the variables, and illustrates the step-by-step encryption of "HELLO" with a key of 3.

For a more detailed walkthrough of the math, especially how the mod 26 operation works for letters at the end of the alphabet, the following video is very helpful.

Caesar Cipher (Part 1)

This Neso Academy video provides a clear, mathematical breakdown of the Caesar cipher.

First, watch the section that explains how to assign numbers to letters. Then, pay close attention to the explanation of the encryption formula and the decryption formula. Finally, watch the specific example of encrypting the letter 'Y', as it's a perfect demonstration of the mod 26 wrap-around.


Your Turn to Practice

Now it's time to try it yourself. For these exercises, remember that spaces and punctuation are typically left unchanged.

  1. Encrypt the message "SECRET MESSAGE" using a key of 7.
  2. Decrypt the message "AOL JHLZHY JPWOLY" using a key of 7.

Take a moment to work through these on paper using the formulas. When you're ready, check your answers below.


Solutions:

  1. Encryption: "SECRET MESSAGE" with key=7

    • S (18) -> (18 + 7) mod 26 = 25 -> Z
    • E (4) -> (4 + 7) mod 26 = 11 -> L
    • C (2) -> (2 + 7) mod 26 = 9 -> J
    • R (17) -> (17 + 7) mod 26 = 24 -> Y
    • E (4) -> (4 + 7) mod 26 = 11 -> L
    • T (19) -> (19 + 7) mod 26 = 26 mod 26 = 0 -> A
    • M (12) -> (12 + 7) mod 26 = 19 -> T
    • E (4) -> (4 + 7) mod 26 = 11 -> L
    • S (18) -> (18 + 7) mod 26 = 25 -> Z
    • S (18) -> (18 + 7) mod 26 = 25 -> Z
    • A (0) -> (0 + 7) mod 26 = 7 -> H
    • G (6) -> (6 + 7) mod 26 = 13 -> N
    • E (4) -> (4 + 7) mod 26 = 11 -> L
    • Result: "ZLJYLA TLZZHNL"
  2. Decryption: "AOL JHLZHY JPWOLY" with key=7

    • A (0) -> (0 - 7) mod 26 = -7 mod 26 = 19 -> T
    • O (14) -> (14 - 7) mod 26 = 7 -> H
    • L (11) -> (11 - 7) mod 26 = 4 -> E
    • ...and so on.
    • Result: "THE CAESAR CIPHER"

    (A quick tip for modular arithmetic with negative numbers: if you get a negative result like -7, just add 26 to it. -7 + 26 = 19).


Conclusion

In this lesson, you've mastered one of history's most foundational ciphers. We've seen how adding a simple, secret key—the shift value—creates a system far more flexible than the fixed rule of the Atbash cipher.

Key Takeaways:

  • The Caesar cipher is a substitution cipher that shifts each letter of the alphabet by a fixed number of positions.
  • This fixed number is the key.
  • The alphabet "wraps around" (e.g., shifting Z by 1 gives A).
  • The process can be described mathematically using modular arithmetic: for encryption and for decryption.

Preview of the Next Lesson:
The Caesar cipher is just one specific example of a broader category. What if instead of a simple shift, the substitution was completely random? In our next lesson, we will explore this idea by defining the monoalphabetic substitution cipher, the family to which both Atbash and Caesar belong, and see how this opens up a vast new world of possible keys.