-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateKeys.py
53 lines (46 loc) · 1.42 KB
/
generateKeys.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
import json
import sys
from fractions import gcd
from Crypto.Util import number
from keysModels import RSAPublicKey, RSAPrivateKey
from keysBase64 import encodeAndSavePrivate, encodeAndSavePublic
sys.setrecursionlimit(1000000) # long type,32bit OS 4B,64bit OS 8B(1bit for sign)
BITS = 1024 # desired bit strength of key (modulus)
# E = 65537
E = 3
def lcm(_a, _b):
return (_a *_b) // gcd(_a, _b)
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, x, y = egcd(b % a, a)
return (g, y - (b // a) * x, x)
def mulinv(b, n):
g, x, _ = egcd(b, n)
if g == 1:
return x % n
def generatePrime(bits):
prime = number.getPrime(bits)
while(egcd(prime-1, E)[0] != 1):
prime = number.getPrime(bits)
return prime
def generateKeys():
print('Generating primes...')
p = generatePrime(BITS/2+1)
q = generatePrime(BITS/2-1)
while (int.bit_length(p * q) != BITS):
q = generatePrime(BITS/2-1)
print('Calculating modulus...')
n = p * q
print('Calculating phi...')
phi = lcm(p-1, q-1)
print('Calculating private exponent...')
d = mulinv(E, phi)
print('Calculating exponent1...')
exponent1 = d % (p-1)
print('Calculating exponent2...')
exponent2 = d % (q-1)
print('Calculating coefficient...')
coefficient = mulinv(q, p)
return (RSAPrivateKey(n, E, d, p, q, exponent1, exponent2, coefficient), RSAPublicKey(n, E))