-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_RSA.py
More file actions
56 lines (56 loc) · 1.88 KB
/
gen_RSA.py
File metadata and controls
56 lines (56 loc) · 1.88 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
from random import randint
from math import sqrt
import numpy as np
def primes_numpy(n):
if n < 2:
return np.array([], dtype=int)
sieve = np.ones(n + 1, dtype=bool)
sieve[0] = sieve[1] = False
sieve[4::2] = False # Убираем четные, кроме 2
for i in range(3, int(n**0.5) + 1, 2):
if sieve[i]:
sieve[i*i::i] = False # Векторизованное удаление кратных
return np.nonzero(sieve)[0]
ma = primes_numpy(2**31)
print(len(ma))
p = ma[randint(300000,len(ma)-1)]#выбираем случайное простое число p;
q = ma[randint(300000,len(ma)-1)]#выбираем случайное простое число q;
while p == q:
p = ma[randint(300000, len(ma) - 1)] # выбираем случайное простое число p
n = p*q
phi = (p-1)*(q-1)# вычисляем φ(n)
i = len(ma)-1
flg = 3
while ma[i]>=phi:#находим примерную границу простых чисел меньше φ(n)
i = i//2
if ma[i]<phi and flg>0:
i = i+i//2
flg-=1
e = ma[randint(500, i - 1)]#выбираем случайное простое число меньшее φ(n)
a = e
while a!=1:
a = e
b = phi
while a != 0 and b != 0:
if a > b:
a = a % b
else:
b = b % a
if a!=1:
e = ma[randint(500, i - 1)]#проверяем на делители
def extended_gcd(a, b):
if b == 0:
return a, 1, 0
gcd, x1, y1 = extended_gcd(b, a % b)
x = y1
y = x1 - (a // b) * y1
return gcd, x, y
gcd, d, y = extended_gcd(e, phi)
if gcd != 1:
print("Ошибка: e и φ(n) не взаимно просты")
else:
d = d % phi # Приводим к положительному виду
print("e",e,sep="")
print("n",n,sep="")
print("d",d,sep="")
input()