Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

前后端分离的Rsa加解密如何实现呢?

想知道如何在前后端分离(Vue+springboot)下,想实现RSA加密表单输入的登录密码,传输入后端在解密呢?
网上查询了一下,感觉有点零散,只知道后端生成公钥私钥,前端接受公钥,并用jsencry.js,加密,在返回给后端解密。
不知道各位大佬有没有相应的教程,比较完整一点,前端和后端都有详细说明的,谢谢啦!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

生成公钥私钥就不用说了吧

前端
安装jsencrypt

npm i jsencrypt --save
var encrypt = new JSEncrypt();
var public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyfEuMt351kG8e2ZSN47rp95dY
TX4rGHdGXrx+mFwm5W7VZlrmtY07QJNUJ0myQragBUiYLSNeVSysSLbNOUDBAs1i
Pcb/QG++UzmJhCDjf1+GJANHtfLI7CDaFM1aoa/7C09qWkds7n0QdpT5iLPCOS6C
VYq5DBv5mDgUeAwleQIDAQAB
";
encrypt.setPublicKey(public_key);
let username  = encrypt.encrypt('username');

username变量就是加密好的字符串,和普通的字符串一样发到后端就行了

后端
后端接收到字符串后用相应的java库通过私钥解密就行了,就如同base64 encode和decode的原理一样,之前用php写的,没有java的代码,所以只能帮到这了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...