java前后端加密解密crypto-js的实现
作者:渔夫搬砖 时间:2023-11-29 12:09:31
项目场景:
适用于接口数据敏感信息,比如 明文传输姓名、居住地址、手机号等信息,如果存在明文传输敏感数据问题、及数据泄漏风险,则可使用此方法加密解密。
一、下载crypot-js
1.如果为vue项目,安装cnpm,采用命令安装即可:
安装命令:cnpm install crypto-js;
2.如果为一般web项目,https://github.com/brix/crypto-js,进行下载;
二、前端引入crypto-js文件,并命名为secrt.js
import CryptoJS from 'crypto-js'
// 默认的 KEY 与 iv 如果没有给,可自行设定,但必须16位
const KEY = CryptoJS.enc.Utf8.parse("abcdefgh12345678");
const IV = CryptoJS.enc.Utf8.parse('12345678abcdefgh');
/**
* AES加密 :字符串 key iv 返回base64
*/
export function Encrypt(word, keyStr, ivStr) {
let key = KEY;
let iv = IV;
if (keyStr) {
key = CryptoJS.enc.Utf8.parse(keyStr);
iv = CryptoJS.enc.Utf8.parse(ivStr);
}
let srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
1. AES 解密 :字符串 key iv 返回base64
2. 3. @return {string}
*/
export function Decrypt(word, keyStr, ivStr) {
let key = KEY;
let iv = IV;
if (keyStr) {
key = CryptoJS.enc.Utf8.parse(keyStr);
iv = CryptoJS.enc.Utf8.parse(ivStr);
}
let base64 = CryptoJS.enc.Base64.parse(word);
let src = CryptoJS.enc.Base64.stringify(base64);
let decrypt = CryptoJS.AES.decrypt(src, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
三、前端页面引入并加密数据
import {Encrypt} from '../../secrt.js'
//text为需要加密的内容
var userId= Encrypt(text);
四、后端解密数据
1.pom引用
2.创建工具类
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class SecretUtil {
/***
* key和iv值可以随机生成,确保与前端的key,iv对应
*/
private static String KEY = "abcdefgh12345678";
private static String IV = "12345678abcdefgh";
/***
* 加密
*
* @param data 要加密的数据
* @return encrypt
*/
public static String encrypt(String data) {
return encrypt(data, KEY, IV);
}
/***
* 解密
* @param data 要解密的数据
*/
public static String desEncrypt(String data) {
return desEncrypt(data, KEY, IV);
}
/**
* 加密方法
*/
private static String encrypt(String data, String key, String iv) {
try {
// "算法/模式/补码方式"NoPadding PkcsPadding
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new Base64().encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解密方法
*/
private static String desEncrypt(String data, String key, String iv) {
try {
byte[] encrypted1 = new Base64().decode(data);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] original = cipher.doFinal(encrypted1);
return new String(original).trim();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
3.数据解密
@GetMapping(value = "/userInfo")
public getUserInfo(@RequestParam(name = "userId") String userId)
String userId= desEncrypt(userId);
}
来源:https://blog.csdn.net/myyw001/article/details/128408579
标签:java,crypto-js,加密,解密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
改变JAVA窗体属性的操作方法
2022-02-17 02:10:59
![](https://img.aspxhome.com/file/2023/5/101795_0s.jpg)
Android开发之DialogFragment用法实例总结
2023-08-30 13:20:21
Android购物车项目快速开发
2021-09-11 01:28:07
C++中string字符串分割函数split()的4种实现方法
2023-02-05 23:28:48
Spring Security OAuth 自定义授权方式实现手机验证码
2021-10-03 23:30:27
![](https://img.aspxhome.com/file/2023/2/72282_0s.png)
java开发之读写txt文件操作的实现
2023-11-17 06:00:23
![](https://img.aspxhome.com/file/2023/3/59553_0s.png)
Android禁止横屏竖屏切换的有效方法
2023-05-05 10:56:16
java读取word-excel-ppt文件代码
2022-07-06 14:20:46
Java动态脚本Groovy获取Bean技巧
2023-08-21 19:41:15
![](https://img.aspxhome.com/file/2023/5/132015_0s.png)
Unity实现切割图集工具
2021-07-03 23:14:57
![](https://img.aspxhome.com/file/2023/4/82124_0s.jpg)
springmvc 参数绑定总结
2023-11-16 21:30:44
c# 二分查找算法
2023-10-24 04:42:45
![](https://img.aspxhome.com/file/2023/9/84439_0s.png)
C# ComboBox的联动操作(三层架构)
2022-06-21 16:37:56
![](https://img.aspxhome.com/file/2023/5/113785_0s.png)
idea首次使用需要配置哪些东西
2022-05-16 20:01:37
![](https://img.aspxhome.com/file/2023/6/129956_0s.png)
Android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)demo实例详解
2023-05-31 15:35:27
![](https://img.aspxhome.com/file/2023/7/111737_0s.gif)
java 集合----Map、Collection
2022-11-09 03:39:01
Java掩码的几种使用例举
2022-02-11 11:08:04
Java的关键字与保留字小结
2021-05-30 10:20:50
SpringBoot解决跨域请求拦截问题代码实例
2021-07-18 12:08:54
![](https://img.aspxhome.com/file/2023/4/67964_0s.png)
C#实现一键换IP、重置DNS、网关及掩码的方法
2021-10-03 00:32:33