在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
有人知道吗?
谢谢
generateKeyPairSync
的文档中提供了一个示例,展示了如何提供publicKeyEncoding
和privateKeyEncoding
选项以及它们应该赋予的值。请参考文档。 - President James K. Polk