0

This will probably need some sort of background in https://www.npmjs.com/package/eccrypto . I am trying to decrypt a message that was retrieved from an api with the ECIES scheme but it keeps telling me that the public key is "bad".

error:

Uncaught (in promise) Error: Bad public key
    at assert (genKey.js:33656)
    at genKey.js:33830
    at new Promise (<anonymous>)
    at exports.derive (genKey.js:33828)
    at Object.exports.decrypt (genKey.js:33886)
    at window.decryptMes (genKey.js:26722)
    at window.search (Login.js:86)

I am pretty sure the decrypt parameter where you pass in the message object contains the public key in it. And if I am wrong then it gets derived from the private key parameter but I don't believe this is, since the private key seems to be fine, and deriving the public key from the private when decrypting is counter intuitive to what ECIES decrypting does, encrypting is another deal.

Below you will see the decrypting function and functions used in it:

window.decryptMes = async function(data)
{
    var skey = getSKey();

    if (skey === null || undefined) 
    {
      console.log('You do not have a key pair');
      return;
    }
    console.log("skey is not null");
    console.log(`data returned ${data}`); //data is returning undefined!
    var decryptedMes = await eccrypto.decrypt(skey, data);
    var deMes = decryptedMes.toString();
    console.log(deMes);
    return deMes;
}
window.getSKey = function()
{
    console.log("getSKey flag: 0");

    var skey = localStorage.getItem("skey");
    const SKey = Buffer.from(skey, 'hex');

    //var skey = base642Array(SKey);

    console.log("getSKey flag: 1");

    console.log("getSKey flag: 2");

    //console.log(skey);

    return SKey;
}

This is how the message object gets encrypted before it gets sent to the API for later retrieval:

window.encryptMes = async function(data)
{
    //for this you need to get the sender's public key to encrypt the message
    console.log("encryptmes: began");
    var pkey = genPKey();

    if (pkey === null || undefined) 
    {
      
      console.log('You do not have a key pair');

    }

    var encryptedMes = await eccrypto.encrypt(pkey, Buffer.from(data));

    var enMes = encryptedMes.toString('hex');

//question now becomes, WHY IS THIS RETURNING OBJECT OBJECT

    console.log(`encryptedMes returned: ${encryptedMes}`); //could be this since it is not stringified when it goes into celox network
    console.log(`enMes returned: ${enMes}`);
    console.log(`enMes completed successfully`);

    return enMes;
}
window.genPKey = function()
{
    console.log("getSKey flag: 0");

    const skey = localStorage.getItem('skey');

    const SKey = Buffer.from(skey, 'hex');

    console.log("getSKey flag: 1");

    if(SKey != null || undefined)
    {
        console.log(SKey);

        console.log("getSKey flag: 2");

        const publicKey = eccrypto.getPublic(SKey);

        const pkey = publicKey.toString('hex');

        localStorage.setItem('pkey', pkey);
        //encrypt(SKey.publicKey.toHex(), "fuck this is shitty");

        console.log("getSKey flag: 3");

        //localStorage.setItem("pkey", window.btoa(JSON.stringify(publicKey)));

        return publicKey;

    }

I am not sure what is wrong since I did provide the decrypt message function with a valid private key, and as far as I know, I did encrypt the message correctly in the first place which is what I eventually passed into decrypt message function. What am I not doing right? Any help and closure will be of great usage for me. Thank you.

  • Where does your implementation of `Buffer` come from and how do you generate the keys and store them? – Artjom B. Feb 11 '21 at 06:06
  • Ok so all I need to do is call generatePrivate() from ecrypto but this is done when the user "Signs up" then I "to string" hex it for then to be stored into local storage. In the getSKey() function above you can see that I retrieve the array as a hex buffer since it does contain binary data and I can't simply store that as a string. I return it to be set as the privatekey for decrypting. Again I think that the problem isn't the private key itself, it could be how I am storing the encrypted message though and I am not sure if I am doing that part adequately. –  Feb 11 '21 at 08:06
  • Model Set -> generatePrivate -> LocalStorage(private) -> PublicEncrypt(data like the username) -> sent to api blockchain Model Get fetch blockchain -> get localstorage Private -> narrow down user object by decrypting username -> fails after this point –  Feb 11 '21 at 08:12

0 Answers0