Caesar Cipher Algorithm in Python
Table of Contents
The Caesar Cipher can also be called Shift Cipher, Substitution Cipher, Caesar’s Code, or Caesar’s Shift. This is going to be a short tutorial on how to create a Caesar Cipher in Python.
What is Caesar Cipher? #
Caesar Cipher is a type of substitution cipher in which each letter in the plaintext is shifted a certain number of places down or up the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
A Brief History of Caesar Cipher #
This is the first and well-known cipher that was used by Julius Caesar. It was said to have been developed around 100 BC. He shifted each letter in his messages in order to make the messages meaningless should the enemy intercept them. Although the shift key varies, he originally used it with a shift of three. Therefore A becomes D when encrypting, and D becomes A when decrypting. A diagrammatic representation of the cipher system that uses a left shift value of 3 is shown below.
Implementation in Python #
In order to make our cipher more flexible, we would allow the user to determine the shift value. I encountered this problem when I was trying to solve an assessment question. Below is the function
Next, I am going to explain what is happening on every line
- In line 1, we create the function
- In line 3, we used the ordinal(ASCII) value of lowercase a to z alphabet in a list comprehension.
- In line 4, we converted the input string to lowercase in order to match with the list of lowercase alphabets that we created
- In line 6, we create an empty string to store the ciphered string
- In line 7, we loop through the input string that we converted to lowercase
- In line 8, we determine the position of the current alphabet
- In line 9, we get the offset position by subtracting the shift value(offset) from where the alphabet is located
- In line 10, the ciphered alphabet will be the value of the position where the shift has landed us
- In line 11, we add the ciphered alphabet to the ciphered string
- In line 12, we return the ciphered string
So, we are not only restricted to the left shift offset of 3, we can use any value we like.
Note: The Caesar Cipher is not a secure encryption method. It can be easily broken even by hand. It is just a fun way to learn about encryption and decryption.