ECDH Key Exchange - Examples
Now let's implement the ECDH algorithm (Elliptic Curve Diffie–Hellman Key Exchange) in Python.
We shall use the tinyec
library for ECC in Python:
Now, let's generate two public-private key pairs, exchange the public keys and calculate the shared secret:
The elliptic curve used for the ECDH calculations is 256-bit named curve brainpoolP256r1
. The private keys are 256-bit (64 hex digits) and are generated randomly. The public keys will be 257 bits (65 hex digits), due to key compression.
The output of the above code looks like this:
Due to randomization, if you run the above code, the keys will be different, but the calculated shared secret for Alice and Bob at the end will always be the same. The generated shared secret is a 257-bit integer (compressed EC point for 256-bit curve, encoded as 65 hex digits).
Last updated
Was this helpful?