"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         curve448
https://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

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Communicating with HTTPS Servers

 tinyec - Tiny Library for ECC

Generating EC Public-Private Keys

 "cryptography.hazmat.primitives.asymmetric.ec" - Generate EC Keys

 "serialization.load_pem_private_key()" - Load EC Private Key

"Crypto.PublicKey.ECC" - Generate EC Keys

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB