import time from time import perf_counter from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.asymmetric import ec def generate_ecc_keypair(): """Generates an elliptic curve key pair (private, public).""" private_key = ec.generate_private_key( curve=ec.SECP256K1(), backend=default_backend() ) public_key = private_key.public_key() return private_key, public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) def encrypt_message(public_key, message): """Encrypts a message using the provided public key (ECC).""" public_key = serialization.load_pem_public_key( public_key, backend=default_backend() ) # Omit complex symmetric encryption for demonstration ciphertext = message.encode('utf-8') # Placeholder for actual encryption return ciphertext def decrypt_message(private_key, ciphertext): """Decrypts a message using the provided private key (ECC).""" # Get the PEM-encoded representation of the private key private_key_pem = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) private_key = serialization.load_pem_private_key( private_key_pem, password=None, # No password for demonstration backend=default_backend() ) # Omit complex symmetric decryption for demonstration message = ciphertext.decode('utf-8') # Placeholder for actual decryption return message if __name__ == '__main__': private_key, public_key = generate_ecc_keypair() message = "This is a secret message!" # You can change the message content here start_time = perf_counter() ciphertext = encrypt_message(public_key, message) encrypt_time = (perf_counter() - start_time) * 1000 print("Encryption time:", encrypt_time, "ms") start_time = perf_counter() decrypted_message = decrypt_message(private_key, ciphertext) decrypt_time = (perf_counter() - start_time) * 1000 print("Decryption time:", decrypt_time, "ms") print("Decrypted message:", decrypted_message)