-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathecdh.py
50 lines (36 loc) · 1.06 KB
/
ecdh.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
"""
Elliptic Curve Diffie Hellman
"""
from curve import Curve, Point
class ECDH:
def __init__(self, curve, generatorPoint):
self.curve = curve
self.generatorPoint = generatorPoint
self.secret = None
self.ecdhmessage = None
def setSecretMultiplicand(self, multiplicand : int):
self.secret = multiplicand
def receiveECDHMessage(self, ecdhmessage : Point):
self.ecdhmessage = ecdhmessage
def getECDHMessage(self) -> Point:
return self.secret*self.generatorPoint
def getCommonKey(self) -> Point:
return self.secret*self.ecdhmessage
curve = Curve(65, -65, 3077783)
G = Point(1, 1, curve)
alice = ECDH(curve, G)
bob = ECDH(curve, G)
alice.setSecretMultiplicand(13)
bob.setSecretMultiplicand(79)
# Alice -> Bob
bob.receiveECDHMessage(
alice.getECDHMessage()
)
print("Alice -> Bob", alice.getECDHMessage())
# Bob -> Alice
alice.receiveECDHMessage(
bob.getECDHMessage()
)
print("Bob -> Alice", bob.getECDHMessage())
print("Alice: K=",alice.getCommonKey())
print("Bob: K=", bob.getCommonKey())