0

我正在尝试使用 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

4

0 回答 0