bitchan / eccrypto

JavaScript Elliptic curve cryptography library
Creative Commons Zero v1.0 Universal
302 stars 98 forks source link

"Error: Bad public key" while it seems everything is OK! #89

Open AlirezaEthDev opened 1 year ago

AlirezaEthDev commented 1 year ago

I'm using eccrypto in my program. Bob encrypts a key in its NodeJS by eccrypto and Carol should decrypt it in its NodeJS by eccrypto too. The issue arises by decryption, encryption is successful but decryption is failed. Whenever the program runs eccrypto.decrypt() it throws this error on PowerShell:

secp256k1 unavailable, reverting to browser version
C:\...\node_modules\eccrypto\browser.js:16
    throw new Error(message || "Assertion failed");
          ^

Error: Bad public key
    at assert (C:\...\node_modules\eccrypto\browser.js:16:11)
    at C:\...\node_modules\eccrypto\browser.js:190:5
    at new Promise (<anonymous>)
    at exports.derive (C:\...\node_modules\eccrypto\browser.js:188:10)
    at Object.exports.decrypt (C:\...\node_modules\eccrypto\browser.js:246:10)
    at Object.<anonymous> (C:\...\Alice.js:30:16)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

This is my whole code:

const eccrypto=require("eccrypto");

const publicKey="0439c1b6515b1e3be3320d5601762d1730d48d01849f006380d4db6b5fb23fc5281de13ffec39787b86fc98ce47aabb26071d53aa6d3e32bcbe4ff4aa517ca1310";
const privateKey="ab2...a";
let crypto="7b2296...5d7d7d";
const cryptoString=hexToString(crypto);
const cryptoObj=JSON.parse(cryptoString);
const privateKeyString=hexToString(privateKey);

      eccrypto.decrypt(privateKeyString,cryptoObj).then(
        console.log
      );

function hexToString(hexString) {
  return new Buffer.from(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
}

What is the problem and how can I solve it?

albertomorini commented 7 months ago

Hello, first of trying to run your code it's throw an error on line 7 (because JSON.parse only take as input a valid JSON, not a string).

Second, are you trying to use it with HTTP/multiple file? Look out at issue #75 . There's a potential solution (to encode in base64 also the value of the encrypted set and to comment a line which trigger this assertion).

Good luck! ;)