Coders Packet

Caesar Cipher using Python

By Vishaka Iyengar

Caesar cipher is the simplest and oldest substitution cipher. It is a fundamental part of cryptography. In this project, we shall understand the cipher, view its features and implement it.

The cipher is created by assigning a numerical value is to each alphabet like so. A-0, B-1,..., Z-25. The algorithm can be expressed as follows:

C = E(key, p) = (key + p)mod26.

where C = Cipher Text, E = encryption, p = plain text

Each letter of the plain text is replaced by a corresponding letter standing 'key' paces away. For example, for the plaintext p = meet me after the party, and for a key = 3, the cipher text C would be = PHHW PH DIWHU WKH SDUWB.

Key points:

1. The alphabet is wrapped around. Thus the letter following Z is A.

2. It is an easy cipher to break as there are only 25 possibilities even for a Brute Force Attack.

3. It does not have an equivalent for numeric values and special characters.

4. Crypt analysis is overtly easy as the alphabets in the language have a certain frequency of being used. For example, e is the most commonly used alphabet in the English language. 

 

The code has been explained in detail in the comments present within it.

#Take input PT (plain text) and the key from the user.
ip = input("Enter plain text:")
key  = input("Enter the key: ")
key  = int(key)
#Since the key can be any number, and we need to restrict it between 0-25, take its mod value.The key must be a positive integer value.
key = key % 26
#print("Your new key is ", key)

#Create a dictionary with key-value pairs like so.
#The dictionary, alphabets has Alphabets as keys and the numbers as values.
alphabets = {'A': '0', 'B': '1', 'C': '2', 'D': '3', 'E': '4', 'F': '5', 'G': '6', 'H': '7', 'I': '8', 'J': '9', 'K': '10', 'L': '11', 'M': '12', 'N': '13', 'O': '14', 'P': '15', 'Q': '16', 'R': '17', 'S': '18', 'T': '19', 'U': '20', 'V': '21', 'W': '22', 'X': '23', 'Y': '24', 'Z': '25'}
#The seccond dictionary basically reverses the first one. Thus the keys are now numbers and values are now alphabets. This is used in the second half of the program.
rev_dict_alphabets = {value:key for (key, value) in alphabets.items()}

cipher_text = [] #Create an empty list that will be used to store the encoded plain text.

ip = ip.upper() #converts the entire input into uppercase as python is case sensitive.
for word in ip:
    ip.split()
    #print(word)
def split(ip):
    return [char for char in ip]
#print(split(ip)) #It splits all the words in the sentence into alphabetic form.

for alpha in split(ip):
    h = alpha
    for x in alphabets:
        if x == h:
            #print(x)
            part1 = alphabets[x]
            part1 = int(part1)
            newval = key + part1
            newval = int(newval)
            #print(newval) --- Find the alphabets in the input in the dictionary alphabets. Add the key to their ccorresponding value.

            #The Caesar cipher is a rotating cipher. Thus if the newval exceeds 25 then it must start at 0 again.
            if newval <= 25:
                p = newval
            else:
                p = newval - 26

            #Usin the reversed dictionary, we find the alphabets corresponding to the number generated in p.
            for y in rev_dict_alphabets:
                y = int(y)
                if y == p:
                    #print(y)
                    y = str(y)
                    part2 = rev_dict_alphabets[y]

            #print("Value corresponding to the alphabet is ", part1 )
            #print("The corresponding alphabet to the new value is ", part2)

            cipher_text.append(part2) #Add the alphabets of the cipher text to their list, join them into a string and print it.
CT = ''.join(cipher_text)
print(CT)

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Vishaka Iyengar (reachvishakas)

Download packets of source code on Coders Packet