I started this blog for generation of the keys. Which I'm able to follow successfully. My generated Private, public key and the address is mentioned in the testIdentity variable in the code below.

Now, I want to use this code ex for encryption/decryption of the data.

Everything seems fine till here. But, when I tried the Ethereum key-pair for the encryption/decryption of data generated using this, I am getting an issue. Below is the code and the issue I'm facing.

var bitcore = require('bitcore-lib');
var ECIES = require('bitcore-ecies');
var message = "foobar";

var testIdentity= {
            type: 'ethereum',
            display: '0xedc8774555d634ec3cdc755296382d572de2a492',
            privateKey: '1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c',
            publicKey: '644c70f5c80c0e0d950659fc3a5ca3e4079072af5b89bdc3d74bf4c43cda5202d294070d3712dc15a1667f1be8541ea5d587b9c041670ce351d031e5c9df563d',
            foreign: false

     * encrypt the message with the publicKey of identity
     * @param  {{privateKey: ?string, publicKey: string}} identity
     * @param  {string} message
     * @return {string}
    var pkiEncrypt = function(identity, message) {

         * this key is used as false sample, because bitcore would crash when alice has no privateKey
        var privKey = new bitcore.PrivateKey('1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c');
          console.log("private key=>" + privKey);
        var alice = ECIES().privateKey(privKey).publicKey(new bitcore.PublicKey(identity.publicKey));
        console.log("Alice's public key=>" + alice);
        var encrypted = alice.encrypt(message);
        console.log("encrypted message by public key of Alice=>"+ encrypted.toString('hex'));

        return encrypted.toString('hex');

     * decrypt the message with the privateKey of identity
     * @param  {{privateKey: ?string, publicKey: string}}   identity
     * @param  {string}   encrypted
     * @return {string}   message
    var pkiDecrypt = function(identity, encrypted) {
        var privKey = new bitcore.PrivateKey(identity.privateKey);
        console.log("Alice Private key=> " + privKey);
        var alice = ECIES().privateKey(privKey);
        console.log("Allice private key again=>" + alice);
        var decryptMe = new Buffer(encrypted, 'hex');
        console.log("decrypted message=>" + decryptMe);
        var decrypted = alice.decrypt(decryptMe);
        console.log("final decrypted message=>"+decrypted.toString('ascii'));
        return decrypted.toString('ascii');

var enc = pkiEncrypt(testIdentity, message);
var dec = pkiDecrypt(testIdentity, enc);


problem while running it:

$ node pki_Stack_Exchange.js
private key=>1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c
    throw new TypeError('Invalid DER format public key');


TypeError: Invalid DER format public key
    at Function.PublicKey._transformDER (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore
    at PublicKey._classifyArgs (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/
    at new PublicKey (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/publickey.
    at pkiEncrypt (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/routes/pki_Stack_Exchange.js:27:59)
    at Object.<anonymous> (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/routes/pki_Stack_Exchange.js:54:11
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

So, the query is that whether

  1. it's possible to generate the key-pair using this blog and then use the keys for encryption and decryption using the code mentioned here?

NOTE: I see the issue of that keys are incompatible. I haven't used bitcore-lib extensively. Hence I need a clear path to generate and use the keys from sepc2561 curve in ethereum to enc-dec dat. That shall be helpful.

  • 1
    What is the issue you are getting? What is your test code? It should work both Ethereum and Bitcoin use the same elliptic curve secp256k1. Perhaps the library you are using is expecting data in a different format. – Ismael Aug 23 '17 at 6:28
  • @ismael, please check the code and the issue. I've updated it. Thanks – Vixon Aug 23 '17 at 13:34
  • 1
    The error is clear, the public key is not in a format accepted by bitcore.PublicKey (see bitcore.io/api/lib/public-key). Review the steps done to generate the key it should start with 02, 03 or 04. – Ismael Aug 24 '17 at 1:24
  • Exactly. That's the issue I want to address here. and would love to understand why the keys are not compatible when they are generated from the same curve.(not sure about the points). & how can I get the compatible keys, which I can generate offline and use it in the code mentioned above – Vixon Aug 24 '17 at 4:58
  • 2
    From the article '04' is removed from the public key. If you add '04' in front of your public key it works! publicKey: '04644c70f5c..... – Ismael Aug 24 '17 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.