Ethereum Secure Messaging (CLI)
A developer-focused guide to end-to-end encrypted messaging using Ethereum keys from the command line.
While wallets like Electrum offer a graphical interface for secure messaging, developers often require a command-line workflow. This guide demonstrates how to use the public-key cryptography inherent in Ethereum for truly private, end-to-end encrypted communication using the Foundry toolkit and a helper script.
This method ensures that only the intended recipient, the holder of the corresponding private key, can decrypt and read your message.
Step 1: Prerequisites & Setup
Before you begin, ensure you have Foundry and Node.js installed. Then, set up the helper script in a new directory:
- Download the script: Use `curl` to download the script from its GitHub repository.
curl -o eth-messenger.js https://raw.githubusercontent.com/kerneldump/Tools/main/tools/eth-messenger/eth-messenger.js
- Install the dependency: In the same directory, use `npm` to install the required `eth-crypto` library.
npm install eth-crypto
- Make the script executable:
chmod +x eth-messenger.js
Step 2: Create or Identify Your Key Pair
To receive encrypted messages, you need an Ethereum private key. If you don't have one or want to use a new one for testing, you can generate one with `cast`.
cast wallet new
The command will output a new address and private key. Save the actual private key that is generated, as you will need it to decrypt messages sent to you.
Successfully created new keypair.
Address: 0xb63C8b5842B5294bc60472e310BE8DC81Ac8d98B
Private key: 0xYOUR_NEW_PRIVATE_KEY...
Step 3: Get The Recipient's Public Key
To send an encrypted message, you need the recipient's uncompressed public key. The recipient can generate this from their private key using `cast`.
The recipient should run the following command, substituting their own private key:
# The recipient runs this command
cast wallet pubkey --private-key YOUR_PRIVATE_KEY_HERE
The output will be a 128-character hex string. The recipient sends this public key (prefixed with `0x`) to you. It can be shared openly without compromising their security.
Step 4: Encrypt Your Message
With the recipient's public key, use the downloaded `eth-messenger.js` script to encrypt your message.
# You, the sender, run this command
./eth-messenger.js encrypt 0xRECIPIENT_PUBLIC_KEY 'Your secret message here'
The script will output a JSON object containing the encrypted data. This entire JSON string is the encrypted payload. Copy it and send it to the recipient through any channel.
Step 5: Decrypt the Received Message
The recipient, who holds the private key, can now decrypt the message. Crucially, the JSON payload must be wrapped in single quotes to prevent the shell from misinterpreting it.
# The recipient runs this command
./eth-messenger.js decrypt YOUR_PRIVATE_KEY '{"iv":"...","ciphertext":"...","mac":"...","ephemPublicKey":"..."}'
The script will use the recipient's private key to decrypt the payload and print the original, secret message to the console.
Security Beyond Finance
This powerful feature demonstrates that the cryptographic tools securing Ethereum are not limited to financial transactions. They provide a foundation for individual sovereignty over data and communication.
Understanding and managing these cryptographic keys is fundamental to security. Nanar Consulting provides expert key management solutions and training to ensure your organization can leverage these technologies safely and effectively.