Cryptography with Chris #1 The Caesar Cipher

Cryptography with Chris #1 The Caesar Cipher

I've been studying a digital cryptography course on Udemy recently. It's pretty good and gives an excellent basis for understanding digital encryption techniques. One of the methods of encryption that it looks at is the classical Caesar method. I'm not just a technologist but a historian as well. So this aspect of the course really fascinated me.

This form of simple cryptography is named after the famous Roman emperor - Julius Caesar, who employed it in his personal correspondence. The Roman historian Suetonius stated that Caesar used a shift of three to protect messages of military significance.

It is a form of substitution cipher where each letter of the original message (which in cryptography is known as plaintext) is replaced with a letter corresponding to a certain number of letters shifted up or down the alphabet.

For each letter of the alphabet, you would take it's position, perhaps 3 for the letter 'C' and shift it by that key number. If we used the example 3 for the letter C, the cipher would be C+3, making every C in the message an F instead.

Using the key of +3, here is a Caesar cipher encryption of a full message:

Easy to Crack

As unreadable as the obfuscated message may appear, this is one of the weakest forms of encryption that can be employed for the following reasons :

- The key space is very small. Using a brute force attack method, an individual could easily try all 25 possible combinations to decrypt the message without initially knowing the key.

- The structure of the plaintext remains intact. This makes the encryption vulnerable to frequency analysis. This works by looking at how often certain characters appear and then substituting them with commonly occurring letters in the language in which the plaintext message is written.

Encryption / Decryption Formula

The above image shows how the cipher can be expressed as a mathematical formula. This means that the encryption of a letter (expressed by x) is equal to a shift of x+n (where n is the key value shift). The result of the process is then taken under a modulo division, using 26 as if the letter is shifted past the alphabet it wraps around to the beginning.

Decryption would then subtract the shift from the letter, leaving the original letter in place.

Implementing the Cipher in Python

Part of the course that I am studying looks at how to encrypt and decrypt these cipher methodologies in the Python language. To encrypt the message, the following code is used :

For decyrption this function is used:

The main program looks like :

So looking through the code, we can see that we define the alphabet as our variable key. We then build our encryption function which checks if the value exists in our alphabet and if it doesn't then we simply output that character instead of the encrypted character. This will allow us to use punctuation and spacing etc.

We apply our algorithm to each letter of the plaintext and return a new concatenated string as our ciphertext. To decrypt this, we simply do the opposite (subtracting the rotation) and we get our result.

Running the program by using the example here:


We wish to encrypt this message and use a key of 5

As you can see it works incredibly well. It's very easy to implement and therefore incredibly easy to crack. Python even includes the Caesar cipher in it's standard library called rot13, which translates to rotate 13 defining how the key works.



To view or add a comment, sign in

More articles by Dr Chris McAuley

Explore content categories