The Caesar Cipher: A widely used encryption technique
Hey guys! In this post I am gonna give you a detailed explanation on Caesar Cipher and I will help you develop a python code for the same.
The Caesar Cipher is one of the famous and easy way of encrypting your data. It is also called as shift cipher. The main thing which we do in Caesar Cipher is we replace each alphabet with an alphabet with some fixed number of positions down the alphabet. Generally, we do a shift of 3 letters. For ex, We shift left or right by 3 letters. In this post, I will develop a program in which the alphabets are right shifted by 3 letters.
Algorithm:
1. Get the input text from the user.
2. Convert all the letters to lower case.
3. One by one take each character from the input string and start converting them.
4. Convert the characters to number and increment by three
5. If the calculated number exceed by three, then come back to the letter 'a' and again calculate the value.
6. Convert the number to the alphabet.
7. Append all the found characters to a new string.
Python Code (I have used Python 2.7):
"""
Created on Thu Jun 08 12:24:53 2017
@author: Amy's pc
"""
mystr = input("Enter the word:\n")
mystr1 = mystr.lower() # Converts the given string to lower case
outp = "" # A new string declared
for i in mystr1:
s = ord(i) + 3 # Convert each letter to its equivalent number
if (s > ord('z')): # If the number is greater than 122,
s = ord('a') + s - ord('z') - 1 # Calculate the equivallent number by cconsidering the ord('a')
chr1 = chr(s) # Convert the number to chaaracter
outp = outp + chr1 # Append the found characters one by one to the output string
print (outp)
Note:
Ord('a') converts a single alphabet to its equivalent integer code (For ex, its ASCII value)
Chr(115) converts the integer back to its equivalent character.