一、前言
整理一下AES加解密的工具类;
加解密分为含有特殊字符和去除特殊字符(只包含字母和数字)两种算法;
有些场景识别不了特殊字符,因此需要去除特殊字符的加解密算法。
二、具体工具类
package com.square.mall.common.util;
import com.square.mall.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
/**
* AES加解密工具类
*
* @author Gencent
* @date 2020/7/2
*/
@Slf4j
public class AesUtil {
private static final String ALGORITHM = "AES";
public static void main(String[] args) {
String cltk = "{\"addressDetail\":\"xx软件园\",\"area\":\"浑南新区\",\"city\":\"沈阳市\",\"consignee\":\"xx\",\"contact\":\"15940130000\",\"mobileNo\":\"15940130000\",\"monthlyId\":\"165223\",\"province\":\"辽宁省\"}";
String key = "jkkHpwub6S3z32vr";
String encrypt = encrypt(cltk, key);
log.info("加密后:" + encrypt.length() + " " + encrypt);
String decryptToken = decryptToken(encrypt, key);
log.info("解密后:" + decryptToken);
}
/**
* 加密
*/
public static String encrypt(String str, String password) {
try {
byte[] enCodeFormat = getSecretKey(password);
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
byte[] byteContent = str.getBytes(CommonConstant.ENCODE_UTF_8);
// 初始化
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
// 加密
return Base64.encodeBase64String(result);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | UnsupportedEncodingException
| IllegalBlockSizeException | BadPaddingException e) {
log.error("encrypt password failed, ", e);
}
return null;
}
/**
* 加密,去除特殊字符
*/
public static String encryptExcludeSpecialChars(String str, String password) {
try {
byte[] enCodeFormat = getSecretKey(password);
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
byte[] byteContent = str.getBytes(CommonConstant.ENCODE_UTF_8);
// 初始化
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
return StringUtil.getBytesToHexStr(result);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | UnsupportedEncodingException
| IllegalBlockSizeException | BadPaddingException e) {
log.error("encrypt password failed, ", e);
}
return null;
}
/**
* 解密
*/
public static String decryptToken(String str, String password) {
log.info("解密密文:{},秘钥:{}", str, password);
// 长token解密得到明文token
String realToken = "";
try {
realToken = decryptEcb(str, password);
} catch (Exception e) {
log.error("decrypt password failed, ", e);
}
log.info("解密明文:{}", realToken);
return realToken;
}
/**
* 解密,没有特殊字符情况下
*/
public static String decryptExcludeSpecialChars(String str, String password) {
log.info("解密密文:{},秘钥:{}", str, password);
String decryptStr = "";
try {
decryptStr = decryptEcbExcludeSpecialChars(str, password);
} catch (Exception e) {
log.error("decrypt password failed, ", e);
}
log.info("解密明文:{}", decryptStr);
return decryptStr;
}
private static String decryptEcb(String data, String key) throws Exception {
if (StringUtil.isBlank(key) || key.length() < 16) {
return null;
}
// 将字符串转化为字节数组
byte[] decryptdata = Base64.decodeBase64(data);
byte[] skey = getSecretKey(key);
// 解密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, toKey(skey));
byte[] result = cipher.doFinal(decryptdata);
// 转化为字符串
return new String(result);
}
private static String decryptEcbExcludeSpecialChars(String data, String key) throws Exception {
if (StringUtil.isBlank(key) || key.length() < 16) {
return null;
}
// 将字符串转化为字节数组
byte[] decryptdata = StringUtil.getHexStrToBytes(data);
byte[] skey = getSecretKey(key);
// 解密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, toKey(skey));
byte[] result = cipher.doFinal(decryptdata);
// 转化为字符串
return new String(result);
}
private static byte[] getSecretKey(String key) throws UnsupportedEncodingException {
return key.substring(0, 16).getBytes(CommonConstant.ENCODE_UTF_8);
}
private static Key toKey(byte[] key) throws IllegalArgumentException {
return new SecretKeySpec(key, ALGORITHM);
}
}
三、个人电商中台开源项目
最后,附上个人开源电商业务中台项目:
square-pavilion(四方阁)
Square pavilion is a cube project for e-commerce.(四方阁是一个为电商而生的中台项目)。 在玄幻小说中,总有一些比较牛逼且左右逢源于各个势力之间的中立组织,这些组织通常带有“阁”字。因此本项目取名“四方阁”,取包容并蓄,吸取百家之长之意。欢迎有兴趣的小伙伴们一起努力,把这个项目完善、推广。 Github square-pavilion