JavaScript, eccrypto.encrypt() 在十六进制值后给出一个 [object object]
JavaScript, eccrypto.encrypt() giving an [object object] after hexing the value
简介:
你好社区。我一直在尝试使用 https://www.npmjs.com/package/eccrypto ~ eccrypto.encrypt() 函数加密消息然后 .toString('hex') 加密后的值但是当我 return 十六进制值时,它给了我[对象对象]。为什么会这样?我是 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;
}
重点:
为什么这个 returning 是一个 [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
,这意味着我们可以在async
函数中await
它们。您必须创建一个 public 和私钥并将它们传递给函数,这样就可以进行加密/解密。
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();
简介:
你好社区。我一直在尝试使用 https://www.npmjs.com/package/eccrypto ~ eccrypto.encrypt() 函数加密消息然后 .toString('hex') 加密后的值但是当我 return 十六进制值时,它给了我[对象对象]。为什么会这样?我是 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;
}
重点: 为什么这个 returning 是一个 [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
,这意味着我们可以在async
函数中await
它们。您必须创建一个 public 和私钥并将它们传递给函数,这样就可以进行加密/解密。
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();