def RSAKeyGenerator(keysize=16): #define key_list for return n, e, and d key_list=[] #generate big prime p and q p=prime.generateLargePrime(keysize) q=prime.generateLargePrime(keysize) #calculate n n=(p)*(q) #calculate Fer based on Fermat's little theorem:a^p=(mod p) Fer=(p-1)*(q-1) key_list.append(n) key_list.append(Fer) #generate big prime as public key:e e=prime.generateLargePrime((int(keysize)*2-1)) ls_gcd=[] #search public key e which GCD(e,n)=1 while(True): ls_gcd=Euclid(e,Fer) gcd=ls_gcd[2] if(gcd==1): break e+=1 key_list.append(e) if(int(ls_gcd[1])<0): ls_gcd[1]=ls_gcd[1]+Fer key_list.append(ls_gcd[1]) #ck=(e*ls_gcd[1])%Fer #print(ck) return key_list
def testSSSS(availale_sharesize,needed_sharesize): #generate random big prime for S and N secret_key=prime.generateLargePrime(15) prime_num=prime.generateLargePrime(16) #generate split key (ls_coef,ls_shares)=splitSecret(secret_key,availale_sharesize,needed_sharesize,prime_num) print("Split prime secret into sharing list based on S-%s;N-%s:\n%s\n" %(secret_key,prime_num,ls_shares)) #Random choose sharing keys in available sharing list k_index=[] while(True): tmp=random.randrange(0,availale_sharesize) if(k_index.count(tmp) == 1): continue k_index.append(tmp) if(len(k_index)==needed_sharesize): break #Generate needed sharing key list ls_sk=[] for i in range(len(k_index)): ls_sk.append(ls_shares[k_index[i]]) print("Needed sharing key list:\n%s\n" %(ls_sk)) #merge sharing keys to prime key print("Merge needed sharing secret list to restore prime secret:\nprime S=%s; recovery S=%s" %(secret_key, mergeSecret(ls_sk,prime_num)))
def __init__(self, p=None, q=None): if not p: self.p = generateLargePrime(1024) else: self.p = p if not q: self.q = generateLargePrime(1024) else: self.q = q print('generating e') self.e = self.generate_e() print('e generated, primes generated until success: {} \n'.format( self.stats_e)) self.d = self.modinv(self.e, self.phi) print('d generated, recursion depth: {} \n'.format(self.stats_n))
def gen_hash_params(self): """ generates a, b and p and returns array of tuples""" m_bitlength = self.m.bit_length() hfs = [] for _ in xrange(self.k): p = generateLargePrime(m_bitlength + 7) a = random.randint(1, p) b = random.randint(1, p) hfs.append((a, b, p)) return hfs
def keygen(): p = prime.generateLargePrime(32) q = prime.generateLargePrime(32) n = p * q #calculating e relative prime to p-1 and q-1 while True: e = random.randrange(2**(1023), 2**(1024)) if rsamath.gcd(e, (p - 1) * (q - 1)) == 1: break #calculate d mod inverse of e d = rsamath.findModInverse(e, (p - 1) * (q - 1)) public_key = (n, e) private_key = (n, d) print "PUBLIC KEY\n", public_key print "Private Key\n", private_key return (public_key, private_key)
def getPrime(self, bits): while True: p = generateLargePrime(bits) if p & 3 == 3: return p