EC Cryptography Tutorials - Herong's Tutorial Examples - v1.04, by Herong Yang
"Crypto.PublicKey.ECC" - Generate EC Keys
This section provides a tutorial example on how to use 'Crypto.PublicKey.ECC' module to generate EC private-public key pairs in Python scripts.
The PyCryptodome library can also be used to generate EC private-public key pairs as shown in the following Python example script:
# Crypto-PublicKey-ECC-keys.py
# Copyright (c) 2025, HerongYang.com, All Rights Reserved.
#
import sys
if (len(sys.argv) < 2):
print("Usage: Crypto-PublicKey-ECC-keys.py curve")
exit()
curve = sys.argv[1]
from Crypto.PublicKey import ECC
private_key = ECC.generate(curve=curve.lower())
public_key = private_key.public_key()
print("Curve name: "+private_key.curve)
print("x: "+str(private_key.pointQ.x))
print("y: "+str(private_key.pointQ.y))
print("d: "+str(private_key.d))
print()
print("Private key: \n"
+ private_key.export_key(format='PEM'))
print()
print("Public key: \n"
+ public_key.export_key(format='PEM'))
Generate private-public key pairs of a given curve
herong$ python3 Crypto-PublicKey-ECC-keys.py secp256r1 Curve name: NIST P-256 x: 8029119512895961891904667501288228163039705301446446847345747... y: 8708255998515365490821628412658056784605921994160276282147485... d: 1029898379719206565456313804324047826628339586034115387837643... Private key: -----BEGIN PRIVATE KEY----- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg47IuvKMyksKv5RkP c+EQN+NtlxPo6ngwyADXJNE+PAGhRANCAASxgzS8Lw4jDJPiAhkk9I3Jg9m5va9f ja0XJQDaj6NbMsCG+3clN8s0lxp4Nxd/LfweYEvjagESGz+FT+VhKc6D -----END PRIVATE KEY----- Public key: -----BEGIN PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEsYM0vC8OIwyT4gIZJPSNyYPZub2v X42tFyUA2o+jWzLAhvt3JTfLNJcaeDcXfy38HmBL42oBEhs/hU/lYSnOgw== -----END PUBLIC KEY-----
Curves supported by the Crypto package
Canonical name Aliases -------------- ---------------------------------- NIST P-192 p192, P-192, prime192v1, secp192r1 NIST P-224 p224, P-224, prime224v1, secp224r1 NIST P-256 p256, P-256, prime256v1, secp256r1 NIST P-384 p384, P-384, prime384v1, secp384r1 NIST P-521 p521, P-521, prime521v1, secp521r1 Ed25519 ed25519 Ed448 ed448 Curve25519 curve25519 Curve448 curve448https://pycryptodome.readthedocs.io/en/latest/src/public_key/public_key.html
Visit https://pycryptodome.readthedocs.io/ for more details on the Crypto.PublicKey.ECC module.
Table of Contents
Geometric Introduction to Elliptic Curves
Algebraic Introduction to Elliptic Curves
Abelian Group and Elliptic Curves
Discrete Logarithm Problem (DLP)
Generators and Cyclic Subgroups
tinyec - Python Library for ECC
ECDH (Elliptic Curve Diffie-Hellman) Key Exchange
ECDSA (Elliptic Curve Digital Signature Algorithm)
ECES (Elliptic Curve Encryption Scheme)
"cryptography.hazmat.primitives.asymmetric.ec" - Generate EC Keys
"serialization.load_pem_private_key()" - Load EC Private Key