|
1
|
|
|
def GetKeyMatrix(key): |
|
2
|
|
|
key += 'abcdefghijklmnopqrstuvwxyz0123456789' |
|
3
|
|
|
Keys = [] |
|
4
|
|
|
[Keys.append(i) for i in key if i not in Keys] |
|
5
|
|
|
return Keys |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
def encode(message, key): |
|
9
|
|
|
KeyMatrix = GetKeyMatrix(key) |
|
10
|
|
|
|
|
11
|
|
|
# convert message |
|
12
|
|
|
message = [i for i in message.lower() if i in KeyMatrix] |
|
13
|
|
|
i = 0 |
|
14
|
|
|
Digraphs = [] |
|
15
|
|
|
while i <= len(message) - 1: |
|
16
|
|
|
# If needed, append a "z" to complete the final digraph (or "x" if the |
|
17
|
|
|
# last letter is "z") |
|
18
|
|
|
if i == len(message) - 1: |
|
19
|
|
|
if message[i] != 'z': |
|
20
|
|
|
Digraphs.append(message[i] + 'z') |
|
21
|
|
|
else: |
|
22
|
|
|
Digraphs.append(message[i] + 'x') |
|
23
|
|
|
i += 1 |
|
24
|
|
|
elif message[i] != message[i + 1]: |
|
25
|
|
|
Digraphs.append(''.join(message[i : i + 2])) |
|
26
|
|
|
i += 2 |
|
27
|
|
|
# If both letters are the same, add an "x" after the first letter (for |
|
28
|
|
|
# double "x" use "z" as completion character) |
|
29
|
|
|
elif message[i] == message[i + 1]: |
|
30
|
|
|
if message[i] != 'x': |
|
31
|
|
|
Digraphs.append(message[i] + 'x') |
|
32
|
|
|
else: |
|
33
|
|
|
Digraphs.append(message[i] + 'z') |
|
34
|
|
|
i += 1 |
|
35
|
|
|
else: |
|
36
|
|
|
print('FUCK!', message[i], message[i + 1]) |
|
37
|
|
|
raise |
|
38
|
|
|
|
|
39
|
|
|
EncryptedText = [] |
|
40
|
|
|
for i in Digraphs: |
|
41
|
|
|
row0 = KeyMatrix.index(i[0]) // 6 |
|
42
|
|
|
col0 = KeyMatrix.index(i[0]) % 6 |
|
43
|
|
|
row1 = KeyMatrix.index(i[1]) // 6 |
|
44
|
|
|
col1 = KeyMatrix.index(i[1]) % 6 |
|
45
|
|
View Code Duplication |
if row0 == row1: |
|
|
|
|
|
|
46
|
|
|
col0 += 1 |
|
47
|
|
|
col1 += 1 |
|
48
|
|
|
if col0 > 5: |
|
49
|
|
|
col0 -= 6 |
|
50
|
|
|
if col1 > 5: |
|
51
|
|
|
col1 -= 6 |
|
52
|
|
|
elif col0 == col1: |
|
53
|
|
|
row0 += 1 |
|
54
|
|
|
row1 += 1 |
|
55
|
|
|
if row0 > 5: |
|
56
|
|
|
row0 -= 6 |
|
57
|
|
|
if row1 > 5: |
|
58
|
|
|
row1 -= 6 |
|
59
|
|
|
else: |
|
60
|
|
|
col0, col1 = col1, col0 |
|
61
|
|
|
EncryptedText.append(KeyMatrix[row0 * 6 + col0] + KeyMatrix[row1 * 6 + col1]) |
|
62
|
|
|
return ''.join(EncryptedText) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def decode(secret_message, key): |
|
66
|
|
|
KeyMatrix = GetKeyMatrix(key) |
|
67
|
|
|
|
|
68
|
|
|
# convert message |
|
69
|
|
|
secret_message = [ |
|
70
|
|
|
secret_message[i : i + 2] for i in range(0, len(secret_message.lower()), 2) |
|
71
|
|
|
] |
|
72
|
|
|
message = [] |
|
73
|
|
|
for i in secret_message: |
|
74
|
|
|
row0 = KeyMatrix.index(i[0]) // 6 |
|
75
|
|
|
col0 = KeyMatrix.index(i[0]) % 6 |
|
76
|
|
|
row1 = KeyMatrix.index(i[1]) // 6 |
|
77
|
|
|
col1 = KeyMatrix.index(i[1]) % 6 |
|
78
|
|
View Code Duplication |
if row0 == row1: |
|
|
|
|
|
|
79
|
|
|
col0 -= 1 |
|
80
|
|
|
col1 -= 1 |
|
81
|
|
|
if col0 < 0: |
|
82
|
|
|
col0 += 6 |
|
83
|
|
|
if col1 < 0: |
|
84
|
|
|
col1 += 6 |
|
85
|
|
|
elif col0 == col1: |
|
86
|
|
|
row0 -= 1 |
|
87
|
|
|
row1 -= 1 |
|
88
|
|
|
if row0 < 0: |
|
89
|
|
|
row0 += 6 |
|
90
|
|
|
if row1 < 0: |
|
91
|
|
|
row1 += 6 |
|
92
|
|
|
else: |
|
93
|
|
|
col0, col1 = col1, col0 |
|
94
|
|
|
message.append(KeyMatrix[row0 * 6 + col0] + KeyMatrix[row1 * 6 + col1]) |
|
95
|
|
|
return ''.join(message) |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if __name__ == '__main__': |
|
99
|
|
|
# These "asserts" using only for self-checking and not necessary for |
|
100
|
|
|
# auto-testing |
|
101
|
|
|
assert ( |
|
102
|
|
|
encode("Fizz Buzz is x89 XX.", "checkio101") == 'do2y7mt22kry94y2y2' |
|
103
|
|
|
), "Encode fizz buzz" |
|
104
|
|
|
assert ( |
|
105
|
|
|
decode("do2y7mt22kry94y2y2", "checkio101") == 'fizxzbuzzisx89xzxz' |
|
106
|
|
|
), "Decode fizz buzz" |
|
107
|
|
|
assert encode("How are you?", "hello") == 'ea2imb1ht0', "Encode How are you" |
|
108
|
|
|
assert decode("ea2imb1ht0", "hello") == 'howareyouz', "Decode How are you" |
|
109
|
|
|
assert encode("My name is Alex!!!", "alexander") == 'i1dlkxjqlexn', "Encode Alex" |
|
110
|
|
|
assert decode("i1dlkxjqlexn", "alexander") == 'mynameisalex', "Decode Alex" |
|
111
|
|
|
assert encode("Who are you?", "human") == 'rnvftc1jd5', "Encode WHo" |
|
112
|
|
|
assert decode("rnvftc1jd5", "human") == 'whoareyouz', "Decode Who" |
|
113
|
|
|
assert encode("ATTACK AT DAWN", "general") == 'ewwektewhnua', "Encode attack" |
|
114
|
|
|
assert decode("ewwektewhnua", "general") == 'attackatdawn', "Decode attack" |
|
115
|
|
|
|