首页 > 解决方案 > 使用 nodejs 进行 gnupg 加密

问题描述

使用nodejs使用gnupg加密的更好的库是什么

我有一个二进制公钥,

需要加密json有效载荷,

将其作为表单数据(多部分表单)发送到另一个 API。

我尝试查看 openpgp.js,尝试读取密钥并加密,但没有运气。

感谢任何帮助

这篇文章给了我一个二进制公钥来加密和发送一个 json 有效载荷。

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

我需要在 nodejs 中执行此操作..

我已经使用openpgp,form-datastream其作为表单数据发布到 api。

下面是我尝试过的代码。

// 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}`,
        },
      })
    );

标签: node.jsencryptiongnupgopenpgp

解决方案


推荐阅读