1def plugboard(word, sets):
2 wordRes = ""
3 for letter in word:
4 for x, y in sets:
5 if letter == x:
6 letter = y
7 break
8 if letter == y:
9 letter = x
10 break
11 wordRes+=letter
12 return wordRes
13
14def rotor(word, alphabet):
15 wordRes = ""
16 for letter in word:
17 if(letter == " "):
18 wordRes += letter
19 continue
20 wordRes+=alphabet[letter]
21 return wordRes
22
23def enigma_encrypt(plugboard_position, rotor_position):
24 def decorator(func):
25 def func_params(initial_str):
26 initial_str = rotor(plugboard(initial_str, plugboard_position), rotor_position)
27 func(initial_str)
28 return func_params
29 return decorator
30
31def enigma_decrypt(plugboard_position, rotor_position):
32 def decorator(func):
33 def func_params(initial_str):
34 reversed_rotor_position = {}
35 for x, y in rotor_position.items():
36 reversed_rotor_position[y] = x
37 initial_str = plugboard(rotor(initial_str, reversed_rotor_position), plugboard_position)
38 func(initial_str)
39 return func_params
40 return 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(word, sets): | f | 1 | def plugboard(word, sets): |
2 | wordRes = "" | 2 | wordRes = "" | ||
3 | for letter in word: | 3 | for letter in word: | ||
4 | for x, y in sets: | 4 | for x, y in sets: | ||
5 | if letter == x: | 5 | if letter == x: | ||
6 | letter = y | 6 | letter = y | ||
7 | break | 7 | break | ||
8 | if letter == y: | 8 | if letter == y: | ||
9 | letter = x | 9 | letter = x | ||
10 | break | 10 | break | ||
11 | wordRes+=letter | 11 | wordRes+=letter | ||
12 | return wordRes | 12 | return wordRes | ||
13 | 13 | ||||
14 | def rotor(word, alphabet): | 14 | def rotor(word, alphabet): | ||
15 | wordRes = "" | 15 | wordRes = "" | ||
16 | for letter in word: | 16 | for letter in word: | ||
17 | if(letter == " "): | 17 | if(letter == " "): | ||
t | t | 18 | wordRes += letter | ||
18 | continue | 19 | continue | ||
19 | wordRes+=alphabet[letter] | 20 | wordRes+=alphabet[letter] | ||
20 | return wordRes | 21 | return wordRes | ||
21 | 22 | ||||
22 | def enigma_encrypt(plugboard_position, rotor_position): | 23 | def enigma_encrypt(plugboard_position, rotor_position): | ||
23 | def decorator(func): | 24 | def decorator(func): | ||
24 | def func_params(initial_str): | 25 | def func_params(initial_str): | ||
25 | initial_str = rotor(plugboard(initial_str, plugboard_position), rotor_position) | 26 | initial_str = rotor(plugboard(initial_str, plugboard_position), rotor_position) | ||
26 | func(initial_str) | 27 | func(initial_str) | ||
27 | return func_params | 28 | return func_params | ||
28 | return decorator | 29 | return decorator | ||
29 | 30 | ||||
30 | def enigma_decrypt(plugboard_position, rotor_position): | 31 | def enigma_decrypt(plugboard_position, rotor_position): | ||
31 | def decorator(func): | 32 | def decorator(func): | ||
32 | def func_params(initial_str): | 33 | def func_params(initial_str): | ||
33 | reversed_rotor_position = {} | 34 | reversed_rotor_position = {} | ||
34 | for x, y in rotor_position.items(): | 35 | for x, y in rotor_position.items(): | ||
35 | reversed_rotor_position[y] = x | 36 | reversed_rotor_position[y] = x | ||
36 | initial_str = plugboard(rotor(initial_str, reversed_rotor_position), plugboard_position) | 37 | initial_str = plugboard(rotor(initial_str, reversed_rotor_position), plugboard_position) | ||
37 | func(initial_str) | 38 | func(initial_str) | ||
38 | return func_params | 39 | return func_params | ||
39 | return decorator | 40 | return decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
31.10.2023 09:28
31.10.2023 09:29
31.10.2023 09:29