def genKeys(primeLength): p = modmath.findPrime(primeLength); q = modmath.findPrime(primeLength); n = p*q; phi = (p-1)*(q-1); e = int(random.random()*(n-1)) + 1; eeres = modmath.ExtendedEuclidean(phi,e); while(eeres.gcd != 1): e = int(random.random()*(n-1)) + 1; eeres = modmath.ExtendedEuclidean(phi,e); d=0; if(phi < e): d = eeres.s; else: d = eeres.t; while(d<0): d += phi; return KeyInfo(d,e,n);
def genKeys(n,t): while(True): #Continue until a n-1 bit prime is found with 2*q+1 also prime q = modmath.findPrime(n-1, t/2); p = 2*q+1; if(modmath.isPrime(p, t/2)): break; g = random.randint(2,p-1); #Random integer to try as primtive while(not modmath.isPrimitive(g,q,p)): g = random.randint(2,p-1); #Continue looking for primitive x = random.randint(2,p-1); #Pick a secret value x h = modmath.modexp(g,x,p); #Compute public h pub = PublicKey(p,g,h) #Generate key objects priv = PrivateKey(p,g,x) return KeyResults(pub, priv)