-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.py
38 lines (30 loc) · 1002 Bytes
/
key.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
from maths import Maths
class Key:
"""Generates an RSA key pair"""
def __init__(self, bitlength, private, public):
self.bitlength = bitlength
self.private = private
self.public = public
@staticmethod
def generate_key(bitlength):
e = 0
while e == 0:
p, q = Maths.generate_prime((int)(bitlength / 2)), \
Maths.generate_prime((int)(bitlength / 2))
n = p * q
phi = ((p - 1) * (q - 1))
# If it's not one of these might as well just restart
if Maths.is_coprime(3, phi):
e = 3
elif Maths.is_coprime(17, phi):
e = 17
elif Maths.is_coprime(65537, phi):
e = 65537
gcd = Maths.extended_gcd(e, phi)
d = (gcd[0] + phi) % phi
private = [n, d]
public = [n, e]
return Key(bitlength, private, public)
key = Key.generate_key(1024)
print(key.private)
print(key.public)