BrightKidz Library
Subjects
A cipher wheel, and a message with every letter slid along A round dial on the left made of an outer ring of notches and an inner disc of notches, with a pointer at the top and a curved arrow showing the inner disc has been turned. On the right a row of six blocks each carries a different mark. Below it the same six marks appear again, but every one has slid along one place, joined to where it started by a dotted line.

Cracking Code with a Caesar Cipher

About 5 minutes

A Caesar Cipher hides a message by shifting every letter of the alphabet by the same amount — shift by 3, and A becomes D, B becomes E, and so on. That shift is the key, and it is what keeps the message secret.

What is a Caesar Cipher?

Imagine the alphabet as a circle. A Caesar Cipher shifts each letter the same number of places around it.

Encoding a Message

Let's encode the message "HELLO" with a shift of 3.

  1. Write out the alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  2. Shift the alphabet by 3: DEFGHIJKLMNOPQRSTUVWXYZABC
  3. Find each letter in your message in the original alphabet and replace it with the corresponding shifted letter.

So "HELLO" becomes "KHOOR".

Decoding a Message

Now, let's decode the message "Lipps Asvph" that was encoded with a shift of 4.

  1. Write out the shifted alphabet (shifted by 4): EFGHIJKLMNOPQRSTUVWXYZABCD
  2. Write out the original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  3. Find each letter in the encoded message in the shifted alphabet and replace it with the corresponding original letter.

So "Lipps Asvph" becomes "Hello World".

What happens at the end of the alphabet?

Look at the shifted alphabet again: DEFGHIJKLMNOPQRSTUVWXYZABC. The last three letters have wrapped round to the front, which is what "imagine the alphabet as a circle" really means.

With a shift of 3, what does Z turn into?

Try it yourself

Here is a Caesar cipher encoder and decoder written in Python.

def caesar_cipher(text, shift, mode):
    result = ''
    for char in text:
        if char.isalpha():
            start = ord('a') if char.islower() else ord('A')
            shifted_char = chr((ord(char) - start + shift) % 26 + start) if mode == 'encode' else chr((ord(char) - start - shift) % 26 + start)
        elif char.isdigit():
            shifted_char = str((int(char) + shift) % 10) if mode == 'encode' else str((int(char) - shift) % 10)
        else:
            shifted_char = char
        result += shifted_char
    return result

# Example usage
message = "Hello, World! 123"
key = 3

encoded_message = caesar_cipher(message, key, 'encode')
print(f"Encoded: {encoded_message}") # Output: Khoor, Zruog! 456

decoded_message = caesar_cipher(encoded_message, key, 'decode')
print(f"Decoded: {decoded_message}") # Output: Hello, World! 123

Troubleshooting:

The shift number is the whole secret. Anyone without it has to try all 25 possible shifts before they find your message.

A cipher wheel unrolled into two strips of the alphabet. Turn it and every letter moves the same distance at once — there is no way to move just one. The first message hands you its key; the second gives you nothing but 25 keys to try. The third needs no trying at all: one letter in it stands alone, so it can only be A or I, and that single fact shuts 23 of the 25 doors.