forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffie.py
More file actions
Latest commit
32 lines (26 loc) · 952 Bytes
/
Copy pathdiffie.py
File metadata and controls
32 lines (26 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ importannotations
deffind_primitive(n: int) ->int|None:
forrinrange(1, n):
li= []
forxinrange(n-1):
val=pow(r, x, n)
ifvalinli:
break
li.append(val)
else:
returnr
returnNone
if__name__=="__main__":
q=int(input("Enter a prime number q: "))
a=find_primitive(q)
ifaisNone:
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)
a_secret=pow(b_public, a_private, q)
b_secret=pow(a_public, b_private, q)
print("The key value generated by A is: ", a_secret)
print("The key value generated by B is: ", b_secret)