Imagine, that you and your friend traveled several thousand years into the past. Somehow, you ended up in different cities, and it is extremely difficult for any of you to go to another place. Of course, you do not have any smartphones, messengers, and even e-mail. So, in order to communicate, you need to send letters. However, it is quite unsafe, as someone can read your letter out of curiosity and learn about your secret (that you are from the future, for example). What can you do in order to avoid that? Use a cipher, of course!
Algorithm
One of the easiest methods, that was actually created for the same purpose – to make correspondence incomprehensible for the outsiders – is the Caesar cipher.
It is extremely simple: in order to encode or decode information you need to know only one number – a shift. A shift determines how many times you need to move the position of the letter in the alphabet in order to encode or decode it. For example, in a cipher with shift 2, the letter "A" turns into "C", "B" – into "D", etc.
Knowing the shift, everything you need to do in order to encode your message is to encode each letter of it. It is done by moving the position of a letter right times. You can use the following formula in order to easily compute the new position of a letter:
Meaning that in this and all of the following formulas and pieces of code, is the length of the alphabet.
means that you take the remainder of dividing by .
For example, for the letter "A" of the English alphabet and the new position will be the following:
, which is the position of the letter "K".
You can decode the letter by moving its position left times or by using the following formula:
You may wonder, why you need in these formulas. The reason is that in some cases you need to go back to the beginning of the alphabet while moving the position of a letter. For instance, you may want to encode the letter "Y" with shift 3. In this case, you need to move back to the letters "A" and "B", which are the first two letters of the alphabet:
Example
So, imagine once again that you traveled to the past with your friend. You decided to use Caesar cipher with shift 3 to encode your messages. One morning you get a letter with the following text: "Gr brx zdqw wr uhwxuq wr wkh ixwxuh?" Let's try to decode it!
To make the process easier, you can use the table below, which has not only the letters but also their positions in the alphabet.
The first one is the letter "D". In order to decode it, you need to move the position left 3 times, as shift equals 3 in our example.
As you can see, the first letter in the decoded message is going to be "D".
Let's move on to the next letter – "R", but this time we will use a formula to get the position of the letter. So, as you can see from the above, the position of the letter "R" is 17, and the length of the alphabet is 26.
Now you need to find the letter at position 14, which turns out to be "O".
In order to make the process easier, you may write code for decoding the message. Let's take a look at the pseudocode:
class CaesarCipher is
method decodeMessage(message: String, shift: Integer, n: Integer) is
decoded = "" // string variable for storing the decoded message
for letter in message do
oldPosition = n + position(letter) - shift // the position of a letter before it was encoded
decoded += char(oldPosition % n) // adding a decoded letter to the resulting variable
return decoded
You can choose either way to decode the rest of the letter. After you do it, you will be able to understand, what your friend wanted to tell you – "Do you want to return to the future?".
Now, let's send the answer. It is nice to change your surroundings once in a while, but there is no place like home, right? So, let's write back something like this: "Of course! When can we do it?"
The first letter is "O". The shift equals 3, so it is encoded into "R".
The next one is "F". Let's encode it using the formula. The position of this letter, as it can be seen from the alphabet above, is 5, so the position of the new letter is the following:
Therefore, "F" is encoded into "I".
Just like for decoding the message, you can use code in order to encode it. The pseudocode for this task looks like this:
class CaesarCipher is
method encodeMessage(message: String, shift: Integer, n: Integer) is
encoded = "" // a string variable for storing the encoded message
for (letter in message) do
newPosition = position(letter) + shift // a position of a letter after it was encoded
encoded += char(newPosition % n) // adding an encoded letter to the resulting variable
return encoded
After performing the steps manually or running the code, you will get the following encoded message: "Ri frxuvh! Zkhq fdq zh gr lw?"
Variations
The simpleness of this cipher makes it easy not only to understand but also to break. Even without knowing the shift you can decode the message, as there is a finite number of values the shift can be equal to.
Therefore, there are a lot of variations, implemented in order to make it more difficult. One of them is changing the shift every time. Another one is using different shifts for every letter.
Additionally, Caesar cipher is mostly used as a part of other more complicated ciphers. One of the possible examples of such is Vigenère cipher.
Conclusion
All in all, here are the main points you need to remember about the Caesar cipher:
- It was created many years ago in order to make correspondence incomprehensible to outsiders.
- The main idea behind the algorithm is to replace all of the letters with the ones, which positions in the alphabet differ from theirs by a certain number.
- It is very simple to break, which is why it is used mainly as a part of other ciphers and not on its own.
- One of the ciphers, based on the Caesar cipher, is the Vigenère cipher.