若依基于sm-crypto实现前后端登录密码加密

上一节介绍了基于jsencrypt实现的密码加密解密登录功能,这次来介绍基于sm-crypto实现前后端登录密码加密,本次采用的是sm2进行的加密解密。

后端

首先从后端代码开始写起(因为公钥和私钥都是要从java代码中生成):
首先需要引入sm-crypto的jar包,点击获取jar包,然后拷贝到下面的目录下
在这里插入图片描述
然后再pom.xml文件中引入依赖,然后刷新依赖

        <!--sm2jar包依赖--><dependency><groupId>com.sm2</groupId><artifactId>sm2</artifactId><version>1.0</version><scope>system</scope><systemPath>${basedir}/src/main/resources/lib/sm-crypto-0.3.2.jar</systemPath></dependency>

然后就是新建一个工具类

package com.ruoyi.common.utils.sm2Encrypt;import com.antherd.smcrypto.sm2.Keypair;
import com.antherd.smcrypto.sm2.Sm2;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Component;import java.util.ArrayList;@Component
public class SM2Encryptor {private static final SmKeyPair smKeyPair = new SmKeyPair();public static String privateKeys = "";private static String publicKeyStr = "";private static String privateKeyStr = "92b704fee9ac448ac27abfa537021ab42d0035151f01fdc03e49b0c2f5496148";/*** 加密,使用公钥** @param publicKey* @param originalText* @return*/public static String encryptText(String publicKey, String originalText) throws Exception {if (StringUtil.isNullOrEmpty(publicKey)) {throw new Exception("密钥不能为空...");}if (StringUtil.isNullOrEmpty(originalText)) {throw new Exception("明文不能为空...");}try {return Sm2.doEncrypt(originalText, publicKey);//HexUtil.encodeHexStr(cipherText); // 加密结果} catch (Exception e) {throw new Exception("加密错误:密钥不正确...");}}public static String getPublicKey() {return publicKeyStr;}public static String getPrivateKey() {return privateKeyStr;}public static SmKeyPair smKeyPair() {return smKeyPair;}/*** 解密,使用私钥** @param privateKey* @param cipherText* @return*/public static String decryptText(String privateKey, String cipherText) throws Exception {if (StringUtil.isNullOrEmpty(privateKey)) {throw new Exception("密钥不能为空...");}if (StringUtil.isNullOrEmpty(cipherText)) {throw new Exception("明文不能为空...");}try {String s = Sm2.doDecrypt(cipherText, privateKey);System.out.println(s);return Sm2.doDecrypt(cipherText, privateKey); // new String(sm2.decrypt(sourceData,prvKey)); // 解密结果} catch (Exception e) {throw new Exception("解密错误:密钥不正确...");}}/*** 获取sm2密钥对,** @return 返回String[];第0个为公钥,第1个为私钥* @throws Exception*/public static String[] generateKeyPair() throws Exception {try {Keypair keypair = Sm2.generateKeyPairHex();String[] result = new String[2];if (keypair != null) {result[0] = keypair.getPublicKey(); //公钥smKeyPair.setPublicKey(keypair.getPublicKey());publicKeyStr = keypair.getPublicKey();result[1] = keypair.getPrivateKey(); // 私钥smKeyPair.setPrivateKey(keypair.getPrivateKey());privateKeyStr = keypair.getPrivateKey();}return result;} catch (Exception e) {throw new Exception("生成密钥对失败...");}}public static void main(String[] args) throws Exception {
//		生成一对 公钥与私钥String[] keys = generateKeyPair();
//		公钥String publicKey = keys[0];
//		String publicKey = publicKeyStr;System.out.println("公钥" + publicKey);
//		私钥String privateKey = privateKeyStr;System.out.println("私钥" + privateKey);//		String str = "测试使用SM2加密、解密";String str = "166ed3fc69afcfee4f1ba2034c36dac93d86dcad176219407776100130a506d9";try {//加密字符串String encryptText = SM2Encryptor.encryptText(publicKey, str);System.out.println(encryptText);} catch (Exception e) {}//解密字符串String decryptText = "null";try {decryptText = SM2Encryptor.decryptText("", str);System.out.println("成功解密后为:"+decryptText);} catch (Exception e) {System.out.println("失败");}}public static void main11(String[] args) throws Exception {ArrayList<String> strings = new ArrayList<>();String str = ("$2a$10$0OXfRm8Jc4XdRFHeXFovB.UKX2DjLfgPYYS/6mz7KSym87LazK.6a,$2a$10$0OXfRm8Jc4XdRFHeXFovB.UKX2DjLfgPYYS/6mz7KSym87LazK.6a,$2a$10$0OXfRm8Jc4XdRFHeXFovB.UKX2DjLfgPYYS/6mz7KSym87LazK.6a,$2a$10$nByAilbqXIko9uf/hj5VKOXiHYlW0e7x0HDDrqLuc/BtYantNFe2u,$2a$10$4O5pYOYvPZPbZNiP5Pn2BeiMD7hTiZi4cenk6heyKFx12E2IMqlTG,$2a$10$ob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$10$6Sc/VvEvcxhAIgH7kUY60.QlM4xU2eHKFxdxXBsQmMZzis4GY2buS,$2a$10$G9FBv5rBu/stjHXImxETgukeTIK2DC65x9m3Ogd91UR8/7L/WhX9W");String[] split = str.split(",");//私钥String privateKey = privateKeyStr;for (String s : split) {//解密字符串String decryptText = SM2Encryptor.decryptText(privateKey, s);System.out.println("解密后密码:"+decryptText);}}/*** 获取sm2密钥对,** @return 返回String[];第0个为公钥,第1个为私钥* @throws Exception*/
//	@Beanpublic void startGenerateKeyPair() throws Exception {try {Keypair keypair = Sm2.generateKeyPairHex();String[] result = new String[2];if (keypair != null) {result[0] = keypair.getPublicKey(); //公钥smKeyPair.setPublicKey(keypair.getPublicKey());publicKeyStr = keypair.getPublicKey();result[1] = keypair.getPrivateKey(); // 私钥smKeyPair.setPrivateKey(keypair.getPrivateKey());privateKeyStr = keypair.getPrivateKey();}} catch (Exception e) {throw new Exception("生成密钥对失败...");}}/*** RSA密钥对对象*/public static class SmKeyPair {private String publicKey;private String privateKey;public SmKeyPair() {}public SmKeyPair(String publicKey, String privateKey) {this.publicKey = publicKey;this.privateKey = privateKey;}public String getPublicKey() {return publicKey;}public void setPublicKey(String publicKey) {this.publicKey = publicKey;}public String getPrivateKey() {return privateKey;}public void setPrivateKey(String privateKey) {this.privateKey = privateKey;}}}

然后运行里面的main方法获取公钥和私钥,保存起来
在这里插入图片描述
在这里插入图片描述
然后再application.yml文件中加入一个配置:
在这里插入图片描述

#SM2解密私钥
SM_privateKey: 92b704fee9ac448ac27abfa537021ab42d0035151f01fdc03e49b0c2f5496148

最后在登录接口那里调用解密方法

/*** 登录方法* * @param loginBody 登录信息* @return 结果*/@PostMapping("/login")public AjaxResult login(@RequestBody LoginBody loginBody) throws Exception{AjaxResult ajax = AjaxResult.success();// 生成令牌String token = loginService.login(loginBody.getUsername(), SM2Encryptor.decryptText(SM_privateKey, loginBody.getPassword()), loginBody.getCode(),loginBody.getUuid());ajax.put(Constants.TOKEN, token);return ajax;}

在这里插入图片描述
到此后端的工作就做完了

前端

前段需要先

npm i sm-crypto

在需要生成的模块,如common模块下新建一个工具类
前端代码:
在utils下新建smcrypto.js,把公钥和私钥放进来

import { sm2 } from 'sm-crypto'const publicKey = "04b9fd451e0d0d5517069076edbc7dc83b3a885307bc8cf00185769f6818bcdf958f8dc4c8575f33dcfbe0bb18c4f651892a05a4fd923f01db2e3ad09fc4bf2878"
const privateKey = '92b704fee9ac448ac27abfa537021ab42d0035151f01fdc03e49b0c2f5496148'
const cipherMode=1// sm2公钥加密
export function smEncryptor(password) {password = sm2.doEncrypt(password, publicKey, cipherMode)return password
}// sm私钥解密
export function decrypt(txt) {return sm2.doDecrypt(txt, privateKey, cipherMode)
}

然后在登录的地方进行加密:

      const password = smEncryptor(userInfo.password)

在这里插入图片描述
最后就是测试效果
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/345605.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI文本生图模型Stable Diffusion部署教程

本文基于CentOS8进行Stable Diffusion开源框架部署. 1. DNS配置(但是今天出现了偶尔无法下载问题) 为了加速Github访问,我在本机配置如下 (sd) [rootshenjian stable-diffusion-webui]# cat /etc/hosts 127.0.0.1 shenjian localhost localhost.localdomain localhost4 loca…

WPF真入门教程27--项目案例--设备数据实时监测

1、上图看效果 今天要做的一个案例是这样的效果&#xff0c;它能实时监测车间设备有关数据&#xff0c;并以表格和图形显示在界面上&#xff0c;这个比上个案例要复杂些&#xff0c;颜值也高些&#xff0c;通过这个来巩固wpf的技能&#xff0c;用到了命令绑定&#xff0c;样式…

论文翻译 | 【深入挖掘Java技术】「底层原理专题」深入分析一下并发编程之父Doug Lea的纽约州立大学的ForkJoin框架的本质和原理

深入分析一下并发编程之父Doug Lea的纽约州立大学的ForkJoin框架的本质和原理这里写目录标题 前提介绍摘要引言设计性能优秀任务粒度合理Cilk框架和基础fork/join的可移植性FJTask框架设计思路线程映射关系拆分子任务排队及调度设置调度管理 标准示例 未完待续 前提介绍 Doug …

C# 图解教程 第5版 —— 第21章 异步编程

文章目录 21.1 什么是 异步21.2 async/await 特性的结构21.3 什么是异步方法21.3.1 异步方法的控制流21.3.2 取消一个异步操作21.3.3 在调用方法中同步地等待任务21.3.4 在异步方法中异步地等待任务21.3.5 Task.Delay 方法 21.4 GUI 程序中的异步操作&#xff08;*&#xff09;…

获得利润和成长,应采取什么步骤, 澳福认为只需坚持一点

大多数交易者通常会考虑在外汇交易中获取利润&#xff0c;但只有少数人会思考这样一个问题:为了获得利润和专业成长&#xff0c;应该采取什么步骤。像“外汇交易怎么赢利”这样的文章很受市场欢迎&#xff0c;但是很少有人在交易中使用这些文章中给出的建议&#xff0c;因为在生…

面试题:说一说多线程常见锁的策略 ?

文章目录 前言一、乐观锁和悲观锁1.1 定义1.2 生动有趣滴例子1.3 版本号机制 二、读写锁2.1 读写锁的由来2.2 生动有趣de例子2.3 ReentrantReadWriteLock 类 三、重量级锁与轻量级锁3.1 定义3.2 生动活泼の例子3.3 自旋锁&#xff08;Spin Lock&#xff09; 四、公平锁与非公平…

Spark原理——Shuffle 过程

Shuffle 过程 Shuffle过程的组件结构 从整体视角上来看, Shuffle 发生在两个 Stage 之间, 一个 Stage 把数据计算好, 整理好, 等待另外一个 Stage 来拉取 放大视角, 会发现, 其实 Shuffle 发生在 Task 之间, 一个 Task 把数据整理好, 等待 Reducer 端的 Task 来拉取 如果更细…

JAVA数组以及小练习

目录 数组的概述和静态初始化 数组的地址值和元素访问 数组的遍历 数组的动态初始化 数组练习 数组的概述和静态初始化 package 数组;public class array1 {public static void main(String[] args){//格式//静态初始化//数据类型 [] 数组名 new 数组类型[]{元素1&#xf…

0104 AJAX介绍

Ajax 的全称是 Asynchronous Javascript And XML &#xff08;异步 JavaScript 和 XML &#xff09;。 通俗的理解&#xff1a;在网页中利用 XMLHttpRequest 对象和服务器进行数据交互的方式&#xff0c;就是 Ajax Ajax 能让我们轻松实现网页与服务器之间的数据交互。 浏览器…

一杯干红葡萄酒的酿造

一杯干红葡萄酒的酿造 一、什么是干红葡萄酒&#xff1f; 干红葡萄酒是指葡萄酒在酿造后&#xff0c;酿酒原料(葡萄汁)中的糖分完全转化成酒精&#xff0c;残糖量小于或等于4.00/L的红葡萄酒。 干红葡萄酒按颜色分可以分为 1&#xff0c;白葡萄酒:选择用白葡萄或浅色果皮的酿…

【光波电子学】基于MATLAB的多模光纤模场分布的仿真分析

基于MATLAB的多模光纤模场分布的仿真分析 一、引言 &#xff08;1&#xff09;多模光纤的概念 多模光纤&#xff08;MMF&#xff09;是一种具有较大纤芯直径的光纤结构&#xff0c;其核心直径通常在10-50微米范围内。与单模光纤&#xff08;SMF&#xff09;相比&#xff0c;…

【C++刷题】位运算

【C刷题】位运算 一、二进制中最右侧的11、位1的个数&#xff08;1&#xff09;题目链接&#xff08;2&#xff09;解析&#xff08;3&#xff09;代码 2、比特位计数&#xff08;1&#xff09;题目链接&#xff08;2&#xff09;解析&#xff08;3&#xff09;代码 3、汉明距离…