from encryption_algorithm import Primitive_root
import random
import math
# Prime judgment
def is_primer(n: int)-> bool:
return False if 0 in [n % i for i in range(2, int(math.sqrt(n))+1)] else True
# Get integer prime
def get_randint_primer(high: int, low=0)-> int:
R = random.randint(low, high)
while(is_primer(R)==False):
R = random.randint(low, high)
return R
# obtain q、a
def get_QA(size_q: list):
q = get_randint_primer(low=size_q[0],high=size_q[1])
a = Primitive_root.Primitive_root(q)
while(len(a)==0):
q = get_randint_primer(low=size_q[0],high=size_q[1])
a = Primitive_root.Primitive_root(q)
return q, a
# Calculation of one-way trapdoor
# a^Pow mod n Calculation
def calc_Key(a, Pow, n):
return int(pow(a, Pow))%n
if __name__=='__main__':
# q Range
size_q = [5000,10000]
# q、a determine
q, a_lt = get_QA(size_q)
a = a_lt[1]
# The private key is determined
Xa = random.randint(1, q)
Xb = random.randint(1, q)
# The public key is calculated and published
Ya = calc_Key(a,Xa,q)
Yb = calc_Key(a,Xb,q)
# Shared key calculation
Share_a = calc_Key(Yb,Xa,q)
Share_b = calc_Key(Ya,Xb,q)
print(' user a Calculated shared key : {}'.format(Share_a))
print(' user b Calculated shared key : {}'.format(Share_b))