/
transpositionTest.py
36 lines (25 loc) · 1.08 KB
/
transpositionTest.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
# Transposition Cipher Test
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
import random, sys, transpositionEncrypt, transpositionDecrypt
def main():
# Set the random "seed" to a static value.
random.seed(42)
for i in range(20):
# Generate random messages to test
message = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' * random.randint(4, 40)
message = list(message)
random.shuffle(message)
# Convert the list back to a string.
message = ''.join(message)
print('Test #%s: "%s..."' % (i + 1, message[:50]))
# Check all possible keys for each message:
for key in range(1, int(len(message)/2)):
encrypted = transpositionEncrypt.encryptMessage(key, message)
decrypted = transpositionDecrypt.decryptMessage(key, encrypted)
if message != decrypted:
print('Mismatch with key %s and message %s.' % (key, message))
print ('Decrypted as: ' + decrypted)
sys.exit()
print('Transposition cipher test passed.')
if __name__ == '__main__':
main()