node.js - 使用 nodejs 进行 gnupg 加密
问题描述
使用nodejs使用gnupg加密的更好的库是什么
我有一个二进制公钥,
需要加密json有效载荷,
将其作为表单数据(多部分表单)发送到另一个 API。
我尝试查看 openpgp.js,尝试读取密钥并加密,但没有运气。
感谢任何帮助
这篇文章给了我一个二进制公钥来加密和发送一个 json 有效载荷。
我需要在 nodejs 中执行此操作..
我已经使用openpgp
,form-data
将stream
其作为表单数据发布到 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}`,
},
})
);
解决方案
推荐阅读
- php - 在电子邮件地址中使用连字符
- google-drive-api - 无法从范围为 drive.file 的谷歌 API 访问我创建的文件或我在 googledrive 上添加的文件
- c++ - Boost Property tree - 处理一个空的孩子
- mysql - MySQL 存储过程失败,错误代码 2013(查询期间与 MySQL 服务器的连接丢失)
- prometheus - Prometheus 抓取错误“预期时间戳或新记录,得到“MNAME”
- c++ - while循环用于矩阵中对角线的总和
- r - 在R中按字符和数字将字符串分成一列
- matplotlib - 可视化我的 popt 输出会产生一条直线,我想可视化 popt 输出以确保其正确。我是初学者
- javascript - 在 JavaScript 中计算时区的 UTC 时间码
- jsoup - 为什么 Jsoup 无法在 JIRA 中获取信息