Python Tutorials - Herong's Tutorial Examples - v2.21, by Herong Yang
"cryptography.hazmat.primitives.asymmetric.ec" - Generate EC Keys
This section provides a tutorial example on how to use 'cryptography.hazmat.primitives.asymmetric' module to generate EC private-public key pairs in Python scripts.
To generate EC private-public key pairs for production systems, you can use the cryptography.hazmat.primitives.asymmetric.ec module. Here is a Python example script that generates EC key pairs of a given curve name.
# cryptography-hazmat-ec-keys.py
# Copyright (c) 2025, HerongYang.com, All Rights Reserved.
#
import sys
if (len(sys.argv) < 2):
print("Usage: python cryptography-hazmat-ec-keys.py 'list'|curve")
exit()
cmd = sys.argv[1]
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
if cmd=='list':
types = ec._CURVE_TYPES
for key in types.keys():
print(key.upper())
# print(val)
else:
curve = getattr(ec, cmd.upper())
private_key = ec.generate_private_key(curve())
public_key = private_key.public_key()
print("Curve name: "+private_key.curve.__class__.__name__)
print("Key size: "+str(private_key.key_size))
print(f"x: {private_key.private_numbers().public_numbers.x}")
print(f"y: {private_key.private_numbers().public_numbers.y}")
print(f"d: {private_key.private_numbers().private_value}")
print()
print("Private key: \n"
+ str(private_key.private_bytes(
serialization.Encoding.PEM,
serialization.PrivateFormat.TraditionalOpenSSL,
serialization.NoEncryption()
), "utf-8")
)
print("Public key: \n"
+ str(public_key.public_bytes(
serialization.Encoding.PEM,
serialization.PublicFormat.SubjectPublicKeyInfo
), "utf-8")
)
List curves supported by the cryptography package
herong$ python3 cryptography-hazmat-ec-keys.py list PRIME192V1 PRIME256V1 SECP192R1 SECP224R1 SECP256R1 SECP384R1 SECP521R1 SECP256K1 BRAINPOOLP256R1 BRAINPOOLP384R1 BRAINPOOLP512R1
Generate private-public key pairs of a given curve
herong$ python3 cryptography-hazmat-ec-keys.py SECP256R1 Curve name: SECP256R1 Key size: 256 x: 1448598621669265146687495365245166001695582165426594859805879... y: 3746487251185212638022777276660681179127648368273248695396648... d: 2771254253714437992124662222726178157117638306617714749456695... Private key: -----BEGIN EC PRIVATE KEY----- MHcCAQEEID1EvkRMtGA9S5qvBZQZxFWcuPZTYC7Aidp0TJFP3q4QoAoGCCqGSM49 AwEHoUQDQgAEIAbHE7y/bPBxbPpqrMZPSJduPA3nYoysdnK2YDnm8ndS1F3LjovD B7pr9xXvLj42Eqlk8EvVkrzlRcUzH56UMQ== -----END EC PRIVATE KEY----- Public key: -----BEGIN PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEIAbHE7y/bPBxbPpqrMZPSJduPA3n YoysdnK2YDnm8ndS1F3LjovDB7pr9xXvLj42Eqlk8EvVkrzlRcUzH56UMQ== -----END PUBLIC KEY-----
Visit https://cryptography.io/en/38.0.0/hazmat/primitives/asymmetric/ec/ for more details on the cryptography.hazmat.primitives.asymmetric.ec module.
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
List, Set and Dictionary Comprehensions
Packages and Package Directories
"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
►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