-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.py
46 lines (39 loc) · 867 Bytes
/
Generator.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
#Key generator
import random
from Primes import *
def generator():
return random.sample(primes,2)
def primeFactors(n):
factors = []
d = 2
step = 1
while d*d <= n:
while n>1:
while n%d == 0:
factors.append(d)
n = n/d
d += step
step = 2
return factors
#following function is from
#https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
def egcd(a, b):
x,y,u,v = 0,1,1,0
while a != 0:
q, r = b//a, b%a
m, n = x-u*q, y-v*q
b,a, x,y, u,v = a,r, u,v, m,n
gcd = b
return y
a = generator()
m = a[0] * a[1]
phi = (a[0] - 1) * (a[1] - 1)
b = primeFactors(phi)
c = random.sample(b,1)
print "Private Key:"
print "p = " + str(a[0])
print "q = " + str(a[1])
print "k inverse = " + str(egcd(phi,17)) + "\n"
print "Public Key:"
print "m" + " = " + str(m)
print "k = 17"