-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerador_clave_wiener.py
72 lines (55 loc) · 1.66 KB
/
generador_clave_wiener.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
import gmpy2, random
from gmpy2 import isqrt, c_div
# Genrate pubkey
from Crypto.PublicKey.RSA import construct
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
urandom = random.SystemRandom()
def get_prime(size):
while True:
r = urandom.getrandbits(size)
if gmpy2.is_prime(r): # Miller-rabin
return r
def test_key(N, e, d):
msg = (N - 123) >> 7
c = pow(msg, e, N)
return pow(c, d, N) == msg
def create_keypair(size):
while True:
p = get_prime(size // 2)
q = get_prime(size // 2)
# Comprobando condición Wiener Attack
if q < p < 2*q:
break
N = p * q
phi_N = (p - 1) * (q - 1)
# Recall that: d < (N^(0.25))/3
max_d = c_div(isqrt(isqrt(N)), 3)
max_d_bits = max_d.bit_length() - 1
while True:
d = urandom.getrandbits(max_d_bits)
try:
e = int(gmpy2.invert(d, phi_N))
except ZeroDivisionError:
continue
if (e * d) % phi_N == 1:
break
assert test_key(N, e, d)
return N, e, d, p, q
if __name__ == "__main__":
N, e, d, p, q = create_keypair(32)
print(f"N: {N}\ne: {e}\nd: {d}\np: {p}\nq: {q}")
# Construct pubkey
rsaKey = construct((N, e))
pubKey = rsaKey.publickey()
pubKeyPEM = rsaKey.exportKey()
print(pubKeyPEM.decode('ascii'))
with open('my_pubkey.key', 'wb') as f:
f.write(pubKeyPEM)
# Construct privkey
privKey = construct((N, e, d, p, q))
print(privKey.has_private())
privKeyPEM = privKey.exportKey()
with open("my_privkey.key", 'wb') as f:
f.write(privKeyPEM)
print(privKeyPEM.decode('ascii'))