How the Ceasar cipher works?
The Caesar cipher, also called Caesar's code, shift cipher or Caesar shift, is one of the simplest and most known forms of encryption. It is a substitution cipher where each letter in the plaintext (original message) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet creating a cipher text.
Caesar cipher, due to the fact that the key is very small is vulnerable to the brute force attack, where attacker has to try only 25 combinations in order to decrypt the message. In addition, the original structure of the plaintext remains intact, so a person trying to decrypt the message can perform a frequency analysis - method to decrypt by analysing the frequency of each letter and comparing it with the frequency of each letter in certain alphabet. For example in English alphabet letter e is the most common one, and by comparing the most common letter in the cipher with letter e we can try to discover the shift that was used. Read more at:
History behind Caesar Cipher
This cipher is named after Julius Caesar, who used it to protect his communication with generals during military campaigns. According to Suetonius, who wrote a book about life of the Roman emperor, Julius used the substituation cipher to a shift of three, meaning shifted each letter 3 places through the alphabet where D becomes A and so on.
If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out. If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others.
Suetonius, Life of Julius Caesar 56
Programming Caesar cipher tools
Great way to learn how this cipher works is to create your own tools to encrypt, decrypt or even brute force the message. You can do this in any programming language you know, but I'm gonna stick with Python as it's my favourite one.
Firsty let's create a function to encrypt the message with a certain shift.
Firstly we declare a variable key that is English aplhabet. Adding the algorith to check if the character exists in our alphabet will allow us to use punctuation, spaces without encrypting them.
Now let's add to this main function that will use function encrypt and will display the results.
And the last step: providing input for the message and the key.
Great! It works and the message is encrypted.
But what if we get the encrypted message from a friend, with a provided key?
Let's create a decrypt script!
As you can see it's mostly the same as the encrypt script. Let's test this!
Great! We decrypted the secret message! But what if we don't have a key but our curiosity won't let us leave this message unread. We can try to analyse frequency of letters but it may take some time. So why not to try and brute force it?
This script is not as advanced as the previous ones becouse as you'll see it 'eats' the spaces but it is capable of trying all combinations so let's give it a try.
As always I hoped you enjoyed exploring this topic with me and it brough you some clarity about how the ciphers worked back then, how to write encoding, decoding and brute forcing scripts using shift cipher. In the next part of Let's Talk Encryption series we're going to explore more present ciphers.
Check out my newest Caesar tool at Github!
Keep learning and stay safe!