1def plugboard(input_str, connections):
2 plugboard_dict = {}
3 for connection in connections:
4 letter1, letter2 = connection
5 plugboard_dict[letter1] = letter2
6 plugboard_dict[letter2] = letter1
7
8 output_str = ""
9 for letter in input_str:
10 if letter in plugboard_dict:
11 output_str += plugboard_dict[letter]
12 else:
13 output_str += letter
14
15 return output_str
16
17
18def rotor(input_str, rotor_dict):
19 result = ""
20 for letter in input_str:
21 result += rotor_dict.get(letter, letter)
22 return result
23
24
25def enigma_encrypt(plugboard_position, rotor_position):
26 def decorator(func_e):
27 def str_encrypt(str_e):
28 str_e = plugboard(str_e, plugboard_position)
29 str_e = rotor(str_e, rotor_position)
30 return func_e(str_e)
31 return str_encrypt
32 return decorator
33
34def enigma_decrypt(plugboard_position, rotor_position):
35 def decorator(func_d):
36 def str_decrypt(str_d):
37 str_d = rotor(str_d, {v: k for k, v in rotor_position.items()})
38 str_d = plugboard(str_d, plugboard_position)
39 return func_d(str_d)
40 return str_decrypt
41 return decorator
42
43plugboard_position = [{'a', 'c'}, {'t', 'z'}]
44rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
45 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
46 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
47 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
48 'm': 'r', 'c': 'k'}
49
50rotor('enigma', rotor_position) # tnwora
51plugboard('enigma', plugboard_position) # enigmc
52
53encryptor = enigma_encrypt(plugboard_position=plugboard_position,
54 rotor_position=rotor_position)
55decryptor = enigma_decrypt(plugboard_position=plugboard_position,
56 rotor_position=rotor_position)
57
58encrypt_print = encryptor(print)
59decrypt_print = decryptor(print)
60
61encrypt_print('enigma') # tnwork
62decrypt_print('tnwork') # enigma
tnwork
enigma
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
f | 1 | def plugboard(input_str, connections): | f | 1 | def plugboard(input_str, connections): |
2 | plugboard_dict = {} | 2 | plugboard_dict = {} | ||
3 | for connection in connections: | 3 | for connection in connections: | ||
4 | letter1, letter2 = connection | 4 | letter1, letter2 = connection | ||
5 | plugboard_dict[letter1] = letter2 | 5 | plugboard_dict[letter1] = letter2 | ||
6 | plugboard_dict[letter2] = letter1 | 6 | plugboard_dict[letter2] = letter1 | ||
7 | 7 | ||||
8 | output_str = "" | 8 | output_str = "" | ||
9 | for letter in input_str: | 9 | for letter in input_str: | ||
10 | if letter in plugboard_dict: | 10 | if letter in plugboard_dict: | ||
11 | output_str += plugboard_dict[letter] | 11 | output_str += plugboard_dict[letter] | ||
12 | else: | 12 | else: | ||
13 | output_str += letter | 13 | output_str += letter | ||
14 | 14 | ||||
15 | return output_str | 15 | return output_str | ||
16 | 16 | ||||
17 | 17 | ||||
18 | def rotor(input_str, rotor_dict): | 18 | def rotor(input_str, rotor_dict): | ||
19 | result = "" | 19 | result = "" | ||
20 | for letter in input_str: | 20 | for letter in input_str: | ||
21 | result += rotor_dict.get(letter, letter) | 21 | result += rotor_dict.get(letter, letter) | ||
22 | return result | 22 | return result | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def enigma_encrypt(plugboard_position, rotor_position): | 25 | def enigma_encrypt(plugboard_position, rotor_position): | ||
n | 26 | def decorator(func): | n | 26 | def decorator(func_e): |
27 | def str_encrypt(str): | 27 | def str_encrypt(str_e): | ||
28 | str = plugboard(str, plugboard_position) | 28 | str_e = plugboard(str_e, plugboard_position) | ||
29 | str = rotor(str, rotor_position) | 29 | str_e = rotor(str_e, rotor_position) | ||
30 | return func(str) | 30 | return func_e(str_e) | ||
31 | return str_encrypt | 31 | return str_encrypt | ||
32 | return decorator | 32 | return decorator | ||
33 | 33 | ||||
34 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
t | 35 | def decorator(func): | t | 35 | def decorator(func_d): |
36 | def str_decrypt(str): | 36 | def str_decrypt(str_d): | ||
37 | str = rotor(str, {v: k for k, v in rotor_position.items()}) | 37 | str_d = rotor(str_d, {v: k for k, v in rotor_position.items()}) | ||
38 | str = plugboard(str, plugboard_position) | 38 | str_d = plugboard(str_d, plugboard_position) | ||
39 | return func(str) | 39 | return func_d(str_d) | ||
40 | return str_decrypt | 40 | return str_decrypt | ||
41 | return decorator | 41 | return decorator | ||
42 | 42 | ||||
43 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | 43 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
44 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 44 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
45 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 45 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
46 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 46 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
47 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 47 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
48 | 'm': 'r', 'c': 'k'} | 48 | 'm': 'r', 'c': 'k'} | ||
49 | 49 | ||||
50 | rotor('enigma', rotor_position) # tnwora | 50 | rotor('enigma', rotor_position) # tnwora | ||
51 | plugboard('enigma', plugboard_position) # enigmc | 51 | plugboard('enigma', plugboard_position) # enigmc | ||
52 | 52 | ||||
53 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | 53 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | ||
54 | rotor_position=rotor_position) | 54 | rotor_position=rotor_position) | ||
55 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | 55 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | ||
56 | rotor_position=rotor_position) | 56 | rotor_position=rotor_position) | ||
57 | 57 | ||||
58 | encrypt_print = encryptor(print) | 58 | encrypt_print = encryptor(print) | ||
59 | decrypt_print = decryptor(print) | 59 | decrypt_print = decryptor(print) | ||
60 | 60 | ||||
61 | encrypt_print('enigma') # tnwork | 61 | encrypt_print('enigma') # tnwork | ||
62 | decrypt_print('tnwork') # enigma | 62 | decrypt_print('tnwork') # enigma |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def plugboard(str, connections): | n | 1 | def plugboard(input_str, connections): |
2 | dict = {} | 2 | plugboard_dict = {} | ||
3 | for connection in connections: | 3 | for connection in connections: | ||
n | 4 | letters = list(connection) | n | 4 | letter1, letter2 = connection |
5 | dict[letters[0]] = letters[1] | ||||
6 | dict[letters[1]] = letters[0] | 5 | plugboard_dict[letter1] = letter2 | ||
6 | plugboard_dict[letter2] = letter1 | ||||
7 | 7 | ||||
n | 8 | read = "" | n | 8 | output_str = "" |
9 | for letter in str: | 9 | for letter in input_str: | ||
10 | |||||
11 | if letter in dict: | 10 | if letter in plugboard_dict: | ||
12 | read += dict[letter] | 11 | output_str += plugboard_dict[letter] | ||
13 | else: | 12 | else: | ||
n | 14 | read += letter | n | 13 | output_str += letter |
15 | 14 | ||||
n | 16 | return read | n | 15 | return output_str |
17 | 16 | ||||
n | n | 17 | |||
18 | def rotor(str, dict): | 18 | def rotor(input_str, rotor_dict): | ||
19 | result = "" | 19 | result = "" | ||
t | 20 | for letter in str: | t | 20 | for letter in input_str: |
21 | if letter in dict: | 21 | result += rotor_dict.get(letter, letter) | ||
22 | result += dict[letter] | ||||
23 | else: | ||||
24 | result += letter | ||||
25 | return result | 22 | return result | ||
26 | 23 | ||||
27 | 24 | ||||
28 | def enigma_encrypt(plugboard_position, rotor_position): | 25 | def enigma_encrypt(plugboard_position, rotor_position): | ||
29 | def decorator(func): | 26 | def decorator(func): | ||
30 | def str_encrypt(str): | 27 | def str_encrypt(str): | ||
31 | str = plugboard(str, plugboard_position) | 28 | str = plugboard(str, plugboard_position) | ||
32 | str = rotor(str, rotor_position) | 29 | str = rotor(str, rotor_position) | ||
33 | return func(str) | 30 | return func(str) | ||
34 | return str_encrypt | 31 | return str_encrypt | ||
35 | return decorator | 32 | return decorator | ||
36 | 33 | ||||
37 | def enigma_decrypt(plugboard_position, rotor_position): | 34 | def enigma_decrypt(plugboard_position, rotor_position): | ||
38 | def decorator(func): | 35 | def decorator(func): | ||
39 | def str_decrypt(str): | 36 | def str_decrypt(str): | ||
40 | str = rotor(str, {v: k for k, v in rotor_position.items()}) | 37 | str = rotor(str, {v: k for k, v in rotor_position.items()}) | ||
41 | str = plugboard(str, plugboard_position) | 38 | str = plugboard(str, plugboard_position) | ||
42 | return func(str) | 39 | return func(str) | ||
43 | return str_decrypt | 40 | return str_decrypt | ||
44 | return decorator | 41 | return decorator | ||
45 | 42 | ||||
46 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | 43 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
47 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 44 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
48 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 45 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
49 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 46 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
50 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 47 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
51 | 'm': 'r', 'c': 'k'} | 48 | 'm': 'r', 'c': 'k'} | ||
52 | 49 | ||||
53 | rotor('enigma', rotor_position) # tnwora | 50 | rotor('enigma', rotor_position) # tnwora | ||
54 | plugboard('enigma', plugboard_position) # enigmc | 51 | plugboard('enigma', plugboard_position) # enigmc | ||
55 | 52 | ||||
56 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | 53 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | ||
57 | rotor_position=rotor_position) | 54 | rotor_position=rotor_position) | ||
58 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | 55 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | ||
59 | rotor_position=rotor_position) | 56 | rotor_position=rotor_position) | ||
60 | 57 | ||||
61 | encrypt_print = encryptor(print) | 58 | encrypt_print = encryptor(print) | ||
62 | decrypt_print = decryptor(print) | 59 | decrypt_print = decryptor(print) | ||
63 | 60 | ||||
64 | encrypt_print('enigma') # tnwork | 61 | encrypt_print('enigma') # tnwork | ||
65 | decrypt_print('tnwork') # enigma | 62 | decrypt_print('tnwork') # enigma |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|