提问者:小点点

将对象保存到JSON文件,而不是在读取后在函数中使用它


我对Node.js完全陌生,但必须在我需要这个库的学生项目中使用它:https://github.com/bitchan/eccrypto。 目标是使用ethereum公钥加密文件,将其保存为JSON,然后将其读回用私钥解密:

var fs = require('fs');
var crypto = require("crypto");
var eccrypto = require("eccrypto");

var content = fs.readFileSync('pdf_test.pdf');

var importPrivateKey = "c337ded6f56c07205fb7b391654d7d463c9e0c726869523ae6024c9bec878878"
var importPublicKey = "04730a151f545f5dcdb1c6d99fb1251f5c70f216f39ba2681bcf10db16bd582e6720bc881d51f25ffbe961df6a0af24a9d39a4db3d86a7f6b3f9bf4eaac0e4006b"

var privateKey = new Buffer(importPrivateKey, "hex");
var publicKey = new Buffer(importPublicKey, "hex");

// Encrypting the file for B.
eccrypto.encrypt(publicKey, Buffer(content)).then(function(encrypted) {
    //console.log('Encrypted message ' + JSON.stringify(encrypted));

let data = JSON.stringify(encrypted);
fs.writeFileSync('encrypted.json', data);
console.log('encryption done');

let rawData = fs.readFileSync('encrypted.json')
let encryptedContent = JSON.parse(rawData);
//console.log(encryptedContent);

 // B decrypting the file.
eccrypto.decrypt(privateKey, encryptedContent).then(function(plaintext) {
    //console.log("Decrypted message: ", plaintext.toString());

    fs.writeFile('decrypted.pdf', plaintext, function (err) {
      if (err) return console.log(err);
      console.log('decryption done');
});
 });
});

我从这段代码中得到以下错误:“(节点:271)UnhandledPromiserEjectionWarning:error:Bad Input.” 当我将eccrypto.decrypt函数上的“encryptedContent”变量替换为“encrypted”时,一切都正常工作,但我想让用户存储加密对象,然后用这个函数解密它。 我怎么能那么做?


共1个答案

匿名用户

问题是encrypted对象不完全是JSON可序列化的,因此您必须在某种JSON可序列化对象中对缓冲区进行编码。 由于您对私钥和公钥使用了十六进制,我在下面也使用了它。 (此外,buffer()构造函数已弃用,不安全,因此我将其切换到buffer.from()

var fs = require('fs');
var crypto = require("crypto");
var eccrypto = require("eccrypto");

var content = fs.readFileSync('pdf_test.pdf');;

var importPrivateKey = "c337ded6f56c07205fb7b391654d7d463c9e0c726869523ae6024c9bec878878"
var importPublicKey = "04730a151f545f5dcdb1c6d99fb1251f5c70f216f39ba2681bcf10db16bd582e6720bc881d51f25ffbe961df6a0af24a9d39a4db3d86a7f6b3f9bf4eaac0e4006b"

let privateKey = Buffer.from(importPrivateKey, 'hex');
let publicKey = Buffer.from(importPublicKey, 'hex');


// Encrypting the file for B.
eccrypto.encrypt(publicKey, Buffer.from(content)).then(function (encrypted) {
    //console.log('Encrypted message ' + JSON.stringify(encrypted));

    let data = JSON.stringify({
        iv: encrypted.iv.toString('hex'),
        ciphertext: encrypted.ciphertext.toString('hex'),
        mac: encrypted.mac.toString('hex'),
        ephemPublicKey: encrypted.ephemPublicKey.toString('hex')
    });
    fs.writeFileSync('encrypted.json', data);
    console.log('encryption done');

    let rawData = fs.readFileSync('encrypted.json')
    let encryptedContent = JSON.parse(rawData);
    encryptedContent = {
        iv: Buffer.from(encryptedContent.iv, 'hex'),
        ciphertext: Buffer.from(encryptedContent.ciphertext, 'hex'),
        mac: Buffer.from(encryptedContent.mac, 'hex'),
        ephemPublicKey: Buffer.from(encryptedContent.ephemPublicKey, 'hex')
    }
    //console.log(encryptedContent);

    // B decrypting the file.
    eccrypto.decrypt(privateKey, encryptedContent).then(function (plaintext) {
        //console.log("Decrypted message: ", plaintext.toString());

        fs.writeFile('decrypted.pdf', plaintext, function (err) {
            if (err) return console.log(err);
            console.log('decryption done');
        });
    });
});