[go: up one dir, main page]

0% found this document useful (0 votes)
61 views3 pages

Css Exp 3

The document describes an experiment to implement the Playfair cipher encryption technique. It defines functions to generate a 5x5 key matrix from a keyword, locate characters within the matrix, encrypt messages by applying the cipher to pairs of characters, and includes sample code and output.

Uploaded by

ritzinator24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

Css Exp 3

The document describes an experiment to implement the Playfair cipher encryption technique. It defines functions to generate a 5x5 key matrix from a keyword, locate characters within the matrix, encrypt messages by applying the cipher to pairs of characters, and includes sample code and output.

Uploaded by

ritzinator24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT 3

Aim: To implement Playfair cipher

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


msg=str(input("Enter Message: "))
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: \n",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

encrypt()
def print_matrix(mat):
for row in mat:
print(" ".join(row))

# Print the Playfair Cipher matrix


print("\nPlayfair Cipher Matrix:")
print_matrix(my_matrix)

OUTPUT:

Enter key: RITTIKA


Enter Message: ARSENAL FC
CIPHER TEXT:
RI YM UF ND DW
Playfair Cipher Matrix:
RITKA
BCDEF
GHLMN
OPQSU
VWXYZ
Shift : 3
Cipher: ULWWLND

You might also like