25

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?

31

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
    secretMessage
);

const decrypted = await EthCrypto.decryptWithPrivateKey(
    alice.privateKey,
    encrypted
);

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

Run via CodeSandbox

  • 1
    Please go ahead and accept your answer by clicking on the checkmark in the voting buttons. :) – eth Jan 6 '17 at 2:27
  • 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 Feb 28 '17 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 Mar 1 '17 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 Mar 1 '17 at 22:03
  • Hi @Malone, Can you help me with the method of encryption that you used? In the above-shared code, he has generated the private and public key form bitcore-lib, which you correctly mentioned that doesn't work with Ethereum address.So would it be possbile to share the code snippet with me, they way you achieved the encryption. I'm generating the key using kobl.one/blog/create-full-ethereum-keypair-and-address blog. and when I'm using the key pair generated via this blog, it's giving me an error. Kindly post an answer to this if possible – Vixon Aug 22 '17 at 9:02
10

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, privacy policy and cookie policy

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