如何解决JavaScript,eccrypto.encrypt() 在对值进行十六进制处理后给出一个 [object object]
简介:
你好社区。我一直在尝试使用 https://www.npmjs.com/package/eccrypto ~ eccrypto.encrypt() 函数加密消息,然后使用 .toString('hex') 加密后的值,但是当我返回十六进制值时,它给了我 [object object]。为什么会发生这种情况?我是 javascript 和 eccrypto 的新手,任何见解将不胜感激。
相关代码:
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;
}
重点: 为什么这会返回一个 [object object] 而不是我可以成功存储的东西?
解决方法
如果您想获取特定属性,则加密返回的值是一个对象。您可以使用例如:.mac or .iv
来访问它们,如果您在响应中调用 JSON.stringify()
,您可以看到对象内部的内容。
const eccrypto = require("eccrypto");
const privateKey = eccrypto.generatePrivate();
const publickey = eccrypto.getPublic(privateKey);
async function waitFerEncryption() {
const encrypted = await eccrypto.encrypt(publickey,Buffer.from("msg"));
console.log();
console.log(`Stringified: ${JSON.stringify(encrypted)}.`)
console.log(`Cipher text: ${encrypted.ciphertext}.`)
console.log(`Mac: ${encrypted.mac}.`)
console.log(`Iv: ${encrypted.iv}.`)
}
waitFerEncryption();
要解密加密数据,您可以使用 eccrypto 中名为 decrypt()
的函数。我编写了这些函数来帮助您了解可能性。加密和解密函数是 asynchronous
,这意味着我们可以在 await
函数中 async
它们。您必须创建公钥和私钥并将它们传递给函数,这样才能进行加密/解密。
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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。