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)
def decrypt(c, d, n): return modmath.modexp(c,d,n);
def encrypt(m, e, n): return modmath.modexp(m,e,n);
def decrypt(cipher, key): s = modmath.modexp(cipher.c, key.x, key.p) return ((cipher.d * modmath.modexp(s,key.p - 2,key.p)) % key.p);
def encrypt(m, key): y = random.randint(0,key.p-1); c = modmath.modexp(key.g, y, key.p); d = (m * modmath.modexp(key.h, y, key.p)) % key.p; return CipherBlock(c,d);