1

what is the better library to use gnupg encryption using nodejs

I have a binary public key,

Need to encrypt json payload,

Send it as a formdata (multipart-form) to another API.

I tried looking at openpgp.js, tried reading the key and encrypting, but no luck.

Appreciate any help

a binary public key was given to me along this article to encrypt and send a json payload.

https://www.ibm.com/support/knowledgecenter/SS4T5W/Watson_Talent_Data_Management_Administrator_Guide/gnupg_to_import_pgp_keys.html

I need to do this in nodejs..

I've used openpgp, form-data, stream for posting this as a formdata to api.

below is the code I've tried.

// encrypting the json payload. Is there anything I'm doing wrong here? receiving end is always failing to decrypt, always the response is a bad request

const pgpEncrypt = async (payload) => {
  var encryptedKey = await fs.readFileSync("pub.bpg");
  const keys = (await openpgp.key.read(encryptedKey)).keys;
  const data = await openpgp.encrypt({
    message: openpgp.message.fromText(JSON.stringify(payload, null, 0)),
    publicKeys: keys,
    armor: false,
  });
  return data.message.packets.write();
};

const formData = new FormData();
let fileContent = await pgpEncrypt(customPayload);
let stream = bufferToStream(Buffer.from(fileContent));
formData.append("file", stream);

const { Readable } = require("stream");
function bufferToStream(binary) {
  const readableInstanceStream = new Readable({
    read() {
      this.push(binary);
      this.push(null);
    },
  });
  return readableInstanceStream;
}

axios.post(url, formData, {
        headers: {
         ...formData.getHeaders(),
          Authorization: `Bearer ${accessToken}`,
        },
      })
    );
hashbytes
  • 769
  • 1
  • 8
  • 26
  • Library recommendations are off-topic on SO. Post your most recent code and describe your problem. – Topaco Jul 24 '20 at 06:14
  • @Topaco Thanks, I've updated the question with what i have tried. – hashbytes Jul 24 '20 at 14:14
  • Hopefully someone must have done this before. It seems very common use case and I assume no one invents encryption algorithms (just for their use cases). I know that I must be doing something stupid there not to work. – hashbytes Jul 24 '20 at 14:31
  • _...tried reading the key and encrypting, but no luck._ is not a very helpful description. What exactly is the problem (error message, wrong decryption...)? I would try to test encryption / decryption and sending independently. For encryption / decryption there are [here](https://github.com/openpgpjs/openpgpjs) enough examples including key generation. In particular I would check if `fromText` in combination with `armor: false` works (just a guess). – Topaco Jul 24 '20 at 21:46
  • @Topaco https://github.com/openpgpjs/openpgpjs/issues/1127#issuecomment-664083564 encryption algorithm that openpgpjs using is different from the gnupg application. Is there a way to force OpenPGP to use BCPGP algorithm ? – hashbytes Jul 27 '20 at 12:29

0 Answers0