1import copy
2
3def plugboard(the_text, matches):
4 from_set_to_str = ""
5 ready = ""
6
7 for character in the_text:
8 new_matches = ""
9 for one_pair in matches:
10 if character in one_pair:
11 new_matches = ''.join(one_pair)
12 from_set_to_str = copy.deepcopy(one_pair)
13 from_set_to_str.remove(character)
14 res=''.join(from_set_to_str)
15 ready += res
16 break
17 if character not in new_matches:
18 ready += character
19 return ready
20
21def rotor(the_text, matches):
22 ready=""
23
24 for character in the_text:
25 if character == " ":
26 ready += " "
27 if character in matches:
28 res = matches.get(character)
29 ready += res
30 return ready
31
32def enigma_encrypt(plugboard_position, rotor_position):
33 def encryptor(func):
34 def encrypt_print(txt):
35 first = plugboard(txt, plugboard_position)
36 result = rotor(first, rotor_position)
37 return func(result)
38 return encrypt_print
39 return encryptor
40
41def enigma_decrypt(plugboard_position, rotor_position):
42 def decryptor(func):
43 def decrypt_print(txt):
44 res = {v: k for k, v in rotor_position.items()}
45 first = rotor(txt, res)
46 result = plugboard(first, plugboard_position)
47 return func(result)
48 return decrypt_print
49 return decryptor
50
51
52
53
54
55plugboard_position = [{'a', 'c'}, {'t', 'z'}]
56rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p',
57 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q',
58 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o',
59 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l',
60 'm': 'r', 'c': 'k'}
61
62rotor('enigma', rotor_position) # tnwora
63plugboard('enigma', plugboard_position) # enigmc
64
65encryptor = enigma_encrypt(plugboard_position=plugboard_position,
66 rotor_position=rotor_position)
67decryptor = enigma_decrypt(plugboard_position=plugboard_position,
68 rotor_position=rotor_position)
69
70encrypt_print = encryptor(print)
71decrypt_print = decryptor(print)
72
73encrypt_print('enigma') # tnwork
74decrypt_print('tnwork') # enigma
tnwork
enigma
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
f | 1 | import copy | f | 1 | import copy |
2 | 2 | ||||
3 | def plugboard(the_text, matches): | 3 | def plugboard(the_text, matches): | ||
4 | from_set_to_str = "" | 4 | from_set_to_str = "" | ||
5 | ready = "" | 5 | ready = "" | ||
6 | 6 | ||||
7 | for character in the_text: | 7 | for character in the_text: | ||
8 | new_matches = "" | 8 | new_matches = "" | ||
9 | for one_pair in matches: | 9 | for one_pair in matches: | ||
10 | if character in one_pair: | 10 | if character in one_pair: | ||
11 | new_matches = ''.join(one_pair) | 11 | new_matches = ''.join(one_pair) | ||
12 | from_set_to_str = copy.deepcopy(one_pair) | 12 | from_set_to_str = copy.deepcopy(one_pair) | ||
13 | from_set_to_str.remove(character) | 13 | from_set_to_str.remove(character) | ||
14 | res=''.join(from_set_to_str) | 14 | res=''.join(from_set_to_str) | ||
15 | ready += res | 15 | ready += res | ||
16 | break | 16 | break | ||
17 | if character not in new_matches: | 17 | if character not in new_matches: | ||
18 | ready += character | 18 | ready += character | ||
19 | return ready | 19 | return ready | ||
20 | 20 | ||||
21 | def rotor(the_text, matches): | 21 | def rotor(the_text, matches): | ||
22 | ready="" | 22 | ready="" | ||
23 | 23 | ||||
24 | for character in the_text: | 24 | for character in the_text: | ||
25 | if character == " ": | 25 | if character == " ": | ||
26 | ready += " " | 26 | ready += " " | ||
t | 27 | for one_pair in matches: | t | 27 | if character in matches: |
28 | if character == str(one_pair): | 28 | res = matches.get(character) | ||
29 | res = matches[one_pair] | ||||
30 | ready += res | 29 | ready += res | ||
31 | break | ||||
32 | return ready | 30 | return ready | ||
33 | 31 | ||||
34 | def enigma_encrypt(plugboard_position, rotor_position): | 32 | def enigma_encrypt(plugboard_position, rotor_position): | ||
35 | def encryptor(func): | 33 | def encryptor(func): | ||
36 | def encrypt_print(txt): | 34 | def encrypt_print(txt): | ||
37 | first = plugboard(txt, plugboard_position) | 35 | first = plugboard(txt, plugboard_position) | ||
38 | result = rotor(first, rotor_position) | 36 | result = rotor(first, rotor_position) | ||
39 | return func(result) | 37 | return func(result) | ||
40 | return encrypt_print | 38 | return encrypt_print | ||
41 | return encryptor | 39 | return encryptor | ||
42 | 40 | ||||
43 | def enigma_decrypt(plugboard_position, rotor_position): | 41 | def enigma_decrypt(plugboard_position, rotor_position): | ||
44 | def decryptor(func): | 42 | def decryptor(func): | ||
45 | def decrypt_print(txt): | 43 | def decrypt_print(txt): | ||
46 | res = {v: k for k, v in rotor_position.items()} | 44 | res = {v: k for k, v in rotor_position.items()} | ||
47 | first = rotor(txt, res) | 45 | first = rotor(txt, res) | ||
48 | result = plugboard(first, plugboard_position) | 46 | result = plugboard(first, plugboard_position) | ||
49 | return func(result) | 47 | return func(result) | ||
50 | return decrypt_print | 48 | return decrypt_print | ||
51 | return decryptor | 49 | return decryptor | ||
52 | 50 | ||||
53 | 51 | ||||
54 | 52 | ||||
55 | 53 | ||||
56 | 54 | ||||
57 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | 55 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
58 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 56 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
59 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 57 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
60 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 58 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
61 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 59 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
62 | 'm': 'r', 'c': 'k'} | 60 | 'm': 'r', 'c': 'k'} | ||
63 | 61 | ||||
64 | rotor('enigma', rotor_position) # tnwora | 62 | rotor('enigma', rotor_position) # tnwora | ||
65 | plugboard('enigma', plugboard_position) # enigmc | 63 | plugboard('enigma', plugboard_position) # enigmc | ||
66 | 64 | ||||
67 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | 65 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | ||
68 | rotor_position=rotor_position) | 66 | rotor_position=rotor_position) | ||
69 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | 67 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | ||
70 | rotor_position=rotor_position) | 68 | rotor_position=rotor_position) | ||
71 | 69 | ||||
72 | encrypt_print = encryptor(print) | 70 | encrypt_print = encryptor(print) | ||
73 | decrypt_print = decryptor(print) | 71 | decrypt_print = decryptor(print) | ||
74 | 72 | ||||
75 | encrypt_print('enigma') # tnwork | 73 | encrypt_print('enigma') # tnwork | ||
76 | decrypt_print('tnwork') # enigma | 74 | decrypt_print('tnwork') # enigma |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import copy | f | 1 | import copy |
2 | 2 | ||||
3 | def plugboard(the_text, matches): | 3 | def plugboard(the_text, matches): | ||
n | 4 | from_set_to_str="" | n | 4 | from_set_to_str = "" |
5 | ready="" | 5 | ready = "" | ||
6 | 6 | ||||
7 | for character in the_text: | 7 | for character in the_text: | ||
8 | new_matches = "" | 8 | new_matches = "" | ||
9 | for one_pair in matches: | 9 | for one_pair in matches: | ||
10 | if character in one_pair: | 10 | if character in one_pair: | ||
11 | new_matches = ''.join(one_pair) | 11 | new_matches = ''.join(one_pair) | ||
12 | from_set_to_str = copy.deepcopy(one_pair) | 12 | from_set_to_str = copy.deepcopy(one_pair) | ||
13 | from_set_to_str.remove(character) | 13 | from_set_to_str.remove(character) | ||
14 | res=''.join(from_set_to_str) | 14 | res=''.join(from_set_to_str) | ||
15 | ready += res | 15 | ready += res | ||
16 | break | 16 | break | ||
17 | if character not in new_matches: | 17 | if character not in new_matches: | ||
18 | ready += character | 18 | ready += character | ||
19 | return ready | 19 | return ready | ||
20 | 20 | ||||
21 | def rotor(the_text, matches): | 21 | def rotor(the_text, matches): | ||
22 | ready="" | 22 | ready="" | ||
23 | 23 | ||||
24 | for character in the_text: | 24 | for character in the_text: | ||
25 | if character == " ": | 25 | if character == " ": | ||
26 | ready += " " | 26 | ready += " " | ||
t | 27 | for one_pair in matches: | t | 27 | for one_pair in matches: |
28 | if character == str(one_pair): | 28 | if character == str(one_pair): | ||
29 | res = matches[one_pair] | 29 | res = matches[one_pair] | ||
30 | ready += res | 30 | ready += res | ||
31 | break | 31 | break | ||
32 | return ready | 32 | return ready | ||
33 | 33 | ||||
34 | def enigma_encrypt(plugboard_position, rotor_position): | 34 | def enigma_encrypt(plugboard_position, rotor_position): | ||
35 | def encryptor(func): | 35 | def encryptor(func): | ||
36 | def encrypt_print(txt): | 36 | def encrypt_print(txt): | ||
37 | first = plugboard(txt, plugboard_position) | 37 | first = plugboard(txt, plugboard_position) | ||
38 | result = rotor(first, rotor_position) | 38 | result = rotor(first, rotor_position) | ||
39 | return func(result) | 39 | return func(result) | ||
40 | return encrypt_print | 40 | return encrypt_print | ||
41 | return encryptor | 41 | return encryptor | ||
42 | 42 | ||||
43 | def enigma_decrypt(plugboard_position, rotor_position): | 43 | def enigma_decrypt(plugboard_position, rotor_position): | ||
44 | def decryptor(func): | 44 | def decryptor(func): | ||
45 | def decrypt_print(txt): | 45 | def decrypt_print(txt): | ||
46 | res = {v: k for k, v in rotor_position.items()} | 46 | res = {v: k for k, v in rotor_position.items()} | ||
47 | first = rotor(txt, res) | 47 | first = rotor(txt, res) | ||
48 | result = plugboard(first, plugboard_position) | 48 | result = plugboard(first, plugboard_position) | ||
49 | return func(result) | 49 | return func(result) | ||
50 | return decrypt_print | 50 | return decrypt_print | ||
51 | return decryptor | 51 | return decryptor | ||
52 | 52 | ||||
53 | 53 | ||||
54 | 54 | ||||
55 | 55 | ||||
56 | 56 | ||||
57 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | 57 | plugboard_position = [{'a', 'c'}, {'t', 'z'}] | ||
58 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | 58 | rotor_position = {'v': 'd', 'd': 'v', 'y': 'u', 'n': 'n', 'i': 'w', 'z': 'p', | ||
59 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | 59 | 's': 'e', 'x': 's', 'h': 'f', 'b': 'x', 'u': 'c', 'p': 'q', | ||
60 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | 60 | 'r': 'g', 'q': 'j', 'e': 't', 'l': 'y', 'o': 'z', 'g': 'o', | ||
61 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | 61 | 'k': 'b', 't': 'h', 'j': 'm', 'a': 'a', 'w': 'i', 'f': 'l', | ||
62 | 'm': 'r', 'c': 'k'} | 62 | 'm': 'r', 'c': 'k'} | ||
63 | 63 | ||||
64 | rotor('enigma', rotor_position) # tnwora | 64 | rotor('enigma', rotor_position) # tnwora | ||
65 | plugboard('enigma', plugboard_position) # enigmc | 65 | plugboard('enigma', plugboard_position) # enigmc | ||
66 | 66 | ||||
67 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | 67 | encryptor = enigma_encrypt(plugboard_position=plugboard_position, | ||
68 | rotor_position=rotor_position) | 68 | rotor_position=rotor_position) | ||
69 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | 69 | decryptor = enigma_decrypt(plugboard_position=plugboard_position, | ||
70 | rotor_position=rotor_position) | 70 | rotor_position=rotor_position) | ||
71 | 71 | ||||
72 | encrypt_print = encryptor(print) | 72 | encrypt_print = encryptor(print) | ||
73 | decrypt_print = decryptor(print) | 73 | decrypt_print = decryptor(print) | ||
74 | 74 | ||||
75 | encrypt_print('enigma') # tnwork | 75 | encrypt_print('enigma') # tnwork | ||
76 | decrypt_print('tnwork') # enigma | 76 | decrypt_print('tnwork') # enigma |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|