-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diffie-Hellman Key Exchange.py
91 lines (81 loc) · 2.84 KB
/
Diffie-Hellman Key Exchange.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Public-Key Cryptography Simple Code Sample
# Python 3.6.7
# https://github.com/S-S0
import random, time
def genRandPrimeNumb(min, max):
randNum = random.SystemRandom().randint(min, max)
while(not(isPrime(randNum))):
randNum = random.SystemRandom().randint(min, max)
return randNum
def isCoprime(int):
#GCD == 1
if int is 1:
return True
else:
return False
def isPrime(int):
if int <= 1:
return False
for i in range(2, int):
if(int % i is 0):
return False
return True
def euclideanAlgorithm(int_1, int_2):
#GCD, Assume that int_1 > int_2
r = int_1 % int_2
while(r is not 0):
int_1 = int_2
int_2 = r
r = int_1 % int_2
return int_2
def getPrimitiveNumbList(primeNum):
# Calculate Primitive Root List
listPrimitiveRoots = []
listCoprime = []
listComparativeGroup = []
for i in range(1, primeNum):
if (isCoprime(euclideanAlgorithm(primeNum, i))):
listCoprime.append(i)
for i in listCoprime:
for j in range(1, primeNum):
k = pow(i, j, primeNum)
if k in listCoprime:
listComparativeGroup.append(k)
else:
break
listTemp = list(set(listCoprime) - set(listComparativeGroup))
if not listTemp:
listPrimitiveRoots.append(i)
listComparativeGroup = []
return listPrimitiveRoots
def choosePrimitiveRoot(RootsList):
PrimitiveRoot = random.choice(RootsList)
return PrimitiveRoot
# Below Test Code
print("======== --- ========")
minInteger = 100
maxInteger = 400
sharedPrimeNumb_q = genRandPrimeNumb(minInteger, maxInteger)
print("Shared Prime Number : ", sharedPrimeNumb_q)
sharedPrimitiveRoot_a = choosePrimitiveRoot(getPrimitiveNumbList(sharedPrimeNumb_q))
print("Shared Primitive Root : ", sharedPrimitiveRoot_a)
print("--- End of sharing ---")
print()
print("--- ALICE ---")
Alice_X = genRandPrimeNumb(minInteger, sharedPrimeNumb_q) # X < sharedPrimeNumb_q, Private_Value
print("Alice's X Value : ", Alice_X)
Alice_Y = pow(sharedPrimitiveRoot_a, Alice_X,sharedPrimeNumb_q) # Public_Value, a^X mod q
print("Alice's Y Value : ", Alice_Y)
print()
print("--- BOB ---")
Bob_X = genRandPrimeNumb(minInteger, sharedPrimeNumb_q) # X < sharedPrimeNumb_q, Private_Value
print("Bob's X Value : ", Bob_X)
Bob_Y = pow(sharedPrimitiveRoot_a, Bob_X, sharedPrimeNumb_q) # Public_Value, a^X mod q
print("Bob's Y Value : ", Bob_Y)
print()
print("--- Calculating Key Value... ---")
Alice_Key = pow(Bob_Y, Alice_X, sharedPrimeNumb_q) # The other person's Y^ Own X mod q
print("Alice Key : ", Alice_Key)
Bob_Key = pow(Alice_Y, Bob_X, sharedPrimeNumb_q) # The other person's Y^ Own X mod q
print("Bob Key : ", Bob_Key)
print("======== --- ========")