【发布时间】:2021-06-21 20:47:03
【问题描述】:
在 node.js 加密模块中,我生成了一个公钥/私钥,但是如何将其保存到文件中并将其从文件加载回内存中?到目前为止我有这个
https://nodejs.org/api/crypto.html
const crypto = require("crypto")
const fs = require('fs');
// The `generateKeyPairSync` method accepts two arguments:
// 1. The type ok keys we want, which in this case is "rsa"
// 2. An object with the properties of the key
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
fs.writeFileSync("public.pem", publicKey);
fs.writeFileSync("private.pem", privateKey);
但我得到了这个错误
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of PublicKeyObject
有人知道吗?
谢谢
【问题讨论】:
-
in the documentation for
generateKeyPairSync的示例向您展示了如何提供publicKeyEncoding和privateKeyEncoding选项以及为它们提供什么值。
标签: node.js file cryptography