2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2021-03-22 06:59:51 +00:00
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def find_primitive(n: int) -> int | None:
|
2019-10-30 11:52:20 +00:00
|
|
|
for r in range(1, n):
|
|
|
|
li = []
|
2019-11-14 18:59:43 +00:00
|
|
|
for x in range(n - 1):
|
|
|
|
val = pow(r, x, n)
|
2019-10-30 11:52:20 +00:00
|
|
|
if val in li:
|
|
|
|
break
|
|
|
|
li.append(val)
|
|
|
|
else:
|
|
|
|
return r
|
2021-03-22 06:59:51 +00:00
|
|
|
return None
|
2019-10-30 11:52:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-11-14 18:59:43 +00:00
|
|
|
q = int(input("Enter a prime number q: "))
|
2019-10-30 11:52:20 +00:00
|
|
|
a = find_primitive(q)
|
2021-03-22 06:59:51 +00:00
|
|
|
if a is None:
|
|
|
|
print(f"Cannot find the primitive for the value: {a!r}")
|
|
|
|
else:
|
|
|
|
a_private = int(input("Enter private key of A: "))
|
|
|
|
a_public = pow(a, a_private, q)
|
|
|
|
b_private = int(input("Enter private key of B: "))
|
|
|
|
b_public = pow(a, b_private, q)
|
2019-10-30 11:52:20 +00:00
|
|
|
|
2021-03-22 06:59:51 +00:00
|
|
|
a_secret = pow(b_public, a_private, q)
|
|
|
|
b_secret = pow(a_public, b_private, q)
|
2019-10-30 11:52:20 +00:00
|
|
|
|
2021-03-22 06:59:51 +00:00
|
|
|
print("The key value generated by A is: ", a_secret)
|
|
|
|
print("The key value generated by B is: ", b_secret)
|