def get_keys(e): pgen = PrimeGenerator(bits=128) pq_cond = False while not pq_cond: p, q = (pgen.findPrime(), pgen.findPrime()) p_bv, q_bv = BitVector(size=128, intVal=p), BitVector(size=128, intVal=q) # now test to see if the conditions are met pq_cond = True # check that the first bits are set if p_bv[0] == 0 or q_bv[0] == 0: pq_cond = False # check that p and q are not equal if p == q: pq_cond = False # check that p-1 and q-1 are coprime to e if gcd_euclid(p - 1, e) != 1 or gcd_euclid(q - 1, e) != 1: pq_cond = False # if any of the above checks are not passed, a new p and q will be generated and checked # calculate n value from p and q n = p * q # find d, the MI of e in mod n e_bv = BitVector(intVal=e) d = e_bv.multiplicative_inverse(BitVector(intVal=n)).int_val() return e, d, n
def __init__(self, master=None): self.generator = BlumMicaliGenerator( base=PrimeGenerator.get_prime(digits=2), modulus=PrimeGenerator.get_prime(digits=20), root=PrimeGenerator.get_prime(digits=5)) super().__init__(master) self.master = master self.pack() self._create_widgets()
def rsa_enc(input_fname: str, output_fname: str): e = 65537 #given enc exponent pgen = PrimeGenerator(bits=128) pq_cond = False while (pq_cond == False): p, q = (pgen.findPrime(), pgen.findPrime()) p_bv, q_bv = BitVector(size=128, intVal=p), BitVector(size=128, intVal=q) # now test to see if the conditions are met pq_cond = True # check that the first bits are set if p_bv[0] == 0 or q_bv[0] == 0: pq_cond = False # check that p and q are not equal if p == q: pq_cond = False # check that p-1 and q-1 are coprime to e if gcd_euclid(p - 1, e) != 1 or gcd_euclid(q - 1, e) != 1: pq_cond = False # if any of the above checks are not passed, a new p and q will be generated and checked # save p and q values in text documents for use with the decryption algorithm pFile = open('p.txt', 'w') qFile = open('q.txt', 'w') pFile.write(str(p)) qFile.write(str(q)) pFile.close() qFile.close() e_bv = BitVector(intVal=e) d_bv = e_bv.multiplicative_inverse( BitVector(intVal=(p - 1) * (q - 1), size=256)) d = d_bv.int_val() dFile = open('d.txt', 'w') dFile.write(str(d)) # now we can encrypt the plaintext using e, p, and q input_bv = BitVector(filename=input_fname) output_file = open(output_fname, 'w') while input_bv.more_to_read: one_block_bv = input_bv.read_bits_from_file(128) one_block_bv.pad_from_right( 128 - one_block_bv.length()) # pad the final block from the right one_block_bv.pad_from_left( 128) # pad each block from the left to make 256 bits # encrypt the block enc_block_bv = BitVector(intVal=pow(one_block_bv.int_val(), e, p * q), size=256) # write the block to the output file in hex format hexstr = enc_block_bv.get_hex_string_from_bitvector() output_file.write(hexstr)
def generate_primes_p_q(e): from PrimeGenerator import PrimeGenerator generator = PrimeGenerator(bits=128, debug=0, emod=e) p = 0 q = 0 while True: p = generator.findPrime() q = generator.findPrime() if verify_primes_p_q(p, q): # Found valid primes break return (p, q)
def genPrimes(): generator = PrimeGenerator(bits=128) while (1): p, q = generator.findPrime(), generator.findPrime() if not ((p & (1 << 127)) | (p & (1 << 126)) | (p & 1)): ##check if appropriate bits are not set continue if not ((q & (1 << 127)) | (q & (1 << 126)) | (q & 1)): ##check if appropriate bits are not set continue if p == q: continue if (gcd(p - 1, E_KEY) != 1): continue if (gcd(q - 1, E_KEY) != 1 ): # using the shortcut from page 25 and 26 instead of using gcdb continue break return (p, q)
def getPQD(n123): c = True generator1 = PrimeGenerator(bits=128, debug=0) generator2 = PrimeGenerator(bits=128, debug=0) count = 0 while c == True: p = generator1.findPrime() q = generator2.findPrime() pbv = BitVector(intVal=p, size=128) qbv = BitVector(intVal=q, size=128) #print p, q if (not (p != q)): c = True elif (not ((pbv[0] == 1) and (qbv[0] == 1))): c = True elif (not ((int(BitVector(intVal=p - 1).gcd(ebv)) == 1) and (int(BitVector(intVal=q - 1).gcd(ebv)) == 1))): c = True else: nbv = BitVector(intVal=p * q, size=256) n = int(nbv) if (int(n123[0].gcd(nbv)) == 1) and (int( n123[1].gcd(nbv)) == 1) and (int(n123[2].gcd(nbv)) == 1): #print count n123[count] = nbv count = count + 1 if count >= 3: c = False else: c = True return n123
def encrypt(self, file, wfile): print("Encrypting") list = [] e = 65537 list1 = [] self.file = file self.wfile = wfile print(file) f = open(file, 'rb') #reading from message file o = open(wfile, 'w+') w3 = open("MyFile.txt", 'w+') while True: generator = PrimeGenerator(bits=128, debug=0, emod=65537) prime = generator.findPrime() generator1 = PrimeGenerator(bits=128, debug=0, emod=65537) prime1 = generator1.findPrime() prime3 = prime >> 126 prime4 = prime1 >> 126 if (prime != prime1): #to make sure two primes are not the same gcd1 = self.gcd(prime - 1, e) gcd2 = self.gcd(prime1 - 1, e) if ((gcd1 == 1) and (gcd2 == 1)): # to check if gcd((prime-1),e)=1 if ((prime3 == 3) and (prime4 == 3)): #checking if left two bits are 1 break n = prime * prime1 w3.write("{} ".format(str(prime))) w3.write(str(prime1)) while True: buf = f.read(16) if buf: if (len(buf) < 16): while (len(buf) < 16): buf = buf + '\n' list.append(buf) else: break f.close() for x in range(0, len(list)): t = list[x] list[x] = t.zfill(32) for u in range(0, (len(list))): print(list[u]) t = ''.join(list) print(t) r = '' for u in range(0, (len(list))): y1 = list[u] list3 = [] for r2 in range(0, len(list[u])): list3.append((str(pow(ord(y1[r2]), e, n))) + ' ') t5 = ' '.join(list3) o.write(t5)
def getPQD(): c = True generator1 = PrimeGenerator( bits = 128, debug = 0 ) generator2 = PrimeGenerator( bits = 128, debug = 0 ) while c == True: p = generator1.findPrime() q = generator2.findPrime() pbv = BitVector(intVal = p, size = 128) qbv = BitVector(intVal = q, size = 128) #print p, q if(not (p != q)): c = True elif(not ( (pbv[0] == 1) and (qbv[0] == 1) ) ): c = True elif(not((int(BitVector(intVal = p-1).gcd(ebv)) == 1) and (int(BitVector(intVal = q-1).gcd(ebv)) == 1))): c = True else: c = False nbv = BitVector(intVal = p*q, size = 256) n = int(nbv) dbv = ebv.multiplicative_inverse( BitVector(intVal = (p-1) * (q-1), size = 256) ) d = int(dbv) return p, q, n, int(dbv), pbv, qbv, nbv, dbv
""" Test class for PrimeGenerator """ from PrimeGenerator import PrimeGenerator pg=PrimeGenerator() pg.run()