如何基于Node.js详细实现RSA加密与解密的全过程步骤?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1014个文字,预计阅读时间需要5分钟。
由于项目未登录密码字段没有加密引发安全疑问,以下是如何基于RSA加密进行前后端通信(Java项目)的简要说明。余暇时,可查看以下node.js下的实现。
一、准备前端:使用jsencrypt.js进行加密后端:使用Java实现RSA加密和解密
二、前端
1.引入jsencrypt.js库
2.使用jsencrypt.js生成公钥和私钥
3.使用公钥加密需要传输的数据
4.将加密后的数据发送到后端
三、后端(Java)
1.接收前端发送的加密数据
2.使用私钥解密数据
3.进行业务处理
4.使用公钥加密返回数据
5.将加密后的数据发送回前端
四、node.js实现
javascriptconst crypto=require('crypto');// 生成RSA密钥对const { publicKey, privateKey }=crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem', }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', },});
// 加密函数function encrypt(text, publicKey) { const encrypted=crypto.publicEncrypt(publicKey, Buffer.from(text)); return encrypted.toString('base64');}
// 解密函数function decrypt(encryptedText, privateKey) { const decrypted=crypto.privateDecrypt( privateKey, Buffer.from(encryptedText, 'base64') ); return decrypted.toString();}
// 测试const text='Hello, world!';const encryptedText=encrypt(text, publicKey);console.log('Encrypted:', encryptedText);
const decryptedText=decrypt(encryptedText, privateKey);console.log('Decrypted:', decryptedText);注意:在实际项目中,需要妥善保管私钥,避免泄露。
因项目登录密码字段没有加密引起安全问题,琢磨了下如何基于RSA加密,进行前后端通信(Java项目)。空余时间,看了下在node下的实现。
一、准备
前端是利用jsencrypt.js去加密,后端利用node-rsa去生成公私钥并解密。
二、实现
我是使用koa2初始化的项目。首先,需要前端页面显示和处理加密数据,所以直接在views中新建了index.html,用html为了不在学习模板上花时间。
修改index中的路由,
router.get('/', async (ctx, next) => { await ctx.render('index.html'); });
在html中引入jsencrypt.js,界面内容仅为一个输入框和发送命令的按钮:
<body> <input type="text" id="content"/> <button id="start">gogogog</button> </body> <script src="/javascripts/jsencrypt.js"></script> <script> document.getElementById('start').onclick = function() { // 获取公钥 fetch('/publicKey').then(function(res){ return res.text(); }).then(function(publicKey) { // 设置公钥并加密 var encrypt = new JSEncrypt(); encrypt.setPublicKey(publicKey); var encrypted = encrypt.encrypt(document.getElementById('content').value); // 发送私钥去解密 fetch('/decryption', { method: 'POST', body: JSON.stringify({value:encrypted}) }).then(function(data) { return data.text(); }).then(function(value) { console.log(value); }); }); }; </script>
点击按钮,将输入框中的值先加密,再发送给服务器解密并打印该值。
前端用到了,publicKey和decryption接口,来看下服务端的实现。
首先引入node-rsa包,并创建实例,再输出公私钥,其中,setOptions必须得加上,否者会有报错问题。
const NodeRSA = require('node-rsa'); const key = new NodeRSA({b: 1024}); // 查看 github.com/rzcoder/node-rsa/issues/91 key.setOptions({encryptionScheme: 'pkcs1'}); // 必须加上,加密方式问题。
publicKey(GET),用于获取公钥,只需要调用下内置的方法就行了,
router.get('/publicKey', async (ctx, next) => { var publicDer = key.exportKey('public'); var privateDer = key.exportKey('private'); console.log('公钥:', publicDer); console.log('私钥:', privateDer); ctx.body = publicDer; });
公钥传出给前端加密用,后端使用私钥解密,
router.post('/decryption', async (ctx, next) => { var keyValue = JSON.parse(ctx.request.body).value; const decrypted = key.decrypt(keyValue, 'utf8'); console.log('decrypted: ', decrypted); ctx.body = decrypted; });
解密时调用decrypt进行解密,前端控制台就能输出对应的值了。
三、demo详细代码
说这么多,直接查看代码最直观啦,详细代码查看:demo。
npm i & npm run start
访问3000端口就可以了。
四、实际项目
在使用npm安装方式(vue或react)的项目中,可以这么使用:
npm i jsencrypt // 实际使用 import { JSEncrypt } from 'jsencrypt';
项目地址可以查看:github.com/2fps/blooog。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计1014个文字,预计阅读时间需要5分钟。
由于项目未登录密码字段没有加密引发安全疑问,以下是如何基于RSA加密进行前后端通信(Java项目)的简要说明。余暇时,可查看以下node.js下的实现。
一、准备前端:使用jsencrypt.js进行加密后端:使用Java实现RSA加密和解密
二、前端
1.引入jsencrypt.js库
2.使用jsencrypt.js生成公钥和私钥
3.使用公钥加密需要传输的数据
4.将加密后的数据发送到后端
三、后端(Java)
1.接收前端发送的加密数据
2.使用私钥解密数据
3.进行业务处理
4.使用公钥加密返回数据
5.将加密后的数据发送回前端
四、node.js实现
javascriptconst crypto=require('crypto');// 生成RSA密钥对const { publicKey, privateKey }=crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem', }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', },});
// 加密函数function encrypt(text, publicKey) { const encrypted=crypto.publicEncrypt(publicKey, Buffer.from(text)); return encrypted.toString('base64');}
// 解密函数function decrypt(encryptedText, privateKey) { const decrypted=crypto.privateDecrypt( privateKey, Buffer.from(encryptedText, 'base64') ); return decrypted.toString();}
// 测试const text='Hello, world!';const encryptedText=encrypt(text, publicKey);console.log('Encrypted:', encryptedText);
const decryptedText=decrypt(encryptedText, privateKey);console.log('Decrypted:', decryptedText);注意:在实际项目中,需要妥善保管私钥,避免泄露。
因项目登录密码字段没有加密引起安全问题,琢磨了下如何基于RSA加密,进行前后端通信(Java项目)。空余时间,看了下在node下的实现。
一、准备
前端是利用jsencrypt.js去加密,后端利用node-rsa去生成公私钥并解密。
二、实现
我是使用koa2初始化的项目。首先,需要前端页面显示和处理加密数据,所以直接在views中新建了index.html,用html为了不在学习模板上花时间。
修改index中的路由,
router.get('/', async (ctx, next) => { await ctx.render('index.html'); });
在html中引入jsencrypt.js,界面内容仅为一个输入框和发送命令的按钮:
<body> <input type="text" id="content"/> <button id="start">gogogog</button> </body> <script src="/javascripts/jsencrypt.js"></script> <script> document.getElementById('start').onclick = function() { // 获取公钥 fetch('/publicKey').then(function(res){ return res.text(); }).then(function(publicKey) { // 设置公钥并加密 var encrypt = new JSEncrypt(); encrypt.setPublicKey(publicKey); var encrypted = encrypt.encrypt(document.getElementById('content').value); // 发送私钥去解密 fetch('/decryption', { method: 'POST', body: JSON.stringify({value:encrypted}) }).then(function(data) { return data.text(); }).then(function(value) { console.log(value); }); }); }; </script>
点击按钮,将输入框中的值先加密,再发送给服务器解密并打印该值。
前端用到了,publicKey和decryption接口,来看下服务端的实现。
首先引入node-rsa包,并创建实例,再输出公私钥,其中,setOptions必须得加上,否者会有报错问题。
const NodeRSA = require('node-rsa'); const key = new NodeRSA({b: 1024}); // 查看 github.com/rzcoder/node-rsa/issues/91 key.setOptions({encryptionScheme: 'pkcs1'}); // 必须加上,加密方式问题。
publicKey(GET),用于获取公钥,只需要调用下内置的方法就行了,
router.get('/publicKey', async (ctx, next) => { var publicDer = key.exportKey('public'); var privateDer = key.exportKey('private'); console.log('公钥:', publicDer); console.log('私钥:', privateDer); ctx.body = publicDer; });
公钥传出给前端加密用,后端使用私钥解密,
router.post('/decryption', async (ctx, next) => { var keyValue = JSON.parse(ctx.request.body).value; const decrypted = key.decrypt(keyValue, 'utf8'); console.log('decrypted: ', decrypted); ctx.body = decrypted; });
解密时调用decrypt进行解密,前端控制台就能输出对应的值了。
三、demo详细代码
说这么多,直接查看代码最直观啦,详细代码查看:demo。
npm i & npm run start
访问3000端口就可以了。
四、实际项目
在使用npm安装方式(vue或react)的项目中,可以这么使用:
npm i jsencrypt // 实际使用 import { JSEncrypt } from 'jsencrypt';
项目地址可以查看:github.com/2fps/blooog。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

