后端
JDK1.8 自带有 crypto 包,不需要额外引入其他依赖。
封装
package com.wt.security.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.UUID;
/**
* @author big uncle
* @date 2021/3/16 14:17
* @module
**/
public class AesEncryptUtil {
/**
* 加密方法
* @param data 要加密的数据
* @param key 加密key
* @return 加密的结果
* @throws Exception
*/
public static String encrypt(String data, String key){
try {
String iv = key;
//"算法/模式/补码方式"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
* @return 解密的结果
* @throws Exception
*/
public static String desEncrypt(String data, String key) {
try {
String iv = key;
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);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 生成默认的 key 和 iv,key = iv iv 的长度必须是16位
**/
public static String generateKeyAndIv(){
String uid = SecureUtil.md5(String.valueOf(System.currentTimeMillis()));
// 盐加密
String salt = UUID.randomUUID().toString();
uid = SecureUtil.md5(uid + salt);
return uid.substring(16);
}
}
前端
1. 引用依赖
npm install crypto-js
2. 封装
封装加密工具
import CryptoJS from 'crypto-js/crypto-js'
/**
* AES加密 :字符串 key iv 返回base64
*/
export function Encrypt(word, keyStr) {
let key,iv,ivStr;
if (!keyStr) {
throw new Error("keyStr 不能为空");
}
ivStr = 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
});
// console.log("-=-=-=-", encrypted.ciphertext)
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
* AES 解密 :字符串 key iv 返回base64
*/
export function Decrypt(word, keyStr) {
let key,iv,ivStr;
if (!keyStr) {
throw new Error("keyStr 不能为空");
}
ivStr = 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);
var decrypt = CryptoJS.AES.decrypt(src, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
使用
import {Encrypt} from "@/utils/AesEncryptUtil";
let u = Encrypt(userAccount.trim(),key)
let p = Encrypt(userPassword.trim(),key)
# key 这个值也需要传入后端
let k = key
网友评论