Eccrypto ECIES 解密方案,JavaScript

问题描述

这可能需要 https://www.npmjs.com/package/eccrypto 中的某种背景。我正在尝试解密从使用 ECIES 方案的 api 检索到的消息,但它一直告诉我公钥是“坏的”。

错误

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)

我很确定您在消息对象中传递的解密参数包含其中的公钥。如果我错了,那么它是从私钥参数派生的,但我不相信这是,因为私钥似乎没问题,并且在解密时从私钥派生公钥与 ECIES 解密的操作相反,加密是另一回事。

下面你会看到解密函数和其中用到的函数

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;
}

这是消息对象在发送到 API 供以后检索之前加密的方式:

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;

    }

我不确定出了什么问题,因为我确实提供了带有有效私钥的解密消息功能,据我所知,我首先正确地加密了消息,这就是我最终传递给解密消息的内容功能。我做错了什么?任何帮助和关闭对我来说都非常有用。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)