-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathБлочное шифрование.py
96 lines (75 loc) · 2.16 KB
/
Блочное шифрование.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
string = input()
strFirst = string[:len(string)//2]
strSecond = string[len(string)//2:]
def cryptAll(str1, str2):
a = 9
b = 5
m = 1024
y0 = 5
def Gamma(y):
gamma_list = []
for i in range(8):
y = (a * y + b) % m
gamma_list.append(y)
return gamma_list
def encrypt(str):
gamma = Gamma(y0)
result_str = ''
for i in range(len(str)):
result_str += chr(((ord(str[i]) + gamma[i])))
return result_str
def code(str):
key = '3'
mas = []
finalMas = []
while len(key) != len(str):
if key[-1] == '3':
key += '1'
elif key[-1] == '1':
key += '4'
elif key[-1] == '4':
key += '3'
for i in range(len(str)):
sym = ord(str[i]) + int(key[i])
mas.append(sym)
for i in range(len(str)):
finalMas.append(chr(mas[i]))
return ''.join(finalMas)
print(encrypt(str1) + code(str2))
encryptAll(encrypt(str1), code(str2))
def encryptAll(str1, str2):
a = 9
b = 5
m = 1024
y0 = 5
def decode(str):
key = '3'
mas = []
finalMas = []
while len(key) != len(str):
if key[-1] == '3':
key += '1'
elif key[-1] == '1':
key += '4'
elif key[-1] == '4':
key += '3'
for i in range(len(str)):
sym = ord(str[i]) - int(key[i])
mas.append(sym)
for i in range(len(str)):
finalMas.append(chr(mas[i]))
return ''.join(finalMas)
def Gamma(y):
gamma_list = []
for i in range(8):
y = (a * y + b) % m
gamma_list.append(y)
return gamma_list
def decrypt(str):
gamma = Gamma(y0)
result_str = ''
for i in range(len(str)):
result_str += chr(((ord(str[i]) - gamma[i])))
return result_str
print(decrypt(str1) + decode(str2))
cryptAll(strFirst, strSecond)