/
hw1.1.v1.4.1.py
387 lines (245 loc) · 9.74 KB
/
hw1.1.v1.4.1.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Filename: hw1.1.v1.4.1.py
Description: This file generates key matrix,
encrypts the plaintext and decrypts the cipher text with the keyword
Author: Subharthi Banerjee
Ver. : 1.4.2
version information:
Key matrix generates fine
encryption works
decryption works
Dated: 02052016
License: Please use the code freely giving the deserved credit to the authors
"""
import numpy as np
import os
import sys
import string
from collections import OrderedDict
from string import digits
print '\n\n\n\n\n\n\n'
## generates the playfair matrix with this function
def keyPreProcess(key):
key = key.replace(" ", "")
key = key.upper(); #change the key to uppercase
key = ''.join([i for i in key if not i.isdigit()])
keyArray = list(key); # making the key as array
# check if j & i both exists
for i in keyArray:
for j in keyArray:
if (i == 'I') is True & (j == 'J') is True:
print 'J & I both can not coexist in the keyword'
sys.exit(0)
## keeping original order while having the key restored
keyArray = list(OrderedDict.fromkeys(key))
if type(key) is not str:
out = 'Please choose a word as a key next time'
print out
sys.exit(0)
newKey = ''.join([str(x) for x in keyArray])
key = newKey
out = 'You entered key as ' + key
print out;
# upto this point the key is generated with no
# duplication of character
# the keyword is now collected
# Creating the playfair matrix
print '\n============Starting to generate the playfair matrix===========\n'
# create a list from A to Z
matr = list(string.ascii_uppercase)
# join two lists
newKeyArray = keyArray + matr;
newKey = ''.join([str(x) for x in newKeyArray]) # join two lists
keyArray = list(OrderedDict.fromkeys(newKey)) # ordering the characters without duplications
keyArray.remove('J') #removing J to consider I in the key only
print 'Playfair matrix as an array'
print keyArray
n = 5
pfm = [keyArray[i:i+n] for i in range(0, len(keyArray), n)] # 1d to 2D transformation for print
# pyhtonic array print
print '\n\n\n\n\nThe playfair keyword \n\n'
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in pfm]))
return keyArray
########################################################################
########################################################################
# The key is created
# Next is playing with the plaintext
# input is as plain text
# cipher text will be returned
# and viceversa
########################################################################
########################################################################
def recurCheck(text):
plaintext = text
textl = [text[i: i + 1] for i in range(0, len(text), 1)] # it will make the pairs
textf = textl
for i in range(0, len(textl) - 1, 2):
if (textl[i] == textl[i + 1]):
textf.insert(i + 1, 'X');
plaintext = ''.join(textf)
plaintext = recurCheck(plaintext)
break
return plaintext
## invertible encrypt and decrypt function
# if the pairs has duplicate element remove the element with X
# if the last element list is not a pair end with Z
# that is how we get plain text
def encrypt(plaintext, key):
# for cipher
textL = [plaintext[i: i + 2] for i in range(0, len(plaintext), 2)] # it will make the pairs
textl = [plaintext[i: i + 1] for i in range(0, len(plaintext), 1)] # it will make the pairs
print 'plain text before inserting X'
print textL
print '\n\nGenerating actual plain text\n\n'
textf = textl
## put X between the pairs and check recursively
for i in range(0, len(textl) - 1 , 2):
if (textl[i] == textl[i + 1]):
textf.insert(i + 1, 'X');
plaintext = ''.join(textf)
plaintext = recurCheck(plaintext)
break
# check if all the elements are pairable
if (len(plaintext) % 2) is not 0:
c = chr(ord('A') + ((ord(plaintext[len(plaintext) - 1]) - ord('A') + 1)%26))
plaintext = plaintext + c
textL = [plaintext[i: i + 2] for i in range(0, len(plaintext), 2)] # it will make the pairs
textl = [plaintext[i: i + 1] for i in range(0, len(plaintext), 1)] # it will make the ones
print 'Plain text After inserting X'
print textL
#print textL
#######################################################
#######################################################
#encryption starts here
##
#######################################################
key5 = np.reshape(key, (-1, 5))
print 'key in np array form\n\n'
print key5
cipherl = textl
key = key5.tolist()
#print textl
## encyption logic
for i in range (0, len(textl) , 2):
r1, c1 = np.where(key5 == textl[i])
r2, c2 = np.where(key5 == textl[i + 1])
if (r1 == r2 and c1 != c2):
case = 1
if (c1 == 4):
cipherl[i] = key[r1][0]
cipherl[i + 1] = key[r2][c2 + 1]
elif (c2 == 4):
cipherl[i + 1] = key[r2][0]
cipherl[i] = key[r1][c1+1]
else:
cipherl[i] = key[r1][c1 + 1]
cipherl[i + 1] = key[r2][c2 + 1]
elif (c1 == c2 and r1 != r2):
case = 2
if (r1 == 4):
cipherl[i] = key[0][c1]
cipherl[i + 1] = key[r2 + 1][c2]
elif (r2 == 4):
cipherl[i + 1] = key[0][c2]
cipherl[i] = key[r1 + 1][c1]
else:
cipherl[i] = key[r1 + 1][c1]
cipherl[i + 1] = key[r2 + 1][c2]
else:
case = 3
cipherl[i] = key[r1][c2] ##change with the intersection
cipherl[i + 1] = key[r2][c1]
ciphertext = cipherl
return ciphertext
def decrypt(ciphertext, key):
textL = [ciphertext[i: i + 2] for i in range(0, len(ciphertext), 2)] # it will make the pairs
textl = [ciphertext[i: i + 1] for i in range(0, len(ciphertext), 1)] # it will make the pairs
#######################################################
#######################################################
#decryption starts here
##
#######################################################
key5 = np.reshape(key, (-1, 5))
plainl = textl
key = key5.tolist()
## decyption logic
for i in range (0, len(textl) , 2):
r1, c1 = np.where(key5 == textl[i])
r2, c2 = np.where(key5 == textl[i + 1])
if (r1 == r2 and c1 != c2):
case = 1
if (c1 == 0):
plainl[i] = key[r1][4]
plainl[i + 1] = key[r2][c2 - 1]
elif (c2 == 0):
plainl[i + 1] = key[r2][4]
plainl[i] = key[r1][c1 - 1]
else:
plainl[i] = key[r1][c1 - 1]
plainl[i + 1] = key[r2][c2 - 1]
elif (c1 == c2 and r1 != r2):
case = 2
if (r1 == 0):
plainl[i] = key[4][c1]
plainl[i + 1] = key[r2 - 1][c2]
elif (r2 == 0):
plainl[i + 1] = key[4][c2]
plainl[i] = key[r1 - 1][c1]
else:
plainl[i] = key[r1 - 1][c1]
plainl[i + 1] = key[r2 - 1][c2]
else:
case = 3
plainl[i] = key[r1][c2] ##change with the intersection
plainl[i + 1] = key[r2][c1]
plaintext = plainl
return plaintext
def countChar(text):
#first collect all chars
allChars = string.uppercase[:26]
alphaList = [allChars[i: i + 1] for i in range(0, len(allChars), 1)] # it will make the pairs
charFreq = alphaList
for index in range (0, len(alphaList)):
charFreq[index] = text.count(alphaList[index])
return charFreq
def printCountedChar(text):
charFreq = countChar(text)
for i in range(0, 26):
out = chr(ord('A') + i)
print out, charFreq[i]
def main():
key = raw_input("Please enter your key to generate playfair matrix:\t")
keyArray = keyPreProcess(key)
plaintext = raw_input('\n\n\nPlease enter your text what you want to encrypt:\t')
print 'Studying the number of character frequencies-------'
text = plaintext.upper()
text = text.translate(None, digits)
print 'For plaintext -------------\n'
printCountedChar(text)
text = text.replace(" ", "")
text1 = text.translate(string.maketrans("",""), string.punctuation)
print '\n\n\n===================== Starting encryption =====================\n\n\n\n'
plaintext = text1
plaintext = plaintext.replace('J', 'I');
ciphertext = encrypt(plaintext, keyArray)
ciphertext = ''.join(ciphertext)
print 'Ciphertext--------------------\n'
print ciphertext
textL = [ciphertext[i: i + 2] for i in range(0, len(ciphertext), 2)] # it will make the pairs
print '\n\nciphertext inpairs======================\n\n'
print textL
print 'For ciphertext------------------------\n'
printCountedChar(ciphertext)
print 'Starting decryption ---------------------------------\n\n'
print 'Decrypted plaintext- -----\n'
plaintext = decrypt(ciphertext, keyArray)
plaintext = ''.join(plaintext)
print plaintext
textL = [plaintext[i: i + 2] for i in range(0, len(plaintext), 2)] # it will make the pairs
print '\n\nplaintext in pairs======================\n\n'
print textL
return
## Run main
if __name__ == "__main__":
main()