使用RSA实现前端公钥加密后端私钥解密

article/2025/9/25 0:27:57

听说你还不会RSA算法实现前端加密后端解密???那就来看这里…

RSA前端加密后端解密避免出现明文密码传递 话不多说,直接开撸

前端JS
  • 项目中先添加rsa.js文件
    链接:https://pan.baidu.com/s/1r9930MkS4n8TR9IsOIXV-Q
    提取码:hd52
  • 添加完成之后在项目中引入rsa.js
<script src="js/rsa.js"></script> <!--根据自己的文件路径添加即可-->
  • 使用Vue,element ui实现
var encrypt = new JSEncrypt();
$.ajax({type: 'post',url: 'log/publicKey',data: '',dataType: 'json',success: function (data) {encrypt.setPublicKey(data.public_key)_this.password = encrypt.encrypt(_this.password)$.ajax({type: 'post',url: 'log/login',data: {username: _this.username,password: _this.password},dataType: 'json',success: function (data) {if (data.code == 200) {<!--成功之后的操作...-->} else {<!--失败操作-->}}})}})
后端代码
  • Controller层
/*** 获取公钥*/
@ResponseBody@PostMapping("publicKey")public Map<String, Object> public_key() {Map<String, Object> map = new HashMap<>();map.put("public_key", KeyManager.getPublic_key());return map;}
/*** 登录** @param username 用户名* @param password 加密后的密码* @param session* @return* @throws Exception*/@PostMapping("login")@ResponseBodypublic R login(String username, String password, HttpSession session, HttpServletRequest request) throws Exception {String pwd = MD5_u.md5s_32(adminUserService.encryptionResult(password));// 判断用户密码错误次数是否过多if (FrequencyUtil.checkFrequency(username, 5)) {logger.info("密码错误次数过多,当前管理员为:{}", username);return R.ok().put("msg", "登录错误次数过多");}String ip = IpUtil.getRemoteIp(request);// 判断同一ip错误次数是否过多if (FrequencyUtil.checkFrequency(ip, 10)) {logger.info("密码错误次数过多,当前管理员为:{}", username);return R.ok().put("msg", "登录错误次数过多");}AdminUser byUsernameAndPassword = adminUserService.findByUsernameAndPassword(username, pwd);if (byUsernameAndPassword == null) {logger.info("用户名或密码错误,当前管理员为:{}", username);return R.ok().put("msg", "用户名或密码错误");} else if (byUsernameAndPassword.getState().equals(0)) {return R.ok().put("msg", "账户已被冻结,请联系管理员");}session.setAttribute("user", byUsernameAndPassword.getUsername());logger.info("管理员登录成功.....当前登录的管理员为:{}", username);FrequencyUtil.removeFrequency(username);FrequencyUtil.removeFrequency(ip);return R.ok().put("code", "200");}
  • 工具类
public class KeyManager {//公钥private static String public_key;//私钥private static String private_key;public static String getPublic_key() {return public_key;}public static void setPublic_key(String public_key) {KeyManager.public_key = public_key;}public static String getPrivate_key() {return private_key;}public static void setPrivate_key(String private_key) {KeyManager.private_key = private_key;}}
public class RSAUtil {//生成秘钥对public static KeyPair getKeyPair() throws Exception {KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");keyPairGenerator.initialize(1024);KeyPair keyPair = keyPairGenerator.generateKeyPair();return keyPair;}//获取公钥(Base64编码)public static String getPublicKey(KeyPair keyPair){PublicKey publicKey = keyPair.getPublic();byte[] bytes = publicKey.getEncoded();return byte2Base64(bytes);}//获取私钥(Base64编码)public static String getPrivateKey(KeyPair keyPair){PrivateKey privateKey = keyPair.getPrivate();byte[] bytes = privateKey.getEncoded();return byte2Base64(bytes);}//将Base64编码后的公钥转换成PublicKey对象public static PublicKey string2PublicKey(String pubStr) throws Exception{byte[] keyBytes = base642Byte(pubStr);X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(keySpec);return publicKey;}//将Base64编码后的私钥转换成PrivateKey对象public static PrivateKey string2PrivateKey(String priStr) throws Exception{byte[] keyBytes = base642Byte(priStr);PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(keySpec);return privateKey;}//公钥加密public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);byte[] bytes = cipher.doFinal(content);return bytes;}//私钥解密public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] bytes = cipher.doFinal(content);return bytes;}//字节数组转Base64编码public static String byte2Base64(byte[] bytes){BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(bytes);}//Base64编码转字节数组public static byte[] base642Byte(String base64Key) throws IOException{BASE64Decoder decoder = new BASE64Decoder();return decoder.decodeBuffer(base64Key);}
}
  • Service层
public interface AdminUserService {String encryptionResult(String password) throws Exception;
}
  • impl实现层
/*** rsa算法解密** @param password* @return* @throws Exception*/@Overridepublic String encryptionResult(String password) throws Exception {// 将Base64编码后的私钥转换成PrivateKey对象PrivateKey privateKey = RSAUtil.string2PrivateKey(KeyManager.getPrivate_key());// 加密后的内容Base64解码byte[] base642Byte = RSAUtil.base642Byte(password);// 用私钥解密byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);return new String(privateDecrypt);}
  • 登录界面
    在这里插入图片描述
  • 前端加密后的密码
    IvY24AmBOh2ihwDL4yalYzB58/EEUw4ELzkp4e6hxZJPdm5XQ3K40DzZlUAcLZ3azXBIz72o8d+8niP0siqHvNIF03Bf9xlW3OeeOm71KfUwO1FcCrQMtLuqecfufalm6EXX4po5o25w04zXoJ5Nbm14p4uPjZ/I4u//PSog+e0=
  • 后端解密后的密码
    Sjfh@123

主:以上所有代码复制即用,详细的登录逻辑需要自己实现…

至此:简单的rsa前端加密后端解密功能就实现了

q:2414701902

如果有什么问题,欢迎大家指导。并相互联系,希望能够通过文章互相学习。


http://chatgpt.dhexx.cn/article/CdEmeCx4.shtml

相关文章

公钥加密应用--数字签名

公钥加密应用–数字签名 文章目录 公钥加密应用--数字签名概述私钥加密HASH算法签名算法的使用流程签名算法的应用认证身份完整性不可抵赖性 撤销签名密钥签名算法DSA算法ECDSA算法ECDSA--SM2签名算法 Reference 概述 ​ 本文主要带大家详细了解数字签名的一些知识&#xff0c…

公钥加密

公钥密码又称为非对称密码&#xff0c;拥有公钥密码的用户分别拥有加密密钥和解密密钥。通过加密密钥不能得到解密密钥。并且加密密钥是公开的。 什么是密钥配送问题 在现实世界中使用对称密码时&#xff0c;我们一定会遇到密钥配送问题( key distribution problem)。 假如 …

SM2公钥加密与解密

SM2加解密 字符串公钥SM2加解密工具类 始于我工作中的一个需求&#xff0c;在网上很少找到资料所以来写一下 字符串公钥 公钥是对接方给的&#xff0c;是一个长字符串类似于这种格式 工具类中也有生成公私钥的方法&#xff0c;我测试加解密也能成功 秘钥生成借鉴的是这个文章…

公钥加密算法-RSA

RSA算法 RSA算法是由它的三位开发者&#xff0c;即 Ron Rivest、Adi Shamir 和 Leonard Adleman 的姓氏的首字母组成的 (Rivest-Shamir-Adleman)。 RSA 可以被用于公钥加密、数字签名和密钥交换。 文章目录 RSA算法一、简介二、RSA算法流程1、 加密或者验签过程2、解密或者签名…

公钥加密与PKI实验

环境&#xff1a;安装openssl库和bless工具 1.openssl库 2.bless工具 一&#xff0e;成为数字证书认证机构 1.新建一个工作目录 2.为CA生成自签名证书 二&#xff0e;为PKILabServer.com生成证书 1.生成公开/私有密钥对 2.生成证书签名请求 3.生成证书 三&#xff0e;在网站中…

java公钥加密私钥解密_公钥加密,私钥解密示例程序(JAVA)详解

公钥与私钥机制属于非对称加密的范畴&#xff0c;非对称是相对于对称加密而言的&#xff0c;对称加密用于加密与解密的密钥是同一把&#xff0c;而非对称加密则用于加密与解密的密钥不相同&#xff0c;一个公开&#xff0c;称为公钥&#xff1b;一个保密&#xff0c;称为私钥&a…

c#加密:四、公钥加密和签名

一、公钥加密 1、概述 公钥加密是非对称的&#xff0c;因此加密和解密需要使用不同的密钥。对称加密的密钥可以是一串长度合适的任意字节串。但非对称加密则需要专门制作一对密钥。 这个密钥对包含一个公钥和一个私钥。它们将一起完成如下工作&#xff1a; 公钥加密消息&#…

为什么用公钥加密却不能用公钥解密?

本文为掘金社区首发签约文章&#xff0c;14天内禁止转载&#xff0c;14天后未获授权禁止转载&#xff0c;侵权必究&#xff01; 一直以来我都在逃避写HTTPS。 毕竟。 HTTPS里名词太多。概念又巨繁琐。 实在是太难解释了&#xff0c;能不写我尽量不写。。。。 但为了让图解网…

公钥加密算法

3 公钥加密算法 加密工具使用 运行RSA-Tool&#xff0c;点击Start按钮&#xff0c;滑动鼠标生成一个随机数。点击Generate生成一对大质数p、q及Npq。 自己选定一个可用的整数e(可使用默认的10001&#xff0c;也可以使用其他的)。点击Calc.D生成d。 3 将公钥(N,e)发送给小伙…

2021-05-05

5.5 实训主题&#xff1a;密码学&#xff08;后续&#xff09; 公钥加密算法 RSA算法的数学原理 阅读RSA.cpp中的代码&#xff0c;了解其实现方法。调试运行RSA.cpp&#xff0c;选择两个不同的素数。根据生成的d和e验证公式de ≡ 1 (mod (p-1)(q-1))。输入要加密的数。验证…

公钥密钥加密原理

转载自&#xff1a;https://www.jianshu.com/p/0bf1c3c047e5 第6章 网络编程与网络框架 6.1 公钥密钥加密原理 6.1.1 基础知识 密钥&#xff1a;一般就是一个字符串或数字&#xff0c;在加密或者解密时传递给加密/解密算法。对称加密算法&#xff1a;加密和解密都是使用的同…

公钥密码体制及RSA公钥加密算法(上)

一.公钥密码体制 &#xff08;1&#xff09;公钥&#xff08;非对称&#xff09;密码概述&#xff1a; 优点&#xff1a;免去密钥分发&#xff0c;密钥管理简单 &#xff08;2&#xff09;公钥加密的设计原则&#xff1a; 已知公私钥&#xff0c;进行加密解密是简单容易的 单…

网关与路由器的区别!!

自我认识&#xff1a; “网关” &#xff1a;让两个不同网络相互之间进行通信&#xff0c;可以使具有不同协议的网络相互连接。 “路由器”&#xff1a;能在计算机网络之间发送和接收数据包的设备&#xff0c;并且提供最佳路由路径的一种网络互联设备。但是只能在使用相同协议…

什么是网关和网关的作用

什么是网关? 网关(Gateway)指的是这一个网络连接到另一个网络的“关口”&#xff0c;它又称网间连接器、协议转换器。在Internet网里&#xff0c;网关的作用是一种连接内部网与Internet上其它网的一种中间设备&#xff0c;通俗来说&#xff0c;也叫做“路由器”。网关地址是能…

微服务——API网关

什么是API网关 API网关&#xff08;API Gateway&#xff09;是应用服务之前的系统&#xff0c;用来代理业务系统的服务API&#xff0c;在网关层处理所有的非业务功能&#xff0c;实现管理授权、访问控制和流量限制等功能。这样业务系统就可以专注于业务&#xff0c;而不用去处…

网关和路由器的区别

摘要&#xff1a;网关是逻辑概念,路由器是物理设备,路由器可以作为网关来使用。路由器是一个设备&#xff0c;而网关是一个结点&#xff08;概念层&#xff09;。应该说&#xff1a;路由器可以实现网关的功能。另外&#xff0c;网关的功能还可以由局域网中一台双网卡的机器&…

PowerBI Server端管理数据网关

作为企业PowerBI的管理员&#xff0c;日常工作除了分发报表&#xff0c;可能就是管理网关了&#xff0c;目前国际版的Server端已经更新了网关管理的页面&#xff0c;但是入口没变&#xff0c;仍然是设置—管理网关。 新建数据源 进入管理网关页面后&#xff0c;左上角有一个新…

守护企业网关

相比UTM、防毒墙等概念&#xff0c;用户关注的只是网关处如何确保网络安全以及对病毒攻击的拦截。 有一天&#xff0c;在一家大型外企工作的小王&#xff0c;利用午休空挡通过MSN将一个名为Dancing Skeleton的游戏小程序传给朋友分享&#xff0c;当消息发过去后&#xff0c;屏…

网关介绍

网关(Gateway)又称网间连接器、协议转换器。网关在网络层以上实现网络互连&#xff0c;是最复杂的网络互连设备&#xff0c;仅用于两个高层协议不同的网络互连。网关既可以用于广域网互连&#xff0c;也可以用于局域网互连。 网关是一种充当转换重任的计算机系统或设备。在使用…

微服务网关——需求篇

概念 API Gateway&#xff08;API GW / API 网关&#xff09;&#xff0c;顾名思义&#xff0c;是企业 软件系统在系统边界上提供给外部访问内部接口服务的统一入口。网关并不是微服务所特有的&#xff0c;实际上网关在微服务之前就已经存在很久了&#xff0c;例如银行、证券等…