Crack the Code! Make a Caesar Cipher
If you need to send a secret message to a friend, how could you prevent other people from reading it? One way is to encrypt the message, or use a secret code that only you and your friend know. Try this activity to learn how to create your own "Caesar cipher," a popular type of code that is easy to learn.
Cryptography is the study of writing or solving secret codes that are used for secure communication. Historically, codes have been used by politicians, spies, and countries at war to prevent their enemies from knowing what they're up to. Many of the earliest codes, or "ciphers," like the one you will create in this project, were easy to create by hand. However, modern cryptography is essential in computer science for keeping everything from emails to bank account information secure.
One of the earliest and most widely known ciphers is the Caesar Cipher, named after Julius Caesar. It is a simple form of a "substitution cipher" where you replace each letter of the alphabet with another letter by shifting the whole alphabet a certain number of letters (wrapping around to the beginning once you reach the end). For example, if you shift each letter by 3 spaces, then the alphabet looks like this:
So, when you write your message, the letter "A" gets replaced with "X", "B" gets replaced with "Y", and so on. For example, the word "hello" becomes:
In order to decode your message, you need to share the "key" (the number 3) with your friend first. After that, you can send messages that are written in cipher, so other people can't read them!
- Pencil and paper
- At least one other person
- Explain the concept of a Caesar cipher to a friend, or have them read the background section of this activity.
- Write down the alphabet from A to Z.
- Pick a number from 1 to 25 (if you use 26, you will just wind up with the original alphabet). This number is your key.
- Shift the entire alphabet by the number you picked and write it down below your original alphabet (as shown in the Background section).
- Pick a message to write to your friend. It might be easiest to start out with a simple message (like a single word or phrase) before you try longer sentences or paragraphs.
- Write down your encoded message using your shifted alphabet. If it helps, write down your plain-text message first, then encode it one letter at a time (like the "hello" example in the background). Just make sure the piece of paper you give your friend only has the encoded message!
- Give your friend the encoded message and tell them the key (don't write the key down – that's like writing down your computer password, a big security no-no!).
- See if your friend can decrypt your message. If it helps for the first try, let them work backwards using the original and shifted alphabets you wrote down. Using the example from the background, the letter "x" becomes "a", "y" becomes "b", and so on.
Extra: try finding a third person who does not know what a Caesar cipher is. Can they crack your code if they "intercept" your message?
Extra: what if the person who intercepts your message knows about Caesar ciphers? Does that make it easier to crack the code? Since there are only 25 possible keys, Caesar ciphers are very vulnerable to a "brute force" attack, where you simply try each possible combination of letters. This might take some patience for a human to do, but modern computers can do that in a fraction of a second, so Caesar ciphers are not considered a secure method to encrypt electronic communications.
Extra: another way to crack the Caesar cipher is "frequency analysis." Frequency analysis is based on the fact that, in natural English speech and writing, certain letters appear much more frequently than others. For example, the letter "e" appears more often than any other letter, and "z" appears the least often (if you have ever played the board game Scrabble®, you may notice that this determines how many points letters are worth!). So, for example, if you read an entire paragraph and notice that the letter "d" appears more often than any other letter, odds are that it used a Caesar cipher with a shift of 1. This technique will be more accurate for longer blocks of text and very inaccurate for short words or phrases, since there are plenty of words that do not contain the letter "e" at all. Can you have a friend write an entire paragraph with a Caesar cipher and then try to crack it using frequency analysis?
Extra: if you plan to use the Caesar cipher for regular communication, one risk is that eventually someone will discover your key. You can help prevent this by changing the key, for example using a new one every week. This is a similar concept to periodically changing your computer passwords.
Extra: the Caesar cipher is just one type of substitution cipher. Look up some other types of substitution ciphers and try them out. Are they harder or easier to use and crack?
Observations and Results
Once you and your friend both understand how to use a Caesar cipher, it should be relatively easy to send encrypted communications to each other. This can be a fun way to pass secret messages back and forth between friends. However, while the Caesar cipher provides a great introduction to cryptography, as discussed above, in the era of modern computers it is no longer a secure way to send encrypted communications – so please don't try to use it for any personal information online (like your address or bank account information)!
Ask an Expert
- Basics of Cryptography: Caesar Cipher, from Instructables
- Science Activities for All Ages!, from Science Buddies