-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diffie_Hellman.py
executable file
·73 lines (56 loc) · 1.41 KB
/
Diffie_Hellman.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
#!/usr/bin/env python3
#Basic Diffie Hellman
import random, numpy, sys, argparse
if "-h" in sys.argv or "--help" in sys.argv:
print("Usage:")
print("./Diffie_Hellman.py")
print("./Diffie_Hellman.py XOR")
print()
print("XOR option replaces (mod) with ^, and outputs how often it works.")
exit()
if "XOR" in sys.argv:
XOR=True
else:
XOR=False
#thanks, StackOverflow
def primesfromXtoN(low,high):
return [x for x in range(low, high)
if all(x % y != 0 for y in range(low, x))]
LargePrimes = primesfromXtoN(1000,2000)
primes = primesfromXtoN(100,600)
def initialise():
global primes , LargePrimes , p , g
p = random.choice(LargePrimes)
g = random.choice(range(3,p-1))
class client:
def __init__(self):
global p , g
self.secret = random.randrange(3,p-1)
if XOR:
self.Pkey = int((g^self.secret) % p)
else:
self.Pkey = int((g**self.secret) % p)
def recieve(self,b):
self.s = (b**self.secret) % p
def run():
global worked, ran
ran+=1
initialise()
c1 = client()
c2 = client()
c1.recieve(c2.Pkey)
c2.recieve(c1.Pkey)
if c1.s == c2.s:
worked+=1
return True
else:
return False
if __name__ == "__main__":
worked=0
ran = 0
while True:
run()
if XOR:
print(str(worked/ran*100)+"%")
else:
print(worked)