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?
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');
1I'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!– MaloneFeb 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.– pubkeyMar 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-ecieshas performance-issues right now. Thanks again.– MaloneMar 1, 2017 at 22:03
1I love the name you picked! Haha 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.– pubkeyJan 15, 2022 at 17:20
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.