-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyExchange.py
More file actions
27 lines (17 loc) · 790 Bytes
/
keyExchange.py
File metadata and controls
27 lines (17 loc) · 790 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
from tinyec import registry
import secrets
def compress(pubkey):
return hex(pubkey.x) + hex(pubkey.y % 2)[2:]
curve = registry.get_curve('brainpoolP256r1')
alicePrivateKey = secrets.randbelow(curve.field.n)
alicePublicKey = alicePrivateKey * curve.g
print("Alice public key is: ", compress(alicePublicKey))
bobPrivateKey = secrets.randbelow(curve.field.n)
bobPublicKey = bobPrivateKey * curve.g
print("Bob public key is: ", compress(bobPublicKey))
print("Now exchange of public keys through internet")
aliceSharedKey = alicePrivateKey * bobPublicKey
bobSharedKey = bobPrivateKey * alicePublicKey
print("Alice shared key is: ", compress(aliceSharedKey))
print("Bob shared key is: ", compress(bobSharedKey))
print("Both shared keys are equal: ", aliceSharedKey == bobSharedKey)