Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

node.js - JavaScript, eccrypto.encrypt() giving an [object object] after hexing the value

Intro:

Hello Community. I have been trying to encrypt a message with the https://www.npmjs.com/package/eccrypto ~ eccrypto.encrypt() function then .toString('hex') the value after encrypting but when I return the hexed value, it gives me [object object]. Why does this happen? I am new to javascript and eccrypto and any insight will be appreciated.

Relevant Code:

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);
        //encrypt(SKey.publicKey.toHex(), "fuck this is shitty");

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

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

        return publicKey;

    }

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

    //genSKey();
    //genPKey();

    return;
}

Focus: Why is this returning an [object object] and not something I can store successfully?

question from:https://stackoverflow.com/questions/65874138/javascript-eccrypto-encrypt-giving-an-object-object-after-hexing-the-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To decrypt the encrypted data you can use a function from eccrypto called decrypt(). I wrote these functions to help you understand the possibilities. The encrypt and decrypt function are asynchronous, which means we can await them in an async function. You have to create a public and private key and pass them to the function, that way encryption / decryption is possible.

const eccrypto = require("eccrypto");

// generate keys
const privateKeyA = eccrypto.generatePrivate();
const publicKeyA = eccrypto.getPublic(privateKeyA);
const privateKeyB = eccrypto.generatePrivate();
const publicKeyB = eccrypto.getPublic(privateKeyB);

// function for encrypting the message you pass in
async function encryptingData(pub_key, msg) {
    return await eccrypto.encrypt(pub_key, Buffer.from(msg));
}

// function for decrypting the encrypted message
async function decryptingData(priv_key, encryptedmsg) {
    return await eccrypto.decrypt(priv_key, encryptedmsg);
}

// example main function on how you could use seperate functions.
async function communicate() {
    const message_a = await encryptingData(publicKeyA, 'Message a');
    const message_b = await encryptingData(publicKeyB, 'Message b');

    const decrypted_a = await decryptingData(privateKeyA, message_a);
    const decrypted_b = await decryptingData(privateKeyB, message_b);

    console.log(`Message a encrypted : ${JSON.stringify(message_a)}.`);
    console.log(`Message a decrypted: ${decrypted_a}.`);
    console.log('-------------------------------------')
    console.log(`Message b encrypted : ${JSON.stringify(message_b)}.`);
    console.log(`Message b decrypted : ${decrypted_b}.`);
}

communicate();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...