1def plugboard_helper(char, set_list):
2 """Converts a given character into another based on a given list of sets."""
3 for double in set_list:
4 if char in double:
5 converted_char = double - set(char)
6 return ''.join(converted_char)
7 return char
8
9
10def plugboard(string, set_list):
11 converted = (''.join([plugboard_helper(char, set_list)
12 for char in string]))
13 return converted
14
15
16def rotor(string, dictionary):
17 converted = ''.join([dictionary.get(char, char) for char in string])
18 return converted
19
20
21def enigma_encrypt(plugboard_position, rotor_position):
22 def encryptor(func):
23 def inner(string):
24 encrypted = (rotor(plugboard(string, plugboard_position),
25 rotor_position))
26 func(encrypted)
27 return inner
28 return encryptor
29
30
31def enigma_decrypt(plugboard_position, rotor_position):
32 def decryptor(func):
33 def inner(string):
34 rotor_position_for_decrypting = (dict(
35 list((value, key) for key, value in rotor_position.items())))
36 decrypted = (plugboard(
37 rotor(string, rotor_position_for_decrypting), plugboard_position))
38 func(decrypted)
39 return inner
40 return decryptor
F.FF.....
======================================================================
FAIL: 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')
AssertionError: None != 'i love python'
======================================================================
FAIL: 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'),
AssertionError: None != 'the quick brown fox jumps over the lazy dog'
======================================================================
FAIL: 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'),
AssertionError: None != 'mjg cavsk nrqfy zqd haulp qxgr mjg itob eqw'
----------------------------------------------------------------------
Ran 9 tests in 0.001s
FAILED (failures=3)
f | 1 | def plugboard_helper(char, set_list): | f | 1 | def plugboard_helper(char, set_list): |
n | 2 | # converts a given character into another based on a given list of sets | n | 2 | """Converts a given character into another based on a given list of sets.""" |
3 | for double in set_list: | 3 | for double in set_list: | ||
4 | if char in double: | 4 | if char in double: | ||
5 | converted_char = double - set(char) | 5 | converted_char = double - set(char) | ||
6 | return ''.join(converted_char) | 6 | return ''.join(converted_char) | ||
7 | return char | 7 | return char | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def plugboard(string, set_list): | 10 | def plugboard(string, set_list): | ||
n | 11 | converted = ''.join(list(plugboard_helper(char, set_list) for char in string)) | n | 11 | converted = (''.join([plugboard_helper(char, set_list) |
12 | for char in string])) | ||||
12 | return converted | 13 | return converted | ||
13 | 14 | ||||
14 | 15 | ||||
15 | def rotor(string, dictionary): | 16 | def rotor(string, dictionary): | ||
n | 16 | converted = ''.join(list(dictionary.get(char, char) for char in string)) | n | 17 | converted = ''.join([dictionary.get(char, char) for char in string]) |
17 | return converted | 18 | return converted | ||
18 | 19 | ||||
19 | 20 | ||||
20 | def enigma_encrypt(plugboard_position, rotor_position): | 21 | def enigma_encrypt(plugboard_position, rotor_position): | ||
21 | def encryptor(func): | 22 | def encryptor(func): | ||
22 | def inner(string): | 23 | def inner(string): | ||
n | 23 | encrypted = rotor(plugboard(string, plugboard_position), \ | n | 24 | encrypted = (rotor(plugboard(string, plugboard_position), |
24 | rotor_position) | 25 | rotor_position)) | ||
25 | func(encrypted) | 26 | func(encrypted) | ||
26 | return inner | 27 | return inner | ||
27 | return encryptor | 28 | return encryptor | ||
28 | 29 | ||||
29 | 30 | ||||
30 | def enigma_decrypt(plugboard_position, rotor_position): | 31 | def enigma_decrypt(plugboard_position, rotor_position): | ||
31 | def decryptor(func): | 32 | def decryptor(func): | ||
32 | def inner(string): | 33 | def inner(string): | ||
t | 33 | rotor_position_for_decrypting = dict( \ | t | 34 | rotor_position_for_decrypting = (dict( |
34 | list((value, key) for key, value in rotor_position.items())) | 35 | list((value, key) for key, value in rotor_position.items()))) | ||
35 | decrypted = plugboard( \ | 36 | decrypted = (plugboard( | ||
36 | rotor(string, rotor_position_for_decrypting), plugboard_position) | 37 | rotor(string, rotor_position_for_decrypting), plugboard_position)) | ||
37 | func(decrypted) | 38 | func(decrypted) | ||
38 | return inner | 39 | return inner | ||
39 | return decryptor | 40 | return decryptor |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard_helper(char, set_list): | f | 1 | def plugboard_helper(char, set_list): |
2 | # converts a given character into another based on a given list of sets | 2 | # converts a given character into another based on a given list of sets | ||
3 | for double in set_list: | 3 | for double in set_list: | ||
4 | if char in double: | 4 | if char in double: | ||
5 | converted_char = double - set(char) | 5 | converted_char = double - set(char) | ||
6 | return ''.join(converted_char) | 6 | return ''.join(converted_char) | ||
7 | return char | 7 | return char | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def plugboard(string, set_list): | 10 | def plugboard(string, set_list): | ||
11 | converted = ''.join(list(plugboard_helper(char, set_list) for char in string)) | 11 | converted = ''.join(list(plugboard_helper(char, set_list) for char in string)) | ||
12 | return converted | 12 | return converted | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def rotor(string, dictionary): | 15 | def rotor(string, dictionary): | ||
16 | converted = ''.join(list(dictionary.get(char, char) for char in string)) | 16 | converted = ''.join(list(dictionary.get(char, char) for char in string)) | ||
17 | return converted | 17 | return converted | ||
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_encrypt(plugboard_position, rotor_position): | 20 | def enigma_encrypt(plugboard_position, rotor_position): | ||
21 | def encryptor(func): | 21 | def encryptor(func): | ||
22 | def inner(string): | 22 | def inner(string): | ||
23 | encrypted = rotor(plugboard(string, plugboard_position), \ | 23 | encrypted = rotor(plugboard(string, plugboard_position), \ | ||
n | 24 | rotor_position) | n | 24 | rotor_position) |
25 | func(encrypted) | 25 | func(encrypted) | ||
26 | return inner | 26 | return inner | ||
27 | return encryptor | 27 | return encryptor | ||
28 | 28 | ||||
29 | 29 | ||||
30 | def enigma_decrypt(plugboard_position, rotor_position): | 30 | def enigma_decrypt(plugboard_position, rotor_position): | ||
31 | def decryptor(func): | 31 | def decryptor(func): | ||
32 | def inner(string): | 32 | def inner(string): | ||
t | 33 | rotor_position_for_decrypting = dict( | t | 33 | rotor_position_for_decrypting = dict( \ |
34 | [(value, key) for key, value in rotor_position.items()]) | 34 | list((value, key) for key, value in rotor_position.items())) | ||
35 | decrypted = plugboard( | 35 | decrypted = plugboard( \ | ||
36 | rotor(string, rotor_position_for_decrypting), plugboard_position) | 36 | rotor(string, rotor_position_for_decrypting), plugboard_position) | ||
37 | func(decrypted) | 37 | func(decrypted) | ||
38 | return inner | 38 | return inner | ||
39 | return decryptor | 39 | return decryptor |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard_helper(char, set_list): | f | 1 | def plugboard_helper(char, set_list): |
2 | # converts a given character into another based on a given list of sets | 2 | # converts a given character into another based on a given list of sets | ||
3 | for double in set_list: | 3 | for double in set_list: | ||
4 | if char in double: | 4 | if char in double: | ||
5 | converted_char = double - set(char) | 5 | converted_char = double - set(char) | ||
6 | return ''.join(converted_char) | 6 | return ''.join(converted_char) | ||
7 | return char | 7 | return char | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def plugboard(string, set_list): | 10 | def plugboard(string, set_list): | ||
11 | converted = ''.join(list(plugboard_helper(char, set_list) for char in string)) | 11 | converted = ''.join(list(plugboard_helper(char, set_list) for char in string)) | ||
12 | return converted | 12 | return converted | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def rotor(string, dictionary): | 15 | def rotor(string, dictionary): | ||
16 | converted = ''.join(list(dictionary.get(char, char) for char in string)) | 16 | converted = ''.join(list(dictionary.get(char, char) for char in string)) | ||
17 | return converted | 17 | return converted | ||
18 | 18 | ||||
19 | 19 | ||||
20 | def enigma_encrypt(plugboard_position, rotor_position): | 20 | def enigma_encrypt(plugboard_position, rotor_position): | ||
21 | def encryptor(func): | 21 | def encryptor(func): | ||
t | 22 | def inner(str): | t | 22 | def inner(string): |
23 | encrypted = rotor(plugboard(str, plugboard_position), \ | 23 | encrypted = rotor(plugboard(string, plugboard_position), \ | ||
24 | rotor_position) | 24 | rotor_position) | ||
25 | func(encrypted) | 25 | func(encrypted) | ||
26 | return inner | 26 | return inner | ||
27 | return encryptor | 27 | return encryptor | ||
28 | 28 | ||||
29 | 29 | ||||
30 | def enigma_decrypt(plugboard_position, rotor_position): | 30 | def enigma_decrypt(plugboard_position, rotor_position): | ||
31 | def decryptor(func): | 31 | def decryptor(func): | ||
32 | def inner(string): | 32 | def inner(string): | ||
33 | rotor_position_for_decrypting = dict( | 33 | rotor_position_for_decrypting = dict( | ||
34 | [(value, key) for key, value in rotor_position.items()]) | 34 | [(value, key) for key, value in rotor_position.items()]) | ||
35 | decrypted = plugboard( | 35 | decrypted = plugboard( | ||
36 | rotor(string, rotor_position_for_decrypting), plugboard_position) | 36 | rotor(string, rotor_position_for_decrypting), plugboard_position) | ||
37 | func(decrypted) | 37 | func(decrypted) | ||
38 | return inner | 38 | return inner | ||
39 | return decryptor | 39 | return decryptor |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def plugboard_helper(char, list): | n | 1 | def plugboard_helper(char, set_list): |
2 | # converts a given character into another based on a given list of sets | 2 | # converts a given character into another based on a given list of sets | ||
n | 3 | for double in list: | n | 3 | for double in set_list: |
4 | if char in double: | 4 | if char in double: | ||
5 | converted_char = double - set(char) | 5 | converted_char = double - set(char) | ||
6 | return ''.join(converted_char) | 6 | return ''.join(converted_char) | ||
7 | return char | 7 | return char | ||
8 | 8 | ||||
9 | 9 | ||||
n | 10 | def plugboard(str, list): | n | 10 | def plugboard(string, set_list): |
11 | converted = ''.join([plugboard_helper(char, list) for char in str]) | 11 | converted = ''.join(list(plugboard_helper(char, set_list) for char in string)) | ||
12 | return converted | 12 | return converted | ||
13 | 13 | ||||
14 | 14 | ||||
n | 15 | def rotor_helper(char, dict): | n | ||
16 | # converts a given character into another based on a given dictionary | ||||
17 | if char == ' ': | ||||
18 | return char | ||||
19 | return dict[char] | ||||
20 | |||||
21 | |||||
22 | def rotor(str, dict): | 15 | def rotor(string, dictionary): | ||
23 | converted = ''.join(list(rotor_helper(char, dict) for char in str)) | 16 | converted = ''.join(list(dictionary.get(char, char) for char in string)) | ||
24 | return converted | 17 | return converted | ||
25 | 18 | ||||
26 | 19 | ||||
27 | def enigma_encrypt(plugboard_position, rotor_position): | 20 | def enigma_encrypt(plugboard_position, rotor_position): | ||
28 | def encryptor(func): | 21 | def encryptor(func): | ||
29 | def inner(str): | 22 | def inner(str): | ||
n | 30 | encrypted = rotor( | n | 23 | encrypted = rotor(plugboard(str, plugboard_position), \ |
31 | plugboard(str, plugboard_position), rotor_position) | 24 | rotor_position) | ||
32 | func(encrypted) | 25 | func(encrypted) | ||
33 | return inner | 26 | return inner | ||
34 | return encryptor | 27 | return encryptor | ||
35 | 28 | ||||
36 | 29 | ||||
37 | def enigma_decrypt(plugboard_position, rotor_position): | 30 | def enigma_decrypt(plugboard_position, rotor_position): | ||
38 | def decryptor(func): | 31 | def decryptor(func): | ||
n | 39 | def inner(str): | n | 32 | def inner(string): |
40 | rotor_position_for_decrypting = dict( | 33 | rotor_position_for_decrypting = dict( | ||
41 | [(value, key) for key, value in rotor_position.items()]) | 34 | [(value, key) for key, value in rotor_position.items()]) | ||
42 | decrypted = plugboard( | 35 | decrypted = plugboard( | ||
t | 43 | rotor(str, rotor_position_for_decrypting), plugboard_position) | t | 36 | rotor(string, rotor_position_for_decrypting), plugboard_position) |
44 | func(decrypted) | 37 | func(decrypted) | ||
45 | return inner | 38 | return inner | ||
46 | return decryptor | 39 | return decryptor |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|