1def plugboard(text, plugboard_position):
2 new_text = ""
3 for letter in text:
4 for pair in plugboard_position:
5 if letter in pair:
6 new_text += [other_letter for other_letter in pair if other_letter is not letter][0]
7 break
8 else:
9 new_text += letter
10 return new_text
11
12def rotor(text, rotor_position):
13 new_text = ""
14 for letter in text:
15 new_text += rotor_position.get(letter," ")
16 return new_text
17
18def enigma_encrypt(plugboard_position, rotor_position):
19 def encrypt_decorator(func):
20 def encrypt_function(text):
21 text = plugboard(text,plugboard_position)
22 text = rotor(text,rotor_position)
23 func(text)
24 return encrypt_function
25 return encrypt_decorator
26
27def enigma_decrypt(plugboard_position, rotor_position):
28 def enigma_decrypt_decorator(func):
29 def decrypt_function(text):
30 new_rotor_position={}
31 for letter in rotor_position:
32 new_rotor_position[rotor_position[letter]] = letter
33 text = rotor(text,new_rotor_position)
34 text = plugboard(text,plugboard_position)
35 func(text)
36 return decrypt_function
37 return enigma_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)
n | 1 | def plugboard(text,plugboard_position): | n | 1 | def plugboard(text, plugboard_position): |
2 | new_text = "" | 2 | new_text = "" | ||
n | 3 | in_pair = False | n | ||
4 | for letter in text: | 3 | for letter in text: | ||
5 | for pair in plugboard_position: | 4 | for pair in plugboard_position: | ||
6 | if letter in pair: | 5 | if letter in pair: | ||
7 | new_text += [other_letter for other_letter in pair if other_letter is not letter][0] | 6 | new_text += [other_letter for other_letter in pair if other_letter is not letter][0] | ||
n | 8 | in_pair = True | n | ||
9 | break | 7 | break | ||
n | 10 | new_text += letter if in_pair is False else "" | n | 8 | else: |
11 | in_pair = False | 9 | new_text += letter | ||
12 | return new_text | 10 | return new_text | ||
13 | 11 | ||||
n | 14 | def rotor(text,rotor_position): | n | 12 | def rotor(text, rotor_position): |
15 | new_text = "" | 13 | new_text = "" | ||
16 | for letter in text: | 14 | for letter in text: | ||
n | 17 | if letter in rotor_position: | n | ||
18 | new_text += rotor_position[letter] | 15 | new_text += rotor_position.get(letter," ") | ||
19 | else: | ||||
20 | new_text += " " | ||||
21 | return new_text | 16 | return new_text | ||
22 | 17 | ||||
n | 23 | def enigma_encrypt(plugboard_position,rotor_position): | n | 18 | def enigma_encrypt(plugboard_position, rotor_position): |
24 | def encrypt_decorator(func): | 19 | def encrypt_decorator(func): | ||
25 | def encrypt_function(text): | 20 | def encrypt_function(text): | ||
26 | text = plugboard(text,plugboard_position) | 21 | text = plugboard(text,plugboard_position) | ||
27 | text = rotor(text,rotor_position) | 22 | text = rotor(text,rotor_position) | ||
28 | func(text) | 23 | func(text) | ||
29 | return encrypt_function | 24 | return encrypt_function | ||
30 | return encrypt_decorator | 25 | return encrypt_decorator | ||
31 | 26 | ||||
t | 32 | def enigma_decrypt(plugboard_position,rotor_position): | t | 27 | def enigma_decrypt(plugboard_position, rotor_position): |
33 | def enigma_decrypt_decorator(func): | 28 | def enigma_decrypt_decorator(func): | ||
34 | def decrypt_function(text): | 29 | def decrypt_function(text): | ||
35 | new_rotor_position={} | 30 | new_rotor_position={} | ||
36 | for letter in rotor_position: | 31 | for letter in rotor_position: | ||
37 | new_rotor_position[rotor_position[letter]] = letter | 32 | new_rotor_position[rotor_position[letter]] = letter | ||
38 | text = rotor(text,new_rotor_position) | 33 | text = rotor(text,new_rotor_position) | ||
39 | text = plugboard(text,plugboard_position) | 34 | text = plugboard(text,plugboard_position) | ||
40 | func(text) | 35 | func(text) | ||
41 | return decrypt_function | 36 | return decrypt_function | ||
42 | return enigma_decrypt_decorator | 37 | return enigma_decrypt_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|