1.下载crypot-js,如果为vue项目,安装cnpm,采用命令安装:cnpm install crypto-js,如果为一般web项目,https://github.com/brix/crypto-js,进行下载;
2. 前端引入crypto-js文件,然后新建一个js文件,命名为secrt.js(文件名称可自行修改),复制以下内容:
import CryptoJS from 'crypto-js'
// 默认的 KEY 与 iv 如果没有给,可自行设定,但必须16位
const KEY = CryptoJS.enc.Utf8.parse("dzst12345678zjyj");
const IV = CryptoJS.enc.Utf8.parse('dzst12345678zjyj');
/**
* 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();
}
3.前端页面引入,调用
import {Encrypt} from '@/utils/encryption/secrt.js'
let userName= ‘小花’;
let password= ‘123465678ZCH’;
//请求后台参数加密,访问后台请求方法可以自己写:
let param = ‘http://localhost:8080/sys/login?’+ ‘userName=’+ Encrypt(userName)+‘&&’+‘password=’+ Encrypt(password);
- 后端pom文件中添加
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
然后创建SecretUtil类,具体代码如下:
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 = "dzst12345678zjyj";
private static String IV = "dzst12345678zjyj";
/***
* 加密
* @param data 要加密的数据
* @return encrypt
*/
public static String encrypt(String data){
return encrypt(data, KEY, IV);
}
/***
* param data 需要解密的数据
* 调用desEncrypt()方法
*/
public static String desEncrypt(String data){
return desEncrypt(data, KEY, IV);
}
/**
* 加密方法
* @param data 要加密的数据
* @param key 加密key
* @param iv 加密iv
* @return 加密的结果
*/
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;
}
}
/**
* 解密方法
* @param data 要解密的数据
* @param key 解密key
* @param iv 解密iv
* @return 解密的结果
*/
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;
}
}
}
- 后端解密
import static org.jeecg.modules.esdrs.utils.SecretUtil.desEncrypt;
public class test
{
@GetMapping(value = "/login")
public login(@RequestParam(name = "userName") String userName,@RequestParam(name = "password") String password,)
String userNamedes= desEncrypt(userName);
String passworddes= desEncrypt(password);
}
注意:前端加密后的字符串可能包含特殊符号,传到后端时需要注意;