当前位置: 首页 > news >正文

Node 如何生成 RSA 公钥私钥对

一、引入crypto模块

crypto 为node 自带模块,无需安装

const crypto = require('crypto');

二、封装生成方法

async function generateRSAKeyPair() {return new Promise((resolve, reject) => {crypto.generateKeyPair('rsa', {modulusLength: 2048, // 密钥长度为 2048 位publicKeyEncoding: {type: 'spki',format: 'pem'},privateKeyEncoding: {type: 'pkcs8',format: 'pem'}}, (err, publicKey, privateKey) => {if (err) {reject(err);} else {resolve({ publicKey, privateKey });}});});
}

modulusLength可根据情况自行修改,modulusLength越长越安全但花费的时间也越多

三、使用生成方法

generateRSAKeyPair().then(({ publicKey, privateKey }) => {console.log('Public Key:', publicKey);console.log('Private Key:', privateKey);// 将公钥发送给客户端
}).catch(error => {console.error('Error generating RSA key pair:', error);
});

在已启用的node服务器中调用,或通过node直接运行当前文件node xxx.js,进行调用

四、效果

调用后查看控制台便能看到生成的公钥私钥pem了
在这里插入图片描述


http://www.mrgr.cn/news/82445.html

相关文章:

  • 群晖Alist套件无法挂载到群晖webdav,报错【连接被服务器拒绝】
  • 常见好用的PHP CMS开源系统有哪些?
  • wireshark上没有显示出来rtp协议如何处理
  • 计算机网络 (44)电子邮件
  • 自动化办公|xlwings简介
  • Leetcode - 周赛431
  • Formality:官方Tutorial(一)
  • 『 Linux 』高级IO (二) - 多路转接
  • SQL字符串截取函数——Left()、Right()、Substring()用法详解
  • 计算机网络 (21)网络层的几个重要概念
  • AI数据标注师理论部分考试题库 - 500题
  • Spring AOP 扫盲
  • React Router 用法概览
  • C# 附加到进程中,发现断点不是实的断点
  • 中国联通首次推出一套量化大模型的新标准
  • 【YOLOv8老鼠检测】
  • USB 驱动开发 --- Gadget 驱动框架梳理
  • 动态规划<八> 完全背包问题及其余背包问题
  • 国内Ubuntu环境Docker部署CosyVoice
  • 国内Ubuntu环境Docker部署Stable Diffusion入坑记录
  • 多模态论文笔记——Coca
  • 多模态论文笔记——CogVLM和CogVLM2(副)
  • redis的集群模式与ELK基础
  • 如何从文档创建 RAG 评估数据集
  • .Net Core配置系统
  • U8G2库使用案例(stm32)