def descobrirpg(n): p = nextprime( n ) #p já é primo, através do teorema de lucas é possível verificar isso #print(p) g = primitive_root(p) #raiz primitiva mais pequena de p #print(g) return p, g
def generate_PG(self): P = randprime(10**26, 10**27) while not (((P - 1) % 2 == 0) and isprime((P - 1) // 2)): P = randprime(10**26, 10**27) G = primitive_root(P) self.P = P self.G = G return P, G
def _generate_root(self) -> int: """ Helper function to calculate the smallest primitive root modulo n where n is the prime specified on the instance """ print(f"Calculating smallest primitive root modulo n where n = {self._prime}") return primitive_root(self._prime)
from random import randint from sympy import isprime from sympy.ntheory.residue_ntheory import primitive_root from sympy.ntheory.generate import randprime from simplecrypt import encrypt, decrypt message = input("Enter message: ") P = randprime(10**58, 10**59) while not (((P - 1) % 2 == 0) and isprime((P - 1) // 2)): P = randprime(10**58, 10**59) G = primitive_root(P) print ("P : ", P, "\nG : ", G) a_private_number = randint(10**58, 10**59) print("Private key a: ", a_private_number) b_private_number = randint(10**58, 10**59) print("Private key b: ", b_private_number) a_open_number = pow(G, a_private_number, P) print("Open key A: ", a_open_number) b_open_number = pow(G, b_private_number, P) print("Open key B: ", b_open_number) a_shared_key = pow(a_open_number, b_private_number, P) print("Secret key A: ", a_shared_key) b_shared_key = pow(b_open_number, a_private_number, P)
## return g def genSecret(prime): start = randint((prime - 1) / 2, prime - 1) while coprime2(start, (p - 1)) == False: start += 1 return start menu = input("Would you like to generate a new key (G) or accept a key (A)? ") if menu == "G": seed = input("Please input a large integer as a seed. ") p = randprime(int(seed), 2 * int(seed)) g = primitive_root(p) print("Tell your partner that p = " + str(p) + " and g = " + str(g)) a = genSecret(p) print("Tell your partner that your unlocked key is " + str(pow(g, a, p))) B = input("What is your partners unlocked key? ") pw = pow(int(B), a, p) print("Password is " + str(pw)) elif menu == "A": p = input("What is the value of p? ") g = input("What is the value of g? ") p = int(p) g = int(g) g = primRoots(p) b = genSecret(p) print("Tell your partner that your unlocked key is " + str(pow(g, b, p)))
def get_primitive_root(modulo): # g^phi(m) = 1 mod m return primitive_root(modulo)