-
Notifications
You must be signed in to change notification settings - Fork 0
/
playfair.py
194 lines (144 loc) · 3.52 KB
/
playfair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import string
plain_text = input("enter plain text: ")
key = input("enter key: ")
print()
print ("name : " + plain_text)
print()
print ("roll no : " + key)
print()
singleCharsInKey = []
for i in key:
if i not in singleCharsInKey:
singleCharsInKey.append(i)
else:
pass
#print singleCharsInKey
alphabets = list(string.ascii_lowercase)
#print alphabets
for i in singleCharsInKey:
for j in alphabets:
if i == j:
alphabets.remove(j)
break
alphabets.remove('b')
#print alphabets
matrixelements = []
for i in singleCharsInKey:
matrixelements.append(i)
for j in alphabets:
matrixelements.append(j)
#print matrixelements
matrix = []
posKey = 0
for i in range(1,6):
matrixElement = []
for j in range(1,6):
matrixElement.append(matrixelements[posKey])
posKey += 1
matrix.append(matrixElement)
#print matrix
plain_text_list = list(plain_text)
#print plain_text_list
if len(plain_text_list)%2:
plain_text_list.append('x')
charadded = 1
#print plain_text_list
correct_list = []
i = 0
while i < len(plain_text_list):
if plain_text_list[i] == plain_text_list[i+1]:
#print "double"
correct_list.append(plain_text_list[i])
correct_list.append("x")
correct_list.append(plain_text_list[i+1])
else:
correct_list.append(plain_text_list[i])
correct_list.append(plain_text_list[i+1])
#print i
i += 2
if len(correct_list)%2:
correct_list.append('x')
i=0
ciphertext = ""
while i < len(correct_list):
letter1 = correct_list[i]
letter2 = correct_list[i+1]
i+=2
letter1pos=[]
letter2pos=[]
temp1=""
temp2=""
for l in range(len(matrix)):
for j in range(5):
if matrix[l][j] == letter1:
letter1pos.append(l)
letter1pos.append(j)
if matrix[l][j] == letter2:
letter2pos.append(l)
letter2pos.append(j)
#print letter1pos,letter2pos
if letter1pos[0]==letter2pos[0]:
#print"row same"
temp1 = (letter1pos[1]+1)%5
temp2 = (letter2pos[1]+1)%5
#print temp1,temp2
ciphertext += matrix[letter1pos[0]][temp1]
ciphertext += matrix[letter1pos[0]][temp2]
#print ciphertext
elif letter1pos[1]==letter2pos[1]:
#print"col same"
temp1 = (letter1pos[0]+1)%5
temp2 = (letter2pos[0]+1)%5
#print temp1,temp2
ciphertext += matrix[temp1][letter1pos[1]]
ciphertext += matrix[temp2][letter2pos[1]]
#print ciphertext
else:
#print"both diff"
ciphertext += matrix[letter1pos[0]][letter2pos[1]]
ciphertext += matrix[letter2pos[0]][letter1pos[1]]
#print ciphertext
print ("cipher text : " + ciphertext)
d_list = list(ciphertext)
i=0
dtext = ""
while i < len(correct_list):
letter1 = d_list[i]
letter2 = d_list[i+1]
i+=2
letter1pos=[]
letter2pos=[]
temp1=""
temp2=""
for l in range(len(matrix)):
for j in range(5):
if matrix[l][j] == letter1:
letter1pos.append(l)
letter1pos.append(j)
if matrix[l][j] == letter2:
letter2pos.append(l)
letter2pos.append(j)
#print letter1pos,letter2pos
if letter1pos[0]==letter2pos[0]:
#print"row same"
temp1 = (letter1pos[1]-1)%5
temp2 = (letter2pos[1]-1)%5
#print temp1,temp2
dtext += matrix[letter1pos[0]][temp1]
dtext += matrix[letter1pos[0]][temp2]
#print ciphertext
elif letter1pos[1]==letter2pos[1]:
#print"col same"
temp1 = (letter1pos[0]-1)%5
temp2 = (letter2pos[0]-1)%5
#print temp1,temp2
dtext += matrix[temp1][letter1pos[1]]
dtext += matrix[temp2][letter2pos[1]]
#print ciphertext
else:
#print"both diff"
dtext += matrix[letter1pos[0]][letter2pos[1]]
dtext += matrix[letter2pos[0]][letter1pos[1]]
if charadded == 1:
dtext = dtext[:-1]
print("decrypted text:",dtext)