I am writing some code in nodejs/browser. I have successfully created ethereum-addresses with the secp256k1-library. I was also able to sign and verify messages. Now I want to encrypt/decrypt a message with the public and private-key of the generated addresses. Has anyone done this before? Can I use CryptoJS to accomplish my goal?


2 Answers 2


thx @Edmunx Edgar, i tried to use ECIES, but it failed to install because of a subdepencency. I now used the bitcore-lib together with bitcore-ecies. This works like expected.

EDIT: I created a npm-module which does exactly theses things and also has some performance optimisations and tutorials: github:eth-crypto.

Here is my code for anyone with the same question:

// run 'npm install eth-crypto --save'

const EthCrypto = require('eth-crypto');

// create identitiy with key-pairs and address
const alice = EthCrypto.createIdentity();

const secretMessage = 'My name is Satoshi Buterin';
const encrypted = await EthCrypto.encryptWithPublicKey(
    alice.publicKey, // encrypt with alice's publicKey

const decrypted = await EthCrypto.decryptWithPrivateKey(

if(decrypted === secretMessage) console.log('success');

Run via CodeSandbox

  • 1
    I've been reading the requirements for bitcoin key-pairs and Ethereum key-pairs. Their sizes are different, more specifically a public key for the bitcoin protocol is 65 bytes (or 33 bytes compressed) whereas the public key on ethereum is 64 bytes. The above code does not work for an Ethereum key-pair. You can however use the Ethereum privKey to generate a new pubKey from the library above like so, var pubKey = new bitcore.PublicKey.fromPrivateKey(privKey); You can now encrypt and decrypt messages. Any suggestions on how I can use this library without having a workaround? Thanks!
    – Malone
    Commented Feb 28, 2017 at 23:59
  • @Malone no sorry, I'm currently not aware of a solution without the workaround. Also to mention that it seems bitcore-ecies has performance-issues.
    – pubkey
    Commented Mar 1, 2017 at 21:38
  • 1
    @pubkey after looking into this a little deeper I've opted for keeping the above method of encryption. I'm using the following [github.com/flyswatter/eth-sig-util](code) to help with obtaining the Ethereum pubKey safely (pretty useful for obtaining key information by means of signing Tx's). I'm building a prototype and this encryption isn't going to happen very often so I'm not too worried if bitcore-ecies has performance-issues right now. Thanks again.
    – Malone
    Commented Mar 1, 2017 at 22:03
  • 1
    I love the name you picked! Haha Commented Jan 17, 2021 at 3:20
  • 1
    @PASH the eth address is a hash of the public key. Because of how hashing works, information is lost. So it is never possible to use the address for encryption. You have to retrieve the public key that belongs to the address. You can use the eth-crypto module to make sure that the key really belongs to that address, by running the hash function again.
    – pubkey
    Commented Jan 15, 2022 at 17:20

Assuming you have the public key of the person you want to send a message to (if they've already signed a transaction you can recover it from the signature) it should be possible to encrypt and decrypt using ECIES. Apparently there's a JavaScript library for this, I assume you can use it in a browser: https://bitcointalk.org/index.php?topic=627927.0

I have no idea either way whether or not these libraries are good enough to use securely.

Another approach is to generate a separate keypair that's actually designed for encryption, then use the Ethereum key to sign the public key and send it to the other party.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.