ECIES Encryption - Example
Now, let's demonstrate how the ECIES encryption scheme works in practice in Python. We shall use a Python library eciespy:
pip install eciespyA sample Python code to generate public / private key pair and encrypt and decrypt a message using ECIES is:
from ecies.utils import generate_eth_key
from ecies import encrypt, decrypt
import binascii
privKey = generate_eth_key()
privKeyHex = privKey.to_hex()
pubKeyHex = privKey.public_key.to_hex()
print("Encryption public key:", pubKeyHex)
print("Decryption private key:", privKeyHex)
plaintext = b'Some plaintext for encryption'
print("Plaintext:", plaintext)
encrypted = encrypt(pubKeyHex, plaintext)
print("Encrypted:", binascii.hexlify(encrypted))
decrypted = decrypt(privKeyHex, encrypted)
print("Decrypted:", decrypted)Run the above code example: https://repl.it/@nakov/ECIES-in-Python.
The above code is pretty simple: just generate ECC public + private key pair using ecies.utils.generate_eth_key() and call the ecies.encrypt(pubKey, msg) and decrypt(privKey, encryptedMsg) functions from the eciespy library.
The output form the above code looks like this:
The Python eciespy library internally uses ECC cryptography over the secp256k1 curve + AES-256-GCM authenticated encryption. Note that the above encrypted message holds together 4 values: {cipherPubKey, AES-nonce, authTag, AES-ciphertext}, packed in binary form and not directly visible from the above output.
Last updated
Was this helpful?