1def plugboard(input_str, plugboard_position):
2 new_str = ''
3 for old_letter in input_str:
4 plugboard_set = [containing_set for containing_set in plugboard_position if old_letter in containing_set]
5 if plugboard_set:
6 new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0]
7 new_str += new_letter
8 else:
9 new_str += old_letter
10 return new_str
11
12def rotor(input_str, rotor_position):
13 new_str = ''
14 for letter in input_str:
15 new_str += rotor_position.get(letter,' ')
16 return new_str
17
18def enigma_encrypt(plugboard_position, rotor_position):
19 def encrypt_decorator(str_func):
20 def decorated_func(input_str):
21 str_func(rotor(plugboard(input_str, plugboard_position), rotor_position))
22 return decorated_func
23 return encrypt_decorator
24
25def enigma_decrypt(plugboard_position, rotor_position):
26 new_rotor_position = {}
27 for key, val in rotor_position.items():
28 new_rotor_position[val] = key
29
30 def decrypt_decorator(str_func):
31 def decorated_func(input_str):
32 str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position))
33 return decorated_func
34 return decrypt_decorator
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(input_str, plugboard_position): | f | 1 | def plugboard(input_str, plugboard_position): |
2 | new_str = '' | 2 | new_str = '' | ||
3 | for old_letter in input_str: | 3 | for old_letter in input_str: | ||
n | 4 | flag = 0 | n | ||
5 | plugboard_set = [set for set in plugboard_position if old_letter in set] | 4 | plugboard_set = [containing_set for containing_set in plugboard_position if old_letter in containing_set] | ||
6 | if plugboard_set: | 5 | if plugboard_set: | ||
7 | new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0] | 6 | new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0] | ||
8 | new_str += new_letter | 7 | new_str += new_letter | ||
n | 9 | flag = 1 | n | 8 | else: |
10 | if flag == 0: | ||||
11 | new_str += old_letter | 9 | new_str += old_letter | ||
12 | return new_str | 10 | return new_str | ||
13 | 11 | ||||
14 | def rotor(input_str, rotor_position): | 12 | def rotor(input_str, rotor_position): | ||
15 | new_str = '' | 13 | new_str = '' | ||
16 | for letter in input_str: | 14 | for letter in input_str: | ||
n | 17 | if letter == ' ': | n | ||
18 | new_str += letter | ||||
19 | continue | ||||
20 | new_str += rotor_position[letter] | 15 | new_str += rotor_position.get(letter,' ') | ||
21 | return new_str | 16 | return new_str | ||
22 | 17 | ||||
23 | def enigma_encrypt(plugboard_position, rotor_position): | 18 | def enigma_encrypt(plugboard_position, rotor_position): | ||
24 | def encrypt_decorator(str_func): | 19 | def encrypt_decorator(str_func): | ||
25 | def decorated_func(input_str): | 20 | def decorated_func(input_str): | ||
26 | str_func(rotor(plugboard(input_str, plugboard_position), rotor_position)) | 21 | str_func(rotor(plugboard(input_str, plugboard_position), rotor_position)) | ||
27 | return decorated_func | 22 | return decorated_func | ||
28 | return encrypt_decorator | 23 | return encrypt_decorator | ||
29 | 24 | ||||
30 | def enigma_decrypt(plugboard_position, rotor_position): | 25 | def enigma_decrypt(plugboard_position, rotor_position): | ||
31 | new_rotor_position = {} | 26 | new_rotor_position = {} | ||
32 | for key, val in rotor_position.items(): | 27 | for key, val in rotor_position.items(): | ||
33 | new_rotor_position[val] = key | 28 | new_rotor_position[val] = key | ||
34 | 29 | ||||
35 | def decrypt_decorator(str_func): | 30 | def decrypt_decorator(str_func): | ||
36 | def decorated_func(input_str): | 31 | def decorated_func(input_str): | ||
37 | str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position)) | 32 | str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position)) | ||
38 | return decorated_func | 33 | return decorated_func | ||
39 | return decrypt_decorator | 34 | return decrypt_decorator | ||
t | 40 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(input_str, plugboard_position): | f | 1 | def plugboard(input_str, plugboard_position): |
2 | new_str = '' | 2 | new_str = '' | ||
3 | for old_letter in input_str: | 3 | for old_letter in input_str: | ||
4 | flag = 0 | 4 | flag = 0 | ||
t | 5 | for plugboard_set in plugboard_position: | t | 5 | plugboard_set = [set for set in plugboard_position if old_letter in set] |
6 | if old_letter in plugboard_set: | 6 | if plugboard_set: | ||
7 | new_letter = [letter for letter in plugboard_set if letter != old_letter][0] | 7 | new_letter = [letter for letter in plugboard_set[0] if letter != old_letter][0] | ||
8 | new_str += new_letter | 8 | new_str += new_letter | ||
9 | flag = 1 | 9 | flag = 1 | ||
10 | if flag == 0: | 10 | if flag == 0: | ||
11 | new_str += old_letter | 11 | new_str += old_letter | ||
12 | return new_str | 12 | return new_str | ||
13 | 13 | ||||
14 | def rotor(input_str, rotor_position): | 14 | def rotor(input_str, rotor_position): | ||
15 | new_str = '' | 15 | new_str = '' | ||
16 | for letter in input_str: | 16 | for letter in input_str: | ||
17 | if letter == ' ': | 17 | if letter == ' ': | ||
18 | new_str += letter | 18 | new_str += letter | ||
19 | continue | 19 | continue | ||
20 | new_str += rotor_position[letter] | 20 | new_str += rotor_position[letter] | ||
21 | return new_str | 21 | return new_str | ||
22 | 22 | ||||
23 | def enigma_encrypt(plugboard_position, rotor_position): | 23 | def enigma_encrypt(plugboard_position, rotor_position): | ||
24 | def encrypt_decorator(str_func): | 24 | def encrypt_decorator(str_func): | ||
25 | def decorated_func(input_str): | 25 | def decorated_func(input_str): | ||
26 | str_func(rotor(plugboard(input_str, plugboard_position), rotor_position)) | 26 | str_func(rotor(plugboard(input_str, plugboard_position), rotor_position)) | ||
27 | return decorated_func | 27 | return decorated_func | ||
28 | return encrypt_decorator | 28 | return encrypt_decorator | ||
29 | 29 | ||||
30 | def enigma_decrypt(plugboard_position, rotor_position): | 30 | def enigma_decrypt(plugboard_position, rotor_position): | ||
31 | new_rotor_position = {} | 31 | new_rotor_position = {} | ||
32 | for key, val in rotor_position.items(): | 32 | for key, val in rotor_position.items(): | ||
33 | new_rotor_position[val] = key | 33 | new_rotor_position[val] = key | ||
34 | 34 | ||||
35 | def decrypt_decorator(str_func): | 35 | def decrypt_decorator(str_func): | ||
36 | def decorated_func(input_str): | 36 | def decorated_func(input_str): | ||
37 | str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position)) | 37 | str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position)) | ||
38 | return decorated_func | 38 | return decorated_func | ||
39 | return decrypt_decorator | 39 | return decrypt_decorator | ||
40 | 40 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(input_str, plugboard_position): | f | 1 | def plugboard(input_str, plugboard_position): |
2 | new_str = '' | 2 | new_str = '' | ||
n | 3 | n | |||
4 | for old_letter in input_str: | 3 | for old_letter in input_str: | ||
5 | flag = 0 | 4 | flag = 0 | ||
n | 6 | n | |||
7 | for plugboard_set in plugboard_position: | 5 | for plugboard_set in plugboard_position: | ||
n | 8 | n | |||
9 | if old_letter in plugboard_set: | 6 | if old_letter in plugboard_set: | ||
n | 10 | for new_letter in plugboard_set: | n | 7 | new_letter = [letter for letter in plugboard_set if letter != old_letter][0] |
11 | if new_letter != old_letter: | ||||
12 | new_str += new_letter | 8 | new_str += new_letter | ||
13 | flag = 1 | 9 | flag = 1 | ||
14 | |||||
15 | if flag == 0: | 10 | if flag == 0: | ||
16 | new_str += old_letter | 11 | new_str += old_letter | ||
n | 17 | n | |||
18 | return new_str | 12 | return new_str | ||
19 | 13 | ||||
20 | def rotor(input_str, rotor_position): | 14 | def rotor(input_str, rotor_position): | ||
21 | new_str = '' | 15 | new_str = '' | ||
n | 22 | n | |||
23 | for letter in input_str: | 16 | for letter in input_str: | ||
n | n | 17 | if letter == ' ': | ||
18 | new_str += letter | ||||
19 | continue | ||||
24 | new_str += rotor_position[letter] | 20 | new_str += rotor_position[letter] | ||
n | 25 | n | |||
26 | return new_str | 21 | return new_str | ||
27 | 22 | ||||
28 | def enigma_encrypt(plugboard_position, rotor_position): | 23 | def enigma_encrypt(plugboard_position, rotor_position): | ||
n | 29 | n | |||
30 | def encrypt_decorator(str_func): | 24 | def encrypt_decorator(str_func): | ||
n | 31 | n | |||
32 | def decorated_func(input_str): | 25 | def decorated_func(input_str): | ||
33 | str_func(rotor(plugboard(input_str, plugboard_position), rotor_position)) | 26 | str_func(rotor(plugboard(input_str, plugboard_position), rotor_position)) | ||
n | 34 | n | |||
35 | return decorated_func | 27 | return decorated_func | ||
n | 36 | n | |||
37 | return encrypt_decorator | 28 | return encrypt_decorator | ||
38 | 29 | ||||
39 | def enigma_decrypt(plugboard_position, rotor_position): | 30 | def enigma_decrypt(plugboard_position, rotor_position): | ||
n | 40 | n | |||
41 | new_rotor_position = {} | 31 | new_rotor_position = {} | ||
n | 42 | n | |||
43 | for key, val in rotor_position.items(): | 32 | for key, val in rotor_position.items(): | ||
44 | new_rotor_position[val] = key | 33 | new_rotor_position[val] = key | ||
45 | 34 | ||||
46 | def decrypt_decorator(str_func): | 35 | def decrypt_decorator(str_func): | ||
n | 47 | n | |||
48 | def decorated_func(input_str): | 36 | def decorated_func(input_str): | ||
49 | str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position)) | 37 | str_func(plugboard(rotor(input_str, new_rotor_position), plugboard_position)) | ||
n | 50 | n | |||
51 | return decorated_func | 38 | return decorated_func | ||
n | 52 | n | |||
53 | return decrypt_decorator | 39 | return decrypt_decorator | ||
54 | 40 | ||||
t | 55 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|