-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspositionTest.py
More file actions
48 lines (38 loc) · 1.62 KB
/
transpositionTest.py
File metadata and controls
48 lines (38 loc) · 1.62 KB
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
# ===================================
# Test Cases for Transposition Cipher
# ===================================
import random
from datetime import datetime
import sys
import transpositionCipher, decryptTranspositionCipher
def main():
# get the RNG warmed up
random.seed(datetime.now())
# Generate 20 random test messages
for i in range(20):
# each message has random length
message = 'ABCDEFGHIKLMNOPQRSTUVWXYZ' * random.randint(4, 40)
# Shuffle the message characters for more randomization
# convert to list, then shuffle
message = list(message)
random.shuffle(message)
# convert list back to string
message = ''.join(message)
# show each test message as it's first 50 characters for convenience
print('Test #%s: "%s..."' % (i+1, message[:50]))
# Check message using all possible keys
# Remember: key's range from 1 to half of message size
for key in range(1, int(len(message)/2)):
ciphertext = transpositionCipher.encryptMessage(key, message)
plaintext = decryptTranspositionCipher.decryptMessage(key, ciphertext)
# if the decryption is not equal to the plaintext
# display error message then quit
if message != plaintext:
print('Error decrypting message %s with key %s' % (message, key))
print('Decrypted as: ' + plaintext)
sys.exit()
# if here, then all test messages encrypt and decrypt properly
print('Transposition cipher test passed.')
# if run directly, call main
if __name__ == '__main__':
main()