-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask4.py
More file actions
61 lines (42 loc) · 1.26 KB
/
task4.py
File metadata and controls
61 lines (42 loc) · 1.26 KB
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
from Crypto.Util.number import inverse
def factor(num):
result = []
d = 2
while d * d <= num:
if num % d == 0:
result.append(d)
num //= d
else:
d += 1
if num > 1:
result.append(num)
return result
def blocking(num, size_num, size_block):
return [int.from_bytes(num.to_bytes(size_num, 'big')[i * size_block: (i + 1) * size_block], 'big') for i in
range(size_num // size_block)]
def decrypt(C, d, n):
bytes_val = blocking(C, 32, 4)
res = bytearray()
for num in bytes_val:
res += pow(num, d, n).to_bytes(8, 'big')
return int.from_bytes(res, 'big')
def encrypt(P, e, n):
bytes_val = blocking(P, 64, 8)
res = bytearray()
for num in bytes_val:
res += pow(num, e, n).to_bytes(4, 'big')
return int.from_bytes(res, 'big')
n = 471090785117207
e = 12377
C = 314999112281065205361706341517321987491098667
print('Зашифрованный текст:', C)
p, q = factor(n)
print('p =', p, ', q =', q)
f = (p - 1) * (q - 1)
print('f =', f)
d = inverse(e, f)
print('d =', d)
P = decrypt(C, d, n)
print('Расшифрованый текст:', P)
C = encrypt(P, e, n)
print('<Проверка> Зашифрованный текст:', C)