/
base_alternative_v2_solveur.py
449 lines (368 loc) · 12.3 KB
/
base_alternative_v2_solveur.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from base_opt import *
import random as r
from mouchar import *
#Author : CABOS Matthieu
#Date : 02/2020
"""
Description of Crypter
======================
**Main Raptor Cryptographic Alternative Algorithm**
_________________________________________________________________
**Algorithm**
-------------
This is the main Raptor Cryptographic Alternative algorithm v2.
The difference between both versions is the type of the numbers list. The second version is using
a representation of float crypted preserving the full precision of the values.
This one is stable on his definition's domain and could be considered as the first one as 'fast crypting algorithm'.
There are differents ways to use :
* Cybersecurity of business and organization (Hospitals, banks, etc)
* Crypting data stream on the web
* Crypting authentification informations
This algorithm is ruled by the followings steps :
* Define two differents sets :
* The sep Set representing terms separators
* The vir Set reprseneting the comma in float values
* Getting raw string as input
* Converting ASCII values to their decimal correspondence
* Dividing each i+1_term of the list by the i_term of the list
* Building key from the given formula : key(i)=((l(i)*l(i+1) modulo l(i+2)) modulo 26)
* Multiplying each term by 10000
* Building the mirror key from the original one
* Compute each fraction division float value. Each fraction is defined by res(i)/key(i+1). Each part of the value is represented into a single integer value
* Multiplying each float res by 10 to get larger values (useful to Base Table converter)
* Convert into key-indexed Base Table values
* Defining commas and separators from the vir and sep Sets
* Return the full crypted string
_________________________________________________________________
**Source Code**
---------------
# Algorithme de cryptage
# txt=input("Entrez un texte")
txt="salut bande de nazes"
# txt="une autre chaine pour"
# txt="salut bande de nazes, on va tester une chaine de grande longueur pour verifier si l'algorithme fonctionne toujours"
l=[]
res=[]
for i in range(len(txt)):
l.append(ord(txt[i]))
print("l = ")
print(l)
print("#################################")
first=int(l[0])
for i in range(0,len(l)-1):
res.append(float(l[i+1]/l[i]))
key=[]
for i in range(0,len(l)-2): # Finir la chaine de texte par trois caractères "usuels", par exemple "..."
key.append(int((l[i]*l[i+1])%l[i+2])) # Eventuellement ameliorer la clé en la complementant a 36 sur [10,36]
key[i]=(key[i])%26
print("res =")
print(res)
print("#################################")
for i in range(len(res)):
res[i]=int(res[i]*10000)
res.append(first)
key.append(key[0]) #key padding
key.append(key[1])
print(len(res))
print(len(key))
print("key = ")
print(key)
print("################################")
print("res =")
print(res)
print("################################")
tmp=0
Float_res=[]
Mirror_key=[]
Mirror_key=mirror(key)
Mirror_key.append(first)
# Compute each fraction division float value. Each fraction is defined by res(i)/key(i+1)
# Each part of the value is represented into a single integer value
for i in range(len(res)):
tmp = res[i]/(key[i]+1)
Float_res.append(int(tmp))
Float_res.append(int((tmp-int(tmp))*1000))
tmp=0.0
print("Float_res = ")
print(Float_res)
print("################################")
print(len(Float_res))
print(len(Mirror_key))
#Multiplying each float res by 10 to get larger values (useful to Base Table converter)
for i in range(len(Float_res)):
Float_res[i]*=10
#Convert into key-indexed Base Table values
crypt=[]
for i in range(len(Float_res)):
crypt.append(table[Mirror_key[i]][Float_res[i]])
print(crypt)
print("################################")
# rajouter des operations de listes reversibles
string=""
ind=0
# Defining commas and separators from the vir and sep Sets
for i in range(len(crypt)):
string+=crypt[i]
if(ind%2==0):
string+=vir[r.randint(0,25)]
else:
string+=sep[r.randint(0,13)]
ind+=1
# Return the full crypted string
print("string = ")
print(string)
print("################################")
Description of De-Crypter
=========================
**Main Raptor Cryptographic Alternative Algorithm**
_________________________________________________________________
**Algorithm**
-------------
To decrypt the obtained string sequence from the Crypter, you have to follow these steps :
* Rebuild the terms list from the given string using sep and vir Sets
* Convert crypted value to their integer index
* Devide each of value by 10 to get the smaller origianl values
* Rebuild float values from the integer couples values
* Round multiplication of float value and Mirror key value to rebuild terms
* Divide each computed values from multiplication of i_term fo the float list with the last computed term by 10000 to get origianls terms
* Round and convert to ASCII values to get the original string
_________________________________________________________________
**Source Code**
---------------
rez=[]
tmp=''
ind=0
first=Mirror_key[-1]
Mirror_key=Mirror_key[:-1]
# Rebuild the terms list from the given string using sep and vir Sets
for item in string:
if not item in sep and not item in vir:
tmp+=item
# print(tmp)
else:
# Convert crypted value to their integer index
rez.append(table[Mirror_key[ind]].index(tmp))
tmp=''
ind+=1
firstt=rez[-1]
rez=rez[:-1]
print("rez =")
print(rez)
print("################################")
# Devide each of value by 10 to get the smaller origianl values
for i in range(len(rez)):
rez[i]/=10
Float_rez=[]
# Rebuild float values from the integer couples values
for i in range(0,len(rez)):
if(i%2==0):
tmp=rez[i]
else:
Float_rez.append(tmp+rez[i]/1000)
tmp=0.0
print("Float_rez = ")
print(Float_rez)
print("################################")
# Algorithme de décryptage
# Round multiplication of float value and Mirror key value to rebuild terms
rez=[]
for i in range(len(Float_rez)):
rez.append(round(Float_rez[i]*(Mirror_key[i]+1)))
print("rez =")
print(rez)
print("################################")
rezz=[]
rezz.append((first*rez[0])/10000)
print("rez =")
print(rez)
print("################################")
# Divide each computed values from multiplication of i_term fo the float list with the last computed term by 10000 to get origianls terms
for i in range(1,len(rez)):
rezz.append((rez[i]*rezz[i-1])/10000)
print(rezz)
final=[]
# Round and convert to ASCII values to get the original string
for i in range(len(rezz)):
final.append(chr(round(rezz[i])))
txt=[]
txt.append(chr(first))
for i in range(len(final)):
txt+=final[i]
print(txt)
"""
sep=['!','"','#','$','%','&','(',')','*','+',',','-','.','/']
vir=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
# Construction de la table des bases
table=table()
def mirror(liste):
"""
The mirror function build a mirror list from the given one.
=============== =========== ========================
**Parameters** **Type** **Description**
**liste** list The list to be treat
=============== =========== ========================
:Returns: list : The mirror list from the given parameter.
"""
res=liste[:]
for i in range(1,len(liste)):
res.append(liste[-i])
res.append(liste[0])
return res
# # Algorithme de cryptage
# # txt=input("Entrez un texte")
# txt="salut bande de nazes"
# # txt="une autre chaine pour"
# # txt="salut bande de nazes, on va tester une chaine de grande longueur pour verifier si l'algorithme fonctionne toujours"
# l=[]
# res=[]
# for i in range(len(txt)):
# l.append(ord(txt[i]))
# # print("l = ")
# # print(l)
# # print("#################################")
# first=int(l[0])
# for i in range(0,len(l)-1):
# res.append(float(l[i+1]/l[i]))
# key=[]
# for i in range(0,len(l)-2): # Finir la chaine de texte par trois caractères "usuels", par exemple "..."
# key.append(int((l[i]*l[i+1])%l[i+2])) # Eventuellement ameliorer la clé en la complementant a 36 sur [10,36]
# key[i]=(key[i])%26
# # print("res =")
# # print(res)
# # print("#################################")
# for i in range(len(res)):
# res[i]=int(res[i]*10000)
# res.append(first)
# key.append(key[0]) #key padding
# key.append(key[1])
# # print(len(res))
# # print(len(key))
# # print("key = ")
# # print(key)
# # print("################################")
# # print("res =")
# # print(res)
# # print("################################")
# tmp=0
# Float_res=[]
# Mirror_key=[]
# Mirror_key=mirror(key)
# Mirror_key.append(first)
# # Compute each fraction division float value. Each fraction is defined by res(i)/key(i+1)
# # Each part of the value is represented into a single integer value
# for i in range(len(res)):
# tmp = res[i]/(key[i]+1)
# Float_res.append(int(tmp))
# Float_res.append(int((tmp-int(tmp))*1000))
# tmp=0.0
# # print("Float_res = ")
# # print(Float_res)
# # print("################################")
# # print(len(Float_res))
# # print(len(Mirror_key))
# #Multiplying each float res by 10 to get larger values (useful to Base Table converter)
# for i in range(len(Float_res)):
# Float_res[i]*=10
# #Convert into key-indexed Base Table values
# crypt=[]
# for i in range(len(Float_res)):
# crypt.append(table[Mirror_key[i]][Float_res[i]])
# # print(crypt)
# # print("################################")
# # rajouter des operations de listes reversibles
# string=""
# ind=0
# # Defining commas and separators from the vir and sep Sets
# for i in range(len(crypt)):
# string+=crypt[i]
# if(ind%2==0):
# string+=vir[r.randint(0,25)]
# else:
# string+=sep[r.randint(0,13)]
# ind+=1
# ind=0
# str_key=''
# for i in range(len(Mirror_key)):
# str_key+=str(Mirror_key[i])
# str_key+=sep[r.randint(0,13)]
# print("key : ")
# print(str_key)
# # Return the full crypted string
# print("################################")
# print("string = ")
# print(string)
# print("################################")
# quit()
#######################################################################################################
string=input('Chaine Cryptée : ')
str_key=input('Clé : ')
Mirror_key=[]
tmp=''
ind=0
for item in str_key:
if not item in sep :
tmp+=item
else:
Mirror_key.append(int(tmp))
tmp=''
ind+=1
rez=[]
tmp=''
ind=0
first=Mirror_key[-1]
Mirror_key=Mirror_key[:-1]
# Rebuild the terms list from the given string using sep and vir Sets
for item in string:
if not item in sep and not item in vir:
tmp+=item
# print(tmp)
else:
# Convert crypted value to their integer index
rez.append(table[Mirror_key[ind]].index(tmp))
tmp=''
ind+=1
firstt=rez[-1]
rez=rez[:-1]
# print("rez =")
# print(rez)
# print("################################")
# Devide each of value by 10 to get the smaller origianl values
for i in range(len(rez)):
rez[i]/=10
Float_rez=[]
# Rebuild float values from the integer couples values
for i in range(0,len(rez)):
if(i%2==0):
tmp=rez[i]
else:
Float_rez.append(tmp+rez[i]/1000)
tmp=0.0
# print("Float_rez = ")
# print(Float_rez)
# print("################################")
# Algorithme de décryptage
# Round multiplication of float value and Mirror key value to rebuild terms
rez=[]
for i in range(len(Float_rez)):
rez.append(round(Float_rez[i]*(Mirror_key[i]+1)))
# print("rez =")
# print(rez)
# print("################################")
rezz=[]
rezz.append((first*rez[0])/10000)
# print("rez =")
# print(rez)
# print("################################")
# Divide each computed values from multiplication of i_term fo the float list with the last computed term by 10000 to get origianls terms
for i in range(1,len(rez)):
rezz.append((rez[i]*rezz[i-1])/10000)
# print(rezz)
final=[]
# Round and convert to ASCII values to get the original string
for i in range(len(rezz)):
final.append(chr(round(rezz[i])))
txt=""
txt=(chr(first))
for i in range(len(final)):
txt+=final[i]
print(txt)