This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecipient.py
110 lines (92 loc) · 3.33 KB
/
recipient.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
import math
import random
from common.const import COUNT_BITS
class Recipient:
def __init__(self):
self.__primes = self.__sieve_sundarama(2000)
self.__private_key = None
def __create_keys(self):
seq, weight = self.__super_growing_sequence(COUNT_BITS)
r = None
q = None
private_key = None
public_key = None
"Сумма весов и поиск простого числа большего суммы"
for num in self.__primes:
if num > weight:
q = num
break
if q is not None:
"Поиск взаимно простого числа с q"
while True:
r = random.randint(2, q)
a, x2, y2 = self.__gsd_advance(q, r, 0, 1, 1, 0)
if a == 1:
break
if r is not None:
private_key = {
"sequence": seq,
"q": q,
"r": r,
}
public_key = [(s * r) % q for s in seq]
return public_key, private_key
def __gsd_advance(self, a: int, b: int, x1: int, x2: int, y1: int, y2: int):
"Расширенный алгоритм Евклида"
if b == 0:
return a, x2, y2
else:
tmp = a % b
div = a // b
a = b
b = tmp
adXi = x2 - div * x1
adYi = y2 - div * y1
return self.__gsd_advance(a, b, adXi, x1, adYi, y1)
def __super_growing_sequence(self, count: int):
"Создание супер-возрастающей последовательности"
first = random.randint(2, 5)
seq = [first]
sum = first
for i in range(count - 1):
num = sum + random.randint(2, 20) + i
sum += num
seq.append(num)
return seq, sum
def __sieve_sundarama(self, num: int):
"Получение простых чисел 'Решетом Сундарама'"
len = num + 1
primes = [i for i in range(len)]
res = [2]
for i in range(1, math.floor(math.sqrt(2 * num + 1) / 2)):
for j in range(i, math.floor((num - i) / (2 * i + 1))):
ind = i + j + 2 * i * j
if (ind) <= num:
primes[ind] = 0
for i in range(1, len):
if primes[i] != 0:
res.append(primes[i] * 2 + 1)
return res
def CreateKeys(self):
public_key, private_key = self.__create_keys()
self.__private_key = private_key
return public_key
def Decrypt(self, text: str):
q = self.__private_key["q"]
"Поиск обратного числа для r по модулю q"
a, x2, y2 = self.__gsd_advance(q, self.__private_key["r"], 0, 1, 1, 0)
while y2 < 0:
y2 += q
res = ""
seq = self.__private_key["sequence"]
for ch in text:
dec = 0
ind_arr = len(seq) - 1
dec_num = (ord(ch) * y2) % q
while dec_num > 0:
if seq[ind_arr] <= dec_num:
dec |= 1 << (COUNT_BITS - ind_arr - 1)
dec_num -= seq[ind_arr]
ind_arr -= 1
res += chr(dec)
return res