-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_playfair_cipher.py
426 lines (331 loc) · 16.7 KB
/
Test_playfair_cipher.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from Crypto.Playfair_cipher import *
# Playfair Cipher Tests:
def test_verify_input_plaintext(plaintext, key):
returned_values = verify_input_plaintext(plaintext, key)
if not returned_values:
print("Error (keyword is missing) in", verify_input_plaintext.__name__)
return False
for value in returned_values:
for char in value:
if char.isspace() or char.isupper():
print(verify_input_plaintext.__name__, "function returned a character that is not supported."
"Value with error:", char)
print("Returned values:", returned_values)
return False
return True
def test_verify_input_ciphertext(ciphertext, keyword):
returned_values = verify_input_plaintext(ciphertext, keyword)
if not returned_values:
print("Error in", verify_input_ciphertext.__name__)
return False
for value in returned_values:
for char in value:
if char.isspace() or char.isupper():
print(verify_input_ciphertext.__name__, "function returned a character that is not supported."
"Value with error:", char)
print("Returned values:", returned_values)
return False
return True
# TODO: Add encryption parameters
def test_playfair_cipher(text_in_pairs, key_table, decrypt=False):
cipher_in_pairs = playfair_cipher(text_in_pairs, key_table, decrypt)
if not cipher_in_pairs:
# Error handling inside the method
return False
if len(cipher_in_pairs) != len(text_in_pairs):
print("ERROR: On function", test_playfair_cipher.__name__, end=". ")
print("The returned list is not of the same size as the input.")
print("text_in_pairs:", text_in_pairs)
print("cipher_in_pairs:", cipher_in_pairs)
return False
return True
def test_make_key_table(key):
ALPHABET = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
table = make_key_table(key)
# print(table)
# The table must be 5x5 and contain only lower cases letters
if len(table) != 5:
print(make_key_table.__name__, "function is returning a list that is not 5x5 in size.")
return False
for row in table:
if len(row) != 5:
print(make_key_table.__name__, "function is returning a list that is not 5x5 in size.")
return False
for char in row:
if not char.isalpha() or not char.islower():
print(make_key_table.__name__, "function is returning a non letter character of an upper case letter."
"Value with error:", row)
return False
try:
ALPHABET.remove(char)
except ValueError:
print("ERROR in function", make_key_table.__name__, "\nThe letter", char, "appeared more than once.")
print("Table:")
for i in table:
print(i)
return False
if len(ALPHABET) == 0 or not (len(ALPHABET) == 1 and ALPHABET[0] == 'j'):
print(ALPHABET)
print("ERROR: The table returned from the", make_key_table.__name__, "function doesn't fit the whole alphabet.")
return False
# Also, the table must contain every character of the key
# I've seen this code for the same purpose and I want to test it later:
# result = all(elem in list1 for elem in list2)
for char in key:
found = False
for row in table:
if char in row:
found = True
if not found:
print("ERROR:", make_key_table.__name__, "function is returning a table that doesn't contain every letter"
"of the key. Missing character:", char)
print("Table:")
for row in table:
print(row)
return False
return True
def test_make_pairs_from_plaintext(plaintext):
pairs = make_pairs_from_plaintext(plaintext)
# print(pairs)
# NOTE: currently not checking if the plaintext is entirely allocated into the pairs
for pair in pairs:
# The plaintext must be separated into pair, always
if len(pair) != 2:
print(make_pairs_from_plaintext.__name__,
"function is returning something other than a pair. Value with error:", pair)
return False
# And the pair cannot have the same letter twice
if pair[0] == pair[1]:
# But if the repeated letter is the special inserted, there's no much to do...
# This is, for example, if we have a word that (for some reason) ends up with Q and have an even length
# In this case the algorithm would make a pair of QQ.
if pair[0] != 'q':
print(make_pairs_from_plaintext.__name__,
"function is returning a pair with repeated letters. Value with error:",
pair)
return False
return True
def test_make_pairs_from_ciphertext(ciphertext):
ciphertext_pairs = make_pairs_from_ciphertext(ciphertext)
#print("ciphertext_pairs:", ciphertext_pairs)
for pair in ciphertext_pairs:
if len(pair) != 2:
print("THIS IS NOT A PAIR:", pair)
return False
return True
def test_row_rule(pair, first_element_indexes, second_element_indexes, key_table, decrypt=False):
cipher_pair = row_rule(pair, first_element_indexes, second_element_indexes, key_table, decrypt)
if not cipher_pair:
# Error handling inside the function
return False
if len(cipher_pair) != 2:
print("ERROR: On function", row_rule.__name__, end=". ")
print("The returned value is not a pair.")
print("Inputted pair:", pair)
print("Cipher pair:", cipher_pair)
return False
return True
def test_column_rule(pair, key_table, first_element_indexes, second_element_indexes, decrypt=False):
cipher_pair = column_rule(pair, key_table, first_element_indexes, second_element_indexes, decrypt)
if not cipher_pair:
# Error handling inside the function
return False
if len(cipher_pair) != 2:
print("ERROR: On function", column_rule.__name__, end=". ")
print("The returned value is not a pair.")
print("Inputted pair:", pair)
print("Cipher pair:", cipher_pair)
return False
return True
def test_rectangle_rule(pair, key_table, first_element_indexes, second_element_indexes):
cipher_pair = rectangle_rule(pair, key_table, first_element_indexes, second_element_indexes)
if not cipher_pair:
# Error handling inside the function
return False
if len(cipher_pair) != 2:
print("ERROR: On function", rectangle_rule.__name__, end=". ")
print("The returned value is not a pair.")
print("Inputted pair:", pair)
print("Cipher pair:", cipher_pair)
return False
return True
def test_playfair_decrypt():
# test
pass
# # # Verify Input # # #
def do_verify_input_tests():
plaintext = "tttest with some words TEST "
ciphertext = "prprptdtqzlroqmcazuskyzltp"
key = "ChuCHU COM BaNaNa"
print(test_verify_input_plaintext.__name__, "|", test_verify_input_plaintext(plaintext, key))
print(test_verify_input_ciphertext.__name__, "|", test_verify_input_ciphertext(ciphertext, key))
# # # Make Key Table # # #
def do_make_key_table_tests():
key = "chuchucombanana"
key_table = make_key_table(key)
expected_key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
# print("KEY TABLE:", key_table)
print(test_make_key_table.__name__, "|", test_make_key_table(key))
print("Key Table is correct:", expected_key_table == key_table)
# # # Make Pairs # # #
def do_make_pairs_tests():
plaintext = "tttsestwithsomewordstest"
plaintext_pairs = make_pairs_from_plaintext(plaintext)
expected_plaintext_pairs = [['t', 'q'], ['t', 'q'], ['t', 's'], ['e', 's'], ['t', 'w'],
['i', 't'], ['h', 's'], ['o', 'm'], ['e', 'w'], ['o', 'r'],
['d', 's'], ['t', 'e'], ['s', 't']]
# print(plaintext_pairs)
print(make_pairs_from_plaintext.__name__, "|", test_make_pairs_from_plaintext(plaintext))
print("Plaintext pairs are correct:", expected_plaintext_pairs == plaintext_pairs)
# # # Make pairs from Ciphertext # # # #
ciphertext = "prprptdtqzlroqmcazuskyzltp"
ciphertext_pairs = make_pairs_from_ciphertext(ciphertext)
expected_ciphertext_pairs = [['p', 'r'], ['p', 'r'], ['p', 't'], ['d', 't'], ['q', 'z'],
['l', 'r'], ['o', 'q'], ['m', 'c'], ['a', 'z'], ['u', 's'],
['k', 'y'], ['z', 'l'], ['t', 'p']]
print(test_make_pairs_from_ciphertext.__name__, "|", test_make_pairs_from_ciphertext(ciphertext))
print("Ciphertext Pairs are correct:", expected_ciphertext_pairs == ciphertext_pairs)
# # # Rules # # #
# # # Row Rule in Encryption # # #
def do_row_rule_encryption_tests():
first_element_indexes = [0, 0]
second_element_indexes = [0, 2]
pair = ['c', 'u']
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
expected_pair_row = ['h', 'o']
pair_row = row_rule(pair, first_element_indexes, second_element_indexes, key_table)
print(row_rule.__name__, "|", test_row_rule(pair, first_element_indexes, second_element_indexes, key_table))
print("Row Rule (encryption) is correct:", expected_pair_row == pair_row)
# # # Row Rule in Decryption # # #
def do_row_rule_decryption_tests():
pair = ['p', 'r']
first_element_indexes = [3, 0]
second_element_indexes = [3, 2]
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
expected_pair_row = ['t', 'q']
pair_row = row_rule(pair, first_element_indexes, second_element_indexes, key_table, decrypt=True)
print(row_rule.__name__, "|", test_row_rule(pair, first_element_indexes, second_element_indexes, key_table,
decrypt=True))
print("Row Rule (decryption) is correct:", expected_pair_row == pair_row)
# # # Column Rule in Encryption # # #
def do_column_rule_encryption_tests():
first_element_indexes = [0, 1]
second_element_indexes = [2, 1]
pair = ['h', 'g']
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
expected_pair_column = ['a', 'q']
pair_column = column_rule(pair, key_table, first_element_indexes, second_element_indexes)
print(column_rule.__name__, "|", test_column_rule(pair, key_table, first_element_indexes, second_element_indexes))
print("Column Rule (encryption) is correct:", expected_pair_column == pair_column)
# # # Column Rule in Decryption # # #
def do_column_rule_decryption_tests():
first_element_indexes = [1, 4]
second_element_indexes = [2, 4]
pair = ['e', 'l']
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
expected_pair_column = ['m', 'e']
pair_column = column_rule(pair, key_table, first_element_indexes, second_element_indexes, decrypt=True)
print(column_rule.__name__, "|", test_column_rule(pair, key_table, first_element_indexes, second_element_indexes,
decrypt=True))
print("Column Rule (decryption) is correct:", expected_pair_column == pair_column)
# # # Rectangle Rule in Encryption # # #
def do_rectangle_rule_encryption_tests():
first_element_indexes = [0, 0]
second_element_indexes = [2, 2]
pair = ['c', 'i']
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
expected_pair_rectangle = ['u', 'f']
pair_rectangle = rectangle_rule(pair, key_table, first_element_indexes, second_element_indexes)
print(rectangle_rule.__name__, "|",
test_rectangle_rule(pair, key_table, first_element_indexes, second_element_indexes))
print("Rectangle Rule (encryption) is correct:", expected_pair_rectangle == pair_rectangle)
# # # Rectangle Rule in Decryption # # #
def do_rectangle_rule_decryption_tests():
first_element_indexes = [0, 2]
second_element_indexes = [2, 0]
pair = ['u', 'f']
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
expected_pair_rectangle = ['c', 'i']
pair_rectangle = rectangle_rule(pair, key_table, first_element_indexes, second_element_indexes)
print(rectangle_rule.__name__, "|",
test_rectangle_rule(pair, key_table, first_element_indexes, second_element_indexes))
print("Rectangle Rule (decryption) is correct:", expected_pair_rectangle == pair_rectangle)
# # # Playfair Cipher Encryption # # #
def do_playfair_cipher_tests():
key_table = [['c', 'h', 'u', 'o', 'm'],
['b', 'a', 'n', 'd', 'e'],
['f', 'g', 'i', 'k', 'l'],
['p', 'q', 'r', 's', 't'],
['v', 'w', 'x', 'y', 'z']]
plaintext_to_cipher_pairs = [['t', 'q'], ['t', 'q'], ['t', 's'], ['e', 's'], ['t', 'w'],
['i', 't'], ['h', 's'], ['o', 'm'], ['e', 'w'], ['o', 'r'],
['d', 's'], ['t', 'e'], ['s', 't']]
expected_plaintext_pairs = [['t', 'q'], ['t', 'q'], ['t', 's'], ['e', 's'], ['t', 'w'],
['i', 't'], ['h', 's'], ['o', 'm'], ['e', 'w'], ['o', 'r'],
['d', 's'], ['t', 'e'], ['s', 't']]
print("plaintext_to_cipher_pairs:\n", plaintext_to_cipher_pairs)
print("expected_plaintext_pairs:\n", expected_plaintext_pairs)
ciphertext_to_plaintext_pairs = [['p', 'r'], ['p', 'r'], ['p', 't'], ['d', 't'], ['q', 'z'],
['l', 'r'], ['o', 'q'], ['m', 'c'], ['a', 'z'], ['u', 's'],
['k', 'y'], ['z', 'l'], ['t', 'p']]
expected_ciphertext_pairs = [['p', 'r'], ['p', 'r'], ['p', 't'], ['d', 't'], ['q', 'z'],
['l', 'r'], ['o', 'q'], ['m', 'c'], ['a', 'z'], ['u', 's'],
['k', 'y'], ['z', 'l'], ['t', 'p']]
print("ciphertext_to_plaintext_pairs:\n", ciphertext_to_plaintext_pairs)
print("expected_ciphertext_pairs:\n", expected_ciphertext_pairs)
print(test_playfair_cipher.__name__, "(encryption) |", test_playfair_cipher(plaintext_to_cipher_pairs, key_table))
print("Playfair Cipher (encryption) is correct:", expected_ciphertext_pairs == plaintext_to_cipher_pairs)
print(test_playfair_cipher.__name__, "(decryption) |", test_playfair_cipher(ciphertext_to_plaintext_pairs,
key_table, decrypt=True))
print("Playfair Cipher (decryption) is correct:", expected_plaintext_pairs == ciphertext_to_plaintext_pairs)
def do_encrypt_tests():
key = "ChuCHU COM BaNaNa"
plaintext = "tttsest with some words TEST "
expected_ciphertext = "prprptdtqzlroqmcazuskyzltp"
print("playfair_encrypt_test:", expected_ciphertext == encrypt(plaintext, key))
def do_decrypt_tests():
key = "ChuCHU COM BaNaNa"
ciphertext = "prprptdtqzlroqmcazuskyzltp"
expected_plaintext = "tqtqtsestwithsomewordstest"
print("playfair_decrypt_test:", expected_plaintext == decrypt(ciphertext, key))
do_verify_input_tests()
do_make_pairs_tests()
do_make_key_table_tests()
do_row_rule_encryption_tests()
do_row_rule_decryption_tests()
do_column_rule_encryption_tests()
do_column_rule_decryption_tests()
do_rectangle_rule_encryption_tests()
do_rectangle_rule_decryption_tests()
do_playfair_cipher_tests()
do_encrypt_tests()
do_decrypt_tests()