Implementation of Playfair Cipher: Experiment-3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EXPERIMENT-3

IMPLEMENTATION OF PLAYFAIR CIPHER

Name: Sonam Gupta (201903015)


Roll no.: 14
Date of Performance: 16/07/2021
Date of Submission: 05/08/2021

Playfair Cipher:
Aim: To write a python program for the implementation of encryption
and decryption of playfair ciphering technique.
Theory:
The Playfair cipher starts with creating a key table. The key table is a 5×5
grid of letters that will act as the key for encrypting your plaintext. Each
of the 25 letters must be unique and one letter of the alphabet is omitted
from the table (as there are 25 spots and 26
letters in the alphabet.

To encrypt a message, one would break the message into digrams (groups
of 2 letters)
such that, for example, "HelloWorld" becomes "HE LL OW OR LD",
and map them out on
the key table. The two letters of the diagram are considered as the
opposite corners of a
rectangle in the key table. Note the relative position of the corners of this
rectangle. Then
apply the following 4 rules, in order, to each pair of letters in the
plaintext:

1. If both letters are the same (or only one letter is left), add an "X" after
the first letter
2. If the letters appear on the same row of your table, replace them with
the letters to
their immediate right respectively
3. If the letters appear on the same column of your table, replace them
with the letters
immediately below respectively
4. If the letters are not on the same row or column, replace them with the
letters on the
same row respectively but at the other pair of corners of the rectangle
defined by the
original pair.

1
Algorithm:
1.Start
2. Remove any punctuation or characters that are not present in the key
square (this may mean spelling out numbers, punctuation etc.).
3. Identify any double letters in the plaintext and replace the second
occurence with an 'x' e.g. 'hammer' -> 'hamxer'.If the plaintext has an odd
number of characters, append an 'x' to the end to make it even.
4. Break the plaintext into pairs of letters, e.g. 'hamxer' -> 'ha mx er'
5.The algorithm now works on each of the letter pairs.
6.Locate the letters in the key square, (the examples given are using the
key square above)
a. If the letters are in different rows and columns, replace the pair with the
letters on the same row respectively but at the other pair of corners of the
rectangle defined by the original pair. The order is important – the first
encrypted letter of the pair is the one that lies on the same row as the first
plaintext letter. 'ha' -> 'bo', 'es' -> 'il'
b. If the letters appear on the same row of the table, replace them with the
letters to their immediate right respectively (wrapping around to the left
side of the row if a letter in the original pair was on the right side of the
row). 'ma' -> 'or', 'lp' -> 'pq'
c.If the letters appear on the same column of the table, replace them with
the letters immediately below respectively (wrapping around to the top
side of the column if a letter in the original pair was on the bottom side of
the column). 'rk' -> 'dt', 'pv' -> 'vo'
7.End.

2
Code:
key=input("Enter key: ")
key=key.replace(" ", "")
key=key.upper()
def matrix(x,y,initial):
return [[initial for i in range(x)] for j in range(y)]

result=list()
for c in key: #storing key
if c not in result:
if c=='J':
result.append('I')
else:
result.append(c)
flag=0
for i in range(65,91): #storing other character
if chr(i) not in result:
if i==73 and chr(74) not in result:
result.append("I")
flag=1
elif flag==0 and i==73 or i==74:
pass
else:
result.append(chr(i))
k=0
my_matrix=matrix(5,5,0) #initialize matrix
for i in range(0,5): #making matrix
for j in range(0,5):
my_matrix[i][j]=result[k]
k+=1

def locindex(c): #get location of each character


loc=list()
if c=='J':
c='I'
for i ,j in enumerate(my_matrix):
for k,l in enumerate(j):
if c==l:
loc.append(i)
loc.append(k)
return loc

def encrypt(): #Encryption

3
msg=str(input("ENTER MSG: "))
msg=msg.upper()
msg=msg.replace(" ", "")
i=0
for s in range(0,len(msg)+1,2):
if s<len(msg)-1:
if msg[s]==msg[s+1]:
msg=msg[:s+1]+'X'+msg[s+1:]
if len(msg)%2!=0:
msg=msg[:]+'X'
print("CIPHER TEXT: ",end=' ')
while i<len(msg):
loc=list()
loc=locindex(msg[i])
loc1=list()
loc1=locindex(msg[i+1])
if loc[1]==loc1[1]:
print("{}{}".format(my_matrix[(loc[0]+1)%5]
[loc[1]],my_matrix[(loc1[0]+1)%5][loc1[1]]),end=' ')
elif loc[0]==loc1[0]:
print("{}{}".format(my_matrix[loc[0]]
[(loc[1]+1)%5],my_matrix[loc1[0]][(loc1[1]+1)%5]),end=' ')
else:
print("{}{}".format(my_matrix[loc[0]]
[loc1[1]],my_matrix[loc1[0]][loc[1]]),end=' ')
i=i+2

def decrypt(): #decryption


msg=str(input("ENTER CIPHER TEXT: "))
msg=msg.upper()
msg=msg.replace(" ", "")
print("PLAIN TEXT: ",end=' ')
i=0
while i<len(msg):
loc=list()
loc=locindex(msg[i])
loc1=list()
loc1=locindex(msg[i+1])
if loc[1]==loc1[1]:
print("{}{}".format(my_matrix[(loc[0]-1)%5]
[loc[1]],my_matrix[(loc1[0]-1)%5][loc1[1]]),end=' ')
elif loc[0]==loc1[0]:

4
print("{}{}".format(my_matrix[loc[0]][(loc[1]-
1)%5],my_matrix[loc1[0]][(loc1[1]-1)%5]),end=' ')
else:
print("{}{}".format(my_matrix[loc[0]]
[loc1[1]],my_matrix[loc1[0]][loc[1]]),end=' ')
i=i+2

while(1):
choice=int(input("\n 1.Encryption \n 2.Decryption: \n 3.EXIT\n"))
if choice==1:
encrypt()
elif choice==2:
decrypt()
elif choice==3:
print("Exiting...")
break
else:
print("Choose correct choice")
Output:
Enter key: crypto
1.Encryption
2.Decryption:
3.EXIT
1
ENTER MSG: Xie is best
CIPHER TEXT: PQ DK NE KZ PZ
1.Encryption
2.Decryption:
3.EXIT
2
ENTER CIPHER TEXT: PQ DK NE KZ PZ
PLAIN TEXT: XI EI SB ES TX
1.Encryption
2.Decryption:
3.EXIT
3
Exiting...

Conclusion: By doing this experiment I get to know that how practical


implementation of coversion from plain text to cipher text is done using
playfair ciphering technique.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy