Photo by Scott Webb on Unsplash

Elliptic Curve Keys, Python and Hazmat

--

Well, RSA has been around for over 40 years, and it is struggling in places. In terms of key sizes, we are now at 2,048-bit keys and above. For a lightweight device, we will struggle to perform these sizes of operations. And so Elliptic Curve Cryptography (ECC) has come to our rescue, and where we use typical key sizes of just 256 bits. In fact, Bitcoin and Ethereum, and most blockchain methods use ECC for their keys. So, let’s dive in and implement some ECC key generation, and use the hazmat primitives with the cryptography library.

The generation of the key pair is fairly simple, and where we just define a curve type:

private_key = ec.generate_private_key(ec.SECP256K1())

This value will contain the private key (n) and which (in this case) is a 256-bit random number. This key must be kept secret. If we want to view the value we simply use:

vals = private_key.private_numbers()
no_bits=vals.private_value.bit_length()
print (f"Private key value: {vals.private_value}. Number of bits {no_bits}")

An example ouput confirms that the private key has around 256 bits:

Private key value: 51959644559546985352323146960120412494517429222374217317044884172801854956286. Number of bits 255

--

--

Prof Bill Buchanan OBE FRSE
ASecuritySite: When Bob Met Alice

Professor of Cryptography. Serial innovator. Believer in fairness, justice & freedom. Based in Edinburgh. Old World Breaker. New World Creator. Building trust.