1def plugboard(text, list_of_sets):
2 converted_text = ''
3 for char in text:
4 for current_set in list_of_sets:
5 if char in current_set:
6 converted_text += current_set.difference({char}).pop()
7 break
8 else:
9 converted_text += char
10
11 return converted_text
12
13
14def rotor(text, dictionary_connections):
15 converted_text = ''
16 for char in text:
17 converted_text += dictionary_connections[char]
18 return converted_text
19
20
21def enigma_encrypt(plugboard_position, rotor_position):
22 def decorator(func):
23 def wrapper(text):
24 text = plugboard(text, plugboard_position)
25 text = rotor(text, rotor_position)
26 return func(text)
27
28 return wrapper
29
30 return decorator
31
32
33def enigma_decrypt(plugboard_position, rotor_position):
34 def decorator(func):
35 def wrapper(text):
36 switched_rotor = {v: k for k, v in rotor_position.items()}
37 text = rotor(text, switched_rotor)
38 text = plugboard(text, plugboard_position)
39 return func(text)
40
41 return wrapper
42
43 return decorator
44
45
46if __name__ == '__main__':
47 plugboard_position = [{'a', 'c'}, {'t', 'z'}]
48 rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
49 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
50 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
51 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
52 'm': 'r', 'c': 'k'}
53 rotor('enigma', rotor_position)
54 plugboard('enigma', plugboard_position)
55
56 encryptor = enigma_encrypt(plugboard_position=plugboard_position,
57 rotor_position=rotor_position)
58 decryptor = enigma_decrypt(plugboard_position=plugboard_position,
59 rotor_position=rotor_position)
60 encrypt_print = encryptor(print)
61 decrypt_print = decryptor(print)
62 encrypt_print('enigma')
63 decrypt_print('tnwork')
E.EE....E
======================================================================
ERROR: test_full_letter_set (test.TestCombination)
Test decrypting an encrypted text against itself.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 139, in test_full_letter_set
self.assertEqual(combined('i love python'), 'i love python')
File "/tmp/solution.py", line 37, in wrapper
text = rotor(text, switched_rotor)
File "/tmp/solution.py", line 17, in rotor
converted_text += dictionary_connections[char]
KeyError: ' '
======================================================================
ERROR: test_full_letter_set (test.TestDecryptor)
Test the decryptor function with all letters in the rotor.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 112, in test_full_letter_set
self.assertEqual(decrypted('mlx fuver cbakn jad guoyq aixb mlx pzhw sat'),
File "/tmp/solution.py", line 37, in wrapper
text = rotor(text, switched_rotor)
File "/tmp/solution.py", line 17, in rotor
converted_text += dictionary_connections[char]
KeyError: ' '
======================================================================
ERROR: test_full_letter_set (test.TestEncryptor)
Test the encryptor function with all letters in the rotor.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 89, in test_full_letter_set
self.assertEqual(encrypted('the quick brown fox jumps over the lazy dog'),
File "/tmp/solution.py", line 25, in wrapper
text = rotor(text, rotor_position)
File "/tmp/solution.py", line 17, in rotor
converted_text += dictionary_connections[char]
KeyError: ' '
======================================================================
ERROR: test_normal_case (test.TestRotor)
Test the rotor function with normally expected input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 67, in test_normal_case
self.assertEqual(rotor('this is a test input', self.ROTOR_POSITION), 'kbjo jo c kdok jylqk')
File "/tmp/solution.py", line 17, in rotor
converted_text += dictionary_connections[char]
KeyError: ' '
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (errors=4)
f | 1 | def plugboard(text, list_of_sets): | f | 1 | def plugboard(text, list_of_sets): |
2 | converted_text = '' | 2 | converted_text = '' | ||
3 | for char in text: | 3 | for char in text: | ||
4 | for current_set in list_of_sets: | 4 | for current_set in list_of_sets: | ||
5 | if char in current_set: | 5 | if char in current_set: | ||
6 | converted_text += current_set.difference({char}).pop() | 6 | converted_text += current_set.difference({char}).pop() | ||
7 | break | 7 | break | ||
8 | else: | 8 | else: | ||
9 | converted_text += char | 9 | converted_text += char | ||
10 | 10 | ||||
11 | return converted_text | 11 | return converted_text | ||
12 | 12 | ||||
13 | 13 | ||||
14 | def rotor(text, dictionary_connections): | 14 | def rotor(text, dictionary_connections): | ||
15 | converted_text = '' | 15 | converted_text = '' | ||
16 | for char in text: | 16 | for char in text: | ||
17 | converted_text += dictionary_connections[char] | 17 | converted_text += dictionary_connections[char] | ||
18 | return converted_text | 18 | return converted_text | ||
19 | 19 | ||||
20 | 20 | ||||
21 | def enigma_encrypt(plugboard_position, rotor_position): | 21 | def enigma_encrypt(plugboard_position, rotor_position): | ||
22 | def decorator(func): | 22 | def decorator(func): | ||
23 | def wrapper(text): | 23 | def wrapper(text): | ||
24 | text = plugboard(text, plugboard_position) | 24 | text = plugboard(text, plugboard_position) | ||
25 | text = rotor(text, rotor_position) | 25 | text = rotor(text, rotor_position) | ||
26 | return func(text) | 26 | return func(text) | ||
27 | 27 | ||||
28 | return wrapper | 28 | return wrapper | ||
29 | 29 | ||||
30 | return decorator | 30 | return decorator | ||
31 | 31 | ||||
32 | 32 | ||||
n | 33 | def switch_rotor(rotor_position): | n | ||
34 | switched_rotor = {v: k for k, v in rotor_position.items()} | ||||
35 | return switched_rotor | ||||
36 | |||||
37 | |||||
38 | def enigma_decrypt(plugboard_position, rotor_position): | 33 | def enigma_decrypt(plugboard_position, rotor_position): | ||
39 | def decorator(func): | 34 | def decorator(func): | ||
40 | def wrapper(text): | 35 | def wrapper(text): | ||
t | 41 | switched_rotor = switch_rotor(rotor_position) | t | 36 | switched_rotor = {v: k for k, v in rotor_position.items()} |
42 | text = rotor(text, switched_rotor) | 37 | text = rotor(text, switched_rotor) | ||
43 | text = plugboard(text, plugboard_position) | 38 | text = plugboard(text, plugboard_position) | ||
44 | return func(text) | 39 | return func(text) | ||
45 | 40 | ||||
46 | return wrapper | 41 | return wrapper | ||
47 | 42 | ||||
48 | return decorator | 43 | return decorator | ||
49 | 44 | ||||
50 | 45 | ||||
51 | if __name__ == '__main__': | 46 | if __name__ == '__main__': | ||
52 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | 47 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
53 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 48 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
54 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 49 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
55 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 50 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
56 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 51 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
57 | 'm': 'r', 'c': 'k'} | 52 | 'm': 'r', 'c': 'k'} | ||
58 | rotor('enigma', rotor_position) | 53 | rotor('enigma', rotor_position) | ||
59 | plugboard('enigma', plugboard_position) | 54 | plugboard('enigma', plugboard_position) | ||
60 | 55 | ||||
61 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | 56 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | ||
62 | rotor_position=rotor_position) | 57 | rotor_position=rotor_position) | ||
63 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | 58 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | ||
64 | rotor_position=rotor_position) | 59 | rotor_position=rotor_position) | ||
65 | encrypt_print = encryptor(print) | 60 | encrypt_print = encryptor(print) | ||
66 | decrypt_print = decryptor(print) | 61 | decrypt_print = decryptor(print) | ||
67 | encrypt_print('enigma') | 62 | encrypt_print('enigma') | ||
68 | decrypt_print('tnwork') | 63 | decrypt_print('tnwork') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|