-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (36 loc) · 971 Bytes
/
Copy pathmain.py
File metadata and controls
48 lines (36 loc) · 971 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import random
import math
def is_prime(a):
if a == 2:
return True
elif (a < 2) or ((a % 2) == 0):
return False
elif a > 2:
for i in range(2, a):
if not (a % i):
return False
return True
def CheckIfProbablyPrime(x):
return pow(2, x - 1, x) == 1
a = int(input("Write A: "))
p = int(input("Write P: "))
check_a = is_prime(a)
check_p = is_prime(p)
while (check_a == False) or (check_p == False):
a = int(input("Enter a prime number for A: "))
p = int(input("Enter a prime number for P: "))
check_p = is_prime(a)
check_q = is_prime(p)
Xa = int(input("Secret number Xa: "))
Xb = int(input("Secret number Xb: "))
if 1 <= Xa <= p - 1 and 1 <= Xb <= p - 1:
Ya = pow(a, Xa, p)
print("Ya: ", Ya)
Yb = pow(a, Xb, p)
print("Yb: ", Yb)
kA = pow(Yb, Xa, p)
print("kA: ", kA)
kB = pow(Ya, Xb, p)
print("kB: ", kB)
else:
print("(Xa or Xb) <1 or >p-1")