07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 ■ Cryptographic Applications and PKI 241

The following example demonstrates how to generate a new ECC key, export

it, and reload it back into your program. The code uses the NIST P-256 algorithm,

which is the most-used elliptic curve, and there are no reasons to believe it’s

insecure:

from Crypto.PublicKey import ECC

key = ECC.generate(curve='P-256')

f = open('myprivatekey.pem','wt')

f.write(key.export_key(format='PEM'))

f.close()

f = open('myprivatekey.pem','rt')

key = ECC.import_key(f.read())

print (key)

The key generated will look similar to the following:

EccKey(curve='NIST P-256',

point_x=8551131792519309159153800555446728338631199151373724862622804721

0045098335773,

point_y=6283402795854508034745449155320613311657026087929116481422492859

9382610602892,

d=2063641786698337143130043788498991558397573514836970341582277689437768

1606808)

Key Lengths and Curves

The ECC algorithms have many strengths, including the variety of elliptic curves

that can be used; each curve offers different levels of security, which extends a

variable of cryptographic strength. Each type of curve also presents a variety of

performance and key lengths. The ECC curves that are provided in our libraries

provide the ability to have named curves such as Curve25519 or Secp256k1.

Curve25519 provides 128 bits of security and is designed for use with the

Elliptic Curve Diffie-Hellman key scheme; it is considered one of the fastest

ECC curves and is publicly available. Secp256k1 is an elliptic curve that is used

in Bitcoin’s public-key cryptography and is defined in the Standards of Efficient

Cryptography (SEC). Another benefit to Secp256k1 is that unlike the popular

NIST curves, Secp256k1’s constants were selected in a predictable way, which

significantly reduces the possibility that the curve’s creator inserted any sort

of backdoor into the curve. ECC keys have length, which directly depends on

the underlying curve. Following is a list of common ECC named curves and

their key lengths:

■■

secp192r1: 192-bit

■■

sect233k1: 233-bit

■■

secp224k1: 224-bit

■■

secp256k1: 256-bit

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!