playfair cipher python

Solutions on MaxInterview for playfair cipher python by the best coders in the world

showing results for - "playfair cipher python"
Romina
22 Jan 2016
1key=input("Enter key")
2key=key.replace(" ", "")
3key=key.upper()
4def matrix(x,y,initial):
5    return [[initial for i in range(x)] for j in range(y)]
6    
7result=list()
8for c in key: #storing key
9    if c not in result:
10        if c=='J':
11            result.append('I')
12        else:
13            result.append(c)
14flag=0
15for i in range(65,91): #storing other character
16    if chr(i) not in result:
17        if i==73 and chr(74) not in result:
18            result.append("I")
19            flag=1
20        elif flag==0 and i==73 or i==74:
21            pass    
22        else:
23            result.append(chr(i))
24k=0
25my_matrix=matrix(5,5,0) #initialize matrix
26for i in range(0,5): #making matrix
27    for j in range(0,5):
28        my_matrix[i][j]=result[k]
29        k+=1
30
31def locindex(c): #get location of each character
32    loc=list()
33    if c=='J':
34        c='I'
35    for i ,j in enumerate(my_matrix):
36        for k,l in enumerate(j):
37            if c==l:
38                loc.append(i)
39                loc.append(k)
40                return loc
41            
42def encrypt():  #Encryption
43    msg=str(input("ENTER MSG:"))
44    msg=msg.upper()
45    msg=msg.replace(" ", "")             
46    i=0
47    for s in range(0,len(msg)+1,2):
48        if s<len(msg)-1:
49            if msg[s]==msg[s+1]:
50                msg=msg[:s+1]+'X'+msg[s+1:]
51    if len(msg)%2!=0:
52        msg=msg[:]+'X'
53    print("CIPHER TEXT:",end=' ')
54    while i<len(msg):
55        loc=list()
56        loc=locindex(msg[i])
57        loc1=list()
58        loc1=locindex(msg[i+1])
59        if loc[1]==loc1[1]:
60            print("{}{}".format(my_matrix[(loc[0]+1)%5][loc[1]],my_matrix[(loc1[0]+1)%5][loc1[1]]),end=' ')
61        elif loc[0]==loc1[0]:
62            print("{}{}".format(my_matrix[loc[0]][(loc[1]+1)%5],my_matrix[loc1[0]][(loc1[1]+1)%5]),end=' ')  
63        else:
64            print("{}{}".format(my_matrix[loc[0]][loc1[1]],my_matrix[loc1[0]][loc[1]]),end=' ')    
65        i=i+2        
66                 
67def decrypt():  #decryption
68    msg=str(input("ENTER CIPHER TEXT:"))
69    msg=msg.upper()
70    msg=msg.replace(" ", "")
71    print("PLAIN TEXT:",end=' ')
72    i=0
73    while i<len(msg):
74        loc=list()
75        loc=locindex(msg[i])
76        loc1=list()
77        loc1=locindex(msg[i+1])
78        if loc[1]==loc1[1]:
79            print("{}{}".format(my_matrix[(loc[0]-1)%5][loc[1]],my_matrix[(loc1[0]-1)%5][loc1[1]]),end=' ')
80        elif loc[0]==loc1[0]:
81            print("{}{}".format(my_matrix[loc[0]][(loc[1]-1)%5],my_matrix[loc1[0]][(loc1[1]-1)%5]),end=' ')  
82        else:
83            print("{}{}".format(my_matrix[loc[0]][loc1[1]],my_matrix[loc1[0]][loc[1]]),end=' ')    
84        i=i+2        
85
86while(1):
87    choice=int(input("\n 1.Encryption \n 2.Decryption: \n 3.EXIT"))
88    if choice==1:
89        encrypt()
90    elif choice==2:
91        decrypt()
92    elif choice==3:
93        exit()
94    else:
95        print("Choose correct choice")
Davide
10 Feb 2016
1Play fai cipher python
Giulio
27 May 2018
1Play fair cipher python