前端
安装
npm install crypto-js
使用
// 加密
Encrypt (content) {
/* content 要加密的内容 */
let iv = '0000000000000000'
const secretKey = 'a90e7d2eb24c4b8b88994ab065e377d3' /* 前后端约定好的key */
const msg = CryptoJS.enc.Utf8.parse(content)
const key = CryptoJS.enc.Utf8.parse(secretKey)
iv = CryptoJS.enc.Utf8.parse(iv)
const encrypt = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypt.toString()
},
// 解密
Decrypt (content) {
/* content 要解密的内容 */
let iv = '0000000000000000'
const secretKey = 'a90e7d2eb24c4b8b88994ab065e377d3' /* 前后端约定好的key */
const key = CryptoJS.enc.Utf8.parse(secretKey)
iv = CryptoJS.enc.Utf8.parse(iv)
const decrypt = CryptoJS.AES.decrypt(content, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return decrypt.toString(CryptoJS.enc.Utf8)
},
后端
package com.yofc.admin.utils;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
public class AESpkcs7paddingUtil {
/**
* 密钥算法
*/
private static final String KEY_ALGORITHM = "AES";
/**
* 加密/解密算法 / 工作模式 / 填充方式
* Java 6支持PKCS5Padding填充方式
* Bouncy Castle支持PKCS7Padding填充方式
*/
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";
/**
* 偏移量,只有CBC模式才需要
*/
private final static String ivParameter = "0000000000000000";
/**
* AES要求密钥长度为128位或192位或256位,java默认限制AES密钥长度最多128位
*/
public static String sKey="a90e7d2eb24c4b8b88994ab065e377d3" ;
/**
* 编码格式
*/
public static final String ENCODING = "utf-8";
static {
//如果是PKCS7Padding填充方式,则必须加上下面这行
Security.addProvider(new BouncyCastleProvider());
}
/**
* AES加密
* @param source 源字符串
* @return 加密后的密文
* @throws Exception
*/
public static String encrypt(String source) throws Exception {
byte[] sourceBytes = source.getBytes(ENCODING);
byte[] keyBytes = sKey.getBytes(ENCODING);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(ENCODING));
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM),iv);
byte[] decrypted = cipher.doFinal(sourceBytes);
return Base64.encodeBase64String(decrypted);
}
/**
* AES解密
* @param encryptStr 加密后的密文
* @return 源字符串
* @throws Exception
*/
public static String decrypt(String encryptStr) throws Exception {
byte[] sourceBytes = Base64.decodeBase64(encryptStr);
byte[] keyBytes = sKey.getBytes(ENCODING);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(ENCODING));
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM),iv);
byte[] decoded = cipher.doFinal(sourceBytes);
return new String(decoded, ENCODING);
}
public static void main(String[] args) throws Exception {
// 加密
long lStart = System.currentTimeMillis();
String enString = AESpkcs7paddingUtil.encrypt("abcd中文测试加标点符号!@#¥%……&*(+——)(*&~,。,;,,/;lkk;ki;'[p]./,'\\467646789");
System.out.println("加密后的字串是:" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
// 解密
lStart = System.currentTimeMillis();
String deString = AESpkcs7paddingUtil.decrypt(enString);
System.out.println("解密后的字串是:" + deString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
}