def buildCell(self, cmi1, cmi2, cmj1, cmj2): f = random.randint(1, int((self.N // 2)**0.5)) N2 = self.N * self.N N = self.N temp1 = util.calculate_expo_mod(cmj1, N - 1, N2) temp1 = (temp1 * cmi1) % N2 temp1 = util.calculate_expo_mod(temp1, f, N2) temp2 = util.calculate_expo_mod(cmj2, N - 1, N2) temp2 = (temp2 * cmi2) % N2 temp2 = util.calculate_expo_mod(temp2, f, N2) return temp1, temp2
def encryption(self, m): N2 = self.N * self.N r = random.randint(1, self.N // 4) cm1 = util.calculate_expo_mod(self.g, r, N2) cm2_part1 = util.calculate_expo_mod(self.h, r, N2) cm2_part2 = (m * self.N % N2) * cm2_part1 % N2 cm2 = (cm2_part1 + cm2_part2) % N2 # print("cm1: ") # print(cm1) # print("cm2: ") # print(cm2) return (cm1, cm2)
def generateSQSKeys(self): N2 = self.N * self.N self.__b = random.randint(1, N2 // 4) # self.__b = random.randint(1, 15) self.h2 = util.calculate_expo_mod( self.g, self.__b, N2) # is p and q 1024 bits, will have problem here
def decryptNumberSecond(self, cm1, cm2): # temp = cm1**self.__a N2 = self.N * self.N temp = util.findMI(util.calculate_expo_mod(cm1, self.__a, N2), N2) # t = cm2/temp t = cm2 * temp # print("t: ") # print(t) N2 = self.N * self.N m = ((t - 1) % N2) // self.N # print(t-1 % N2) return m
def comparison(mi, mj): par1 = participant.Participant(g, N, h) cmi1, cmi2 = par1.encryption(mi) par2 = participant.Participant(g, N, h) cmj1, cmj2 = par1.encryption(mj) f = random.randint(1, int((N // 2)**0.5)) temp1 = util.calculate_expo_mod(cmj1, N - 1, N2) temp1 = (temp1 * cmi1) % N2 temp1 = util.calculate_expo_mod(temp1, f, N2) temp2 = util.calculate_expo_mod(cmj2, N - 1, N2) temp2 = (temp2 * cmi2) % N2 temp2 = util.calculate_expo_mod(temp2, f, N2) print("cm1: ") print(temp1) print("cm2: ") print(temp2) cm1prime, cm2prime = sqs.decryptNumberFirst(temp1, temp2) print("cm1prime: ") print(cm1prime) print("cm2prime: ") print(cm2prime) decryptedM = qp.decryptNumberSecond(cm1prime, cm2prime) print("decrypted message: ") print(decryptedM)
def generateKeys(bits): p = number.getPrime(bits) # p and q are still int, can do calculations q = number.getPrime(bits) # p = 5 # q = 7 print(p) print(q) N = p*q N2 = N*N u = 0 while (True): u = random.randint(0, N2) if (number.GCD(u, N2) == 1): break # print("u:") # print(u) g = util.calculate_expo_mod (-u, 2*N, N2) # g = -(u*u)%N2 return g, N
def generateQPKeys(self): N2 = self.N * self.N self.__a = random.randint(1, N2 // 4) # self.__a = random.randint(1, 15) self.h1 = util.calculate_expo_mod(self.g, self.__a, N2)
def decryptNumberFirst(self, cm1, cm2): N2 = self.N * self.N # temp = (cm1**self.__b)%N2 temp = util.findMI(util.calculate_expo_mod(cm1, self.__b, N2), N2) # return (cm1, cm2/temp) return (cm1, cm2 * temp % N2)