0

I am trying to JSON with JWE using node-jose in Node.js. Following are the steps i am doing.

I have Java key store through which I have exported Certificate for Encryption and Private for Decryption.

For Encryption

 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));
  });

  });

For Decryption:

  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'));
    });
      });

Expected plaintext : { "Param1":"param1" };

But Result is : [object Object]

As you can see I have tried to convert Buffer data to string but still it is not showing expected result. Can anyone help me out in this where I am wrong.

Thanks

Regards MJ

user1162546
  • 13
  • 1
  • 6

0 Answers0