This repository was archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
293 lines (238 loc) · 9.1 KB
/
crypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# crypting and decrypting functions
# about caesar, vigenere, polybe and
# affine
def crypt(crypting_type, code, keys):
# crypting_type is a string representing the kind of crypting method
# code is also a string
# keys is a list
try:
if crypting_type == "affine":
return affine_crypt(code, keys[0], keys[1])
elif crypting_type == "caesar":
return caesar_crypt(code, keys[0])
elif crypting_type == "vigenere":
return vigenere_crypt(code, keys[0])
elif crypting_type == "polybe":
return polybe_crypt(code, keys[0])
else:
print("I do not know this crypting method")
return code
except Exception as e:
print("Got an error when crypting, probably wrong keys")
print("=" * 15)
print(crypting_type, code, keys)
print("-" * 15)
print(e)
print("=" * 15)
return code
def decrypt(crypting, code, keys):
# crypting is a string representing the kind of scripting
# code is the crypted message (string)
# keys is a list
try:
if crypting == "affine":
return affine_decrypt(code, keys[0], keys[1])
elif crypting == "caesar":
return caesar_decrypt(code, keys[0])
elif crypting == "vigenere":
return vigenere_decrypt(code, keys[0])
elif crypting == "polybe":
return polybe_decrypt(code, keys[0])
else:
print("I do not know this decrypting method")
return code
except Exception:
print("Got an error when decrypting, probably wrong keys")
print("=" * 15)
print(crypting, code, keys)
print("-" * 15)
print(e)
print("=" * 15)
return code
def affine_crypt(code, key, key2):
code = code.lower() # we turn the whole code into lowercase
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
# an inline function to get easily access to the crypted character giving the index of
# the character in the message
# is strictly the equivalent of f: x |--> (ax + b)[27]
get_character_index = lambda x: (key * x + key2) % len(alphabet)
crypted = []
for c in code:
# we get the new character at alphabet[index]
# with index = get_character_index(x)
# with x = the index of the character in the alphabet
crypted.append(alphabet[get_character_index(alphabet.index(c))])
return "".join(crypted)
def affine_decrypt(code, key, key2):
decrypted = []
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
k = 0
p_second = lambda _k: -(_k * key2) % len(alphabet)
while (k * key) % len(alphabet) != 1:
k += 1
for c in code:
decrypted.append(alphabet[(k * alphabet.index(c) + p_second(k)) % len(alphabet)])
return "".join(decrypted)
def caesar_crypt(code, key):
key = key.lower() # we turn the whole key into lowercase, same with the code
code = code.lower()
crypted = []
# we create a list containing the abc,
# one element => one letter
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
# create the new abc regarding to the given key
# example :
# key = 'd'
# crypted_abc = 'defghijklmnopqrstuvwxyz' + 'abc'
# we cut the abc list where the key is and reassemble it
crypted_abc = alphabet[alphabet.index(key):] + alphabet[:alphabet.index(key)]
for c in code:
# for each character in code
# we add a character to crypted :
# which is located in crypted_abc at its index in the abc
# example :
# c = 'e'
# its index in the abc is 4 (a is 0)
# so we take the character in the crypted_abc at the index 4
# if the key is 'd' as before, the new character will be 'h'
crypted.append(crypted_abc[alphabet.index(c)])
# we create a string from each character contained in the list named crypted
return "".join(crypted)
def caesar_decrypt(code, key):
decrypted = []
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
# create the new abc regarding to the given key
# example :
# key = 'd'
# crypted_abc = 'defghijklmnopqrstuvwxyz' + 'abc'
# we cut the abc list where the key is and reassemble it
crypted_abc = alphabet[alphabet.index(key):] + alphabet[:alphabet.index(key)]
for c in code:
decrypted.append(alphabet[crypted_abc.index(c)])
return "".join(decrypted)
def vigenere_crypt(code, key):
crypted = []
key = key.lower() # we turn the wholme key into lowercase, same with the code
code = code.lower()
# we create a list containing the abc,
# one element => one letter
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
# we create the crypted abc as a list
crypted_abc = []
# we must reformat the key to eliminate the duplicated characters
reformatted_key = []
for c in key:
# for each character in the key :D
if c not in reformatted_key:
# if the character is not in the reformatted_key list
# we add it to the list
reformatted_key.append(c)
for c in reformatted_key:
# we add the key to our new abc
crypted_abc.append(c)
for c in alphabet:
# for each character in the abc
if c not in crypted_abc:
# if the character is not in the new abc, we add it to our crypted abc
crypted_abc.append(c)
for c in code:
# for each character in code
# we add a character to crypted :
# which is located in crypted_abc at its index in the abc
crypted.append(crypted_abc[alphabet.index(c)])
return "".join(crypted)
def vigenere_decrypt(code, key):
decrypted = []
# we create a list containing the abc,
# one element => one letter
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
# we create the crypted abc as a list
crypted_abc = []
# we must reformat the key to eliminate the duplicated characters
reformatted_key = []
for c in key:
# for each character in the key :D
if c not in reformatted_key:
# if the character is not in the reformatted_key list
# we add it to the list
reformatted_key.append(c)
for c in reformatted_key:
# we add the key to our new abc
crypted_abc.append(c)
for c in alphabet:
# for each character in the abc
if c not in crypted_abc:
# if the character is not in the new abc, we add it to our crypted abc
crypted_abc.append(c)
for c in code:
decrypted.append(alphabet[crypted_abc.index(c)])
return "".join(decrypted)
def polybe_crypt(code, key):
square = []
for i in range(key):
# we have 6 lines
# we add an array representing a line
square.append([])
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
index = 0
# for each letter in the alphabet we place them in the array
for c in alphabet:
# if the size of the line is >= to 6 (ex if key = 6)
if len(square[index]) > key - 1:
index += 1
square[index].append(c)
crypted = []
for c in code:
for y, line in enumerate(square):
for x, elem in enumerate(line):
if elem.lower() == c:
crypted.append(str(x) + str(y))
break
return "".join(crypted)
def polybe_decrypt(code, key):
decrypted = []
square = []
for i in range(key):
# we have 6 lines
# we add an array representing a line
square.append([])
alphabet = list("abcdefghijklmnopqrstuvwxyz ")
index = 0
# for each letter in the alphabet we place them in the array
for c in alphabet:
# if the size of the line is >= to 6 (ex if key = 6)
if len(square[index]) > key - 1:
index += 1
square[index].append(c)
couples = []
for c in code:
if not couples or len(couples[-1]) == 2:
couples.append([int(c)])
else:
couples[-1].append(int(c))
for cc in couples:
decrypted.append(square[cc[1]][cc[0]])
return "".join(decrypted)
# if we start the script directly, not when we import it
if __name__ == '__main__':
# tests
msg = "ceci est un test"
print(msg)
cc = caesar_crypt(msg, "e")
vc = vigenere_crypt(msg, "vigenere")
pc = polybe_crypt(msg, 7)
ac = affine_crypt(msg, 2, 4)
print("Crypting")
print("Caesar", cc, sep="\n")
print("Vigenere", vc, sep="\n")
print("Polybe", pc, sep="\n")
print("Affine", ac, sep="\n")
dcc = caesar_decrypt(cc, "e")
dvc = vigenere_decrypt(vc, "vigenere")
dpc = polybe_decrypt(pc, 7)
dac = affine_decrypt(ac, 2, 4)
print("\nDecrypting")
print("Caesar", dcc, sep="\n")
print("Vigenere", dvc, sep="\n")
print("Polybe", dpc, sep="\n")
print("Affine", dac, sep="\n")