Cracking Code with a Caesar Cipher
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.
- Write out the alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
- Shift the alphabet by 3: DEFGHIJKLMNOPQRSTUVWXYZABC
- 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.
- Write out the shifted alphabet (shifted by 4): EFGHIJKLMNOPQRSTUVWXYZABCD
- Write out the original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
- 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?
- Nothing — Z is the last letter, so it stays Z
- C, because the alphabet wraps round to the start again
- W, by counting three letters backwards
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:
- If you get strange characters, double-check your shift value and make sure you are using the correct encoding/decoding mode.
- Make sure your Python environment is set up correctly.
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.