Package: eciespy
eciespyElliptic Curve Integrated Encryption Scheme for secp256k1 in Python.Other language versions:RustTypeScriptGolangWASMYou can also check a FastAPI web backend demohere.Installpip install eciespyQuick Start>>>fromecies.utilsimportgenerate_eth_key,generate_key>>>fromeciesimportencrypt,decrypt>>>eth_k=generate_eth_key()>>>sk_hex=eth_k.to_hex()# hex string>>>pk_hex=eth_k.public_key.to_hex()# hex string>>>data=b'this is a test'>>>decrypt(sk_hex,encrypt(pk_hex,data))b'this is a test'>>>secp_k=generate_key()>>>sk_bytes=secp_k.secret# bytes>>>pk_bytes=secp_k.public_key.format(True)# bytes>>>decrypt(sk_bytes,encrypt(pk_bytes,data))b'this is a test'Or just use a builtin commandeciespyin your favoritecommand line.APIecies.encrypt(receiver_pk: Union[str, bytes], msg: bytes) -> bytesParameters:receiver_pk- Receiver's public key (hex str or bytes)msg- Data to encryptReturns:bytesecies.decrypt(receiver_sk: Union[str, bytes], msg: bytes) -> bytesParameters:receiver_sk- Receiver's private key (hex str or bytes)msg- Data to decryptReturns:bytesCommand Line InterfaceShow help$eciespy-husage: eciespy [-h] [-e] [-d] [-g] [-k KEY] [-D [DATA]] [-O [OUT]]Elliptic Curve Integrated Encryption Scheme for secp256k1 in Pythonoptional arguments:-h, --help            show this help message and exit-e, --encrypt         encrypt with public key, exclusive with -d-d, --decrypt         decrypt with private key, exclusive with -e-g, --generate        generate ethereum key pair-k KEY, --key KEY     public or private key file-D [DATA], --data [DATA]file to encrypt or decrypt, if not specified, it willread from stdin-O [OUT], --out [OUT]encrypted or decrypted file, if not specified, it willwrite to stdoutGenerate eth key$eciespy-gPrivate: 0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7dPublic: 0x98afe4f150642cd05cc9d2fa36458ce0a58567daeaf5fde7333ba9b403011140a4e28911fcf83ab1f457a30b4959efc4b9306f514a4c3711a16a80e3b47eb58bAddress: 0x47e801184B3a8ea8E6A4A7A4CFEfEcC76809Da72Encrypt with public key and decrypt with private key$echo'0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d'>sk$echo'0x98afe4f150642cd05cc9d2fa36458ce0a58567daeaf5fde7333ba9b403011140a4e28911fcf83ab1f457a30b4959efc4b9306f514a4c3711a16a80e3b47eb58b'>pk$echo'hello ecies'|eciespy-e-kpk|eciespy-d-kskhello ecies$echo'data to encrypt'>data$eciespy-e-kpk-Ddata-Oenc_data$eciespy-d-ksk-Denc_datadata to encrypt$rmskpkdataenc_dataConfigurationEphemeral key format in the payload and shared key in the key derivation can be configured as compressed or uncompressed format.SymmetricAlgorithm=Literal["aes-256-gcm","xchacha20"]NonceLength=Literal[12,16]# only for aes-256-gcm, xchacha20 will always be 24COMPRESSED_PUBLIC_KEY_SIZE=33UNCOMPRESSED_PUBLIC_KEY_SIZE=65@dataclass()classConfig:is_ephemeral_key_compressed:bool=Falseis_hkdf_key_compressed:bool=Falsesymmetric_algorithm:SymmetricAlgorithm="aes-256-gcm"symmetric_nonce_length:NonceLength=16@propertydefephemeral_key_size(self):return(COMPRESSED_PUBLIC_KEY_SIZEifself.is_ephemeral_key_compressedelseUNCOMPRESSED_PUBLIC_KEY_SIZE)ECIES_CONFIG=Config()If you setis_ephemeral_key_compressed = true, the payload would be like:33 Bytes + AESinstead of65 Bytes + AES.If you setis_hkdf_key_compressed = true, the hkdf key would be derived fromephemeral public key (compressed) + shared public key (compressed)instead ofephemeral public key (uncompressed) + shared public key (uncompressed).If you setsymmetric_algorithm = "xchacha20", plaintext data would be encrypted with XChaCha20-Poly1305.If you setsymmetric_nonce_length = 12, then the nonce of AES-256-GCM would be 12 bytes. XChaCha20-Poly1305's nonce is always 24 bytes.For compatibility, make sure different applications share the same configuration.Technical detailsThey are moved toDETAILS.md.ChangelogSeeCHANGELOG.md.