A guide to the Caesar Cipher, and coding one in Python

Many people might look at cryptography and see it as complicated, some might hear that there is a lot of maths involved and in turn fear the subject or just lose interest.

Well yes there is a lot of maths involved, I personally admit when I heard maths I straight away started to lose interest. I then stuck at it and in doing so found that cryptography is quite interesting.

In this article I will be; explaining the fundamentals of a Caesar cipher, what it is, how to encrypt and decrypt one, what ROT13 is and finally how to create a program to encrypt and decrypt messages in the Python programming language.

Caesar Cipher:

A Caesar cipher is one of the earliest yet simplest types of encryption. The name Caesar Cipher comes from the fact that it was used by Julius Caesar to communicate in secret between his generals. Encryption and decryption with this cipher is quite easy, this being as all it involves is shifting the characters across the alphabet with a hidden key number.

Lets say we are encrypting a word such as “Hello” and we are using a key number which is six, so to encrypt this to cipher text (cipher text is the encrypted text) we would shift the letters to the right six times.

Example of Encryption:

Alphabet: A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
Text: H-E-L-L-O
Key: 6
Cipher Text: N-K-R-R-U

What we can see above is all 26 letters of the alphabet and where I’ve put the text as Hello and the key as 6. Looking at the alphabet we can move each letter to the right six times and in turn ending up with NLRRU which is HELLO in the cipher text.

Example of Decryption:

Alphabet:  A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
Text: N-L-R-R-U
Key: 20
Cipher Text: H-E-L-L-O

What is ROT13?

ROT13 might sound complicated when you look at it’s name however the only part you need to pay attention to is the number, thirteen, this number is what you encrypt the message with. It’s the most unsecured type of encryption because the encryption and decryption key are exactly the same.

Example of ROT13:

Alphabet: A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
Text: J-O-H-N
Key: 13
Cipher Text: W-B-U-A

Above we follow the same method as before although this time the encryption key and decryption key are the same, 13. Now if you try decrypting it yourself you should get JOHN.

Python Example:

Here we are decrypting the cipher text from the previous example, now normally to do this you would write out underneath the cipher text all possible outcomes, in this example we are brute forcing the result until we get an answer.

So I have explained how you would do it by hand, but what about using another skill such as Python coding to make a program that will encrypt and decrypt the text for us, is it possible? Well luckily for you we’re going to go through a code example with comments so you can take it a step at a time.

Image source: Python

Robert Sterio

I've been studying the field of Cyber Security for some time now and have grown a real passion for it as well as the related networking areas.

You may also like...

  • Great hands on article by Rob. Some interesting examples in there as well.