Node.JS中使用node-jose进行加密和解密

时间:2019-03-15 08:12:53

标签: javascript node.js encryption jwe jose

我正在尝试使用Node.js中的node-jose与JWE进行JSON。以下是我正在执行的步骤。

我有Java密钥存储区,通过它可以导出加密证书和专用解密证书。

用于加密

 var publicKEY  = fs.readFileSync('./josecert','utf8');
 var importkeystore = jose.JWK.createKeyStore();
 
  importkeystore.add(publicKEY, 'pem').
  then(function(result) {
    // {result} is a jose.JWK.Key
    // {result.keystore} is a unique jose.JWK.KeyStore
    console.log(result);

    jose.JWE.createEncrypt({ format: 'flattened' },result).
    update(buffPayload).
    final().
    then(function(result) {
            console.log("Encrypted Result :: " + JSON.stringify(result));
		});

  });

用于解密:

  var privateKEY  = fs.readFileSync('./private','utf8');

  var Encrypted = {"protected":"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJraWQiOiJhYmwifQ","encrypted_key":"b6X63AY_NfwRkr0TSOkQfNFe7Y5MeCtNyUs3krcQtOp1bafL-H74WJyNpeh2BtLw4lf8y2N_VAM9w01Y9XQwXwHgdrP4waWRzppIPyp8YLfMnznma1XgSvXi5XlFWmgWU_TAX9K-2Y-m3Lwpj841seB5fISbs40QLQZT7oYaDnTKkSxheZ7lrjnVygNmRfoW6kxTHiCJWUW1GiO4ODJFCjMTZM-Fd1OwWrjxE56nsiCyY0axLCf2yh1_l_nj-SBCVLNeyr7-e6ysR6ZP0Y35Imo_sIGTudcT7UR5YXBwvKjpZ8ui4gkMswqQTWAMAJFsw-HrzHKoQM-AxxU-Wt7KLw","iv":"chsJbbhbuMk45bR-fk7HWw","ciphertext":"JuppENUeg-xWCz3KVSew1A","tag":"qm7RXojYNnjkc3g9uicQWg"}
  var importkeystore = jose.JWK.createKeyStore();
  importkeystore.add(privateKEY, 'pem').
  then(function(result) {
    console.log(result);
 
  jose.JWE.createDecrypt(result).
        decrypt(input).
        then(function(result) {
          // ....
          console.log(result);
          let json = JSON.stringify(result.plaintext);
          console.log(json);
   
          let bufferOriginal = Buffer.from(JSON.parse(json).data);
          console.log(bufferOriginal.toString('utf8'));
    });
      });

预期的纯文本:{     “ Param1”:“ param1”};

但是结果是:[object Object]

如您所见,我已尝试将Buffer数据转换为字符串,但仍未显示预期结果。谁能帮我解决这个错误的地方。

谢谢

问候 MJ

0 个答案:

没有答案