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?
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');
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.