package cn.huge.base.common.utils; import cn.huge.base.common.exception.BaseException; import com.thoughtworks.xstream.core.util.Base64Encoder; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * @title: aes加密工具 * @description: aes加密工具 * @company: hugeinfo * @author: liyj * @time: 2021-11-05 16:51:48 * @version: 1.0.0 */ public class AesUtils { /** * aes加密秘钥 */ public static final String AES_PASSWORD = "tVTShp12fFVkNBxV"; /** * 默认的加密方式 */ public static final String KEY_ALGORITHM = "AES"; /** * 默认的加密算法 */ public static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /** * 加密 * @param content * @return * @throws Exception */ public static String encode(String content) { try { byte[] raw = AES_PASSWORD.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); //"算法/模式/补码方式" Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(content.getBytes("utf-8")); return byte2Base64(encrypted); } catch (Exception e) { throw new BaseException("AesUtils.encodeByte", e); } } /** * 加密 * @param content * @return * @throws Exception */ public static byte[] encodeByte(String content) { try { byte[] raw = AES_PASSWORD.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); //"算法/模式/补码方式" Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(content.getBytes("utf-8")); return encrypted; } catch (Exception e) { throw new BaseException("AesUtils.encodeByte", e); } } /** * 加密 * @param data 返回数据 * @return ReturnBo * @throws Exception */ public static byte[] encodeByte(Object data) { try { //加密 byte[] raw =AES_PASSWORD.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); JsonMapper jsonMapper = new JsonMapper(); byte[] encrypted = cipher.doFinal(jsonMapper.toJson(data).getBytes("utf-8")); return encrypted; } catch (Exception e) { throw new BaseException("AesUtils.encodeByte", e); } } /** * 解密 * @param content * @return * @throws Exception */ public static String decodeByte(byte[] content) { try { byte[] raw = AES_PASSWORD.getBytes(); SecretKeySpec key = new SecretKeySpec(raw, KEY_ALGORITHM); //创建密码器 Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(content); //AES解密 return new String(result); } catch (Exception e) { throw new BaseException("AesUtils.decodeByte", e); } } /** * 解密 * @param content * @return * @throws Exception */ public static String decode(String content) { try { byte[] raw = AES_PASSWORD.getBytes(); SecretKeySpec key = new SecretKeySpec(raw, KEY_ALGORITHM); //创建密码器 Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); //base64解密 byte[] contentByte = new Base64().decode(content); byte[] result = cipher.doFinal(contentByte); //AES解密 return new String(result,"utf-8"); } catch (Exception e) { throw new BaseException("AesUtils.decode", e); } } /** * 字节数组转Base64编码 * * @param bytes * @return */ public static String byte2Base64(byte[] bytes) { Base64Encoder encoder = new Base64Encoder(); return encoder.encode(bytes); } } /** * -------------------_ooOoo_------------------- * ------------------o8888888o------------------ * ------------------88" . "88------------------ * ------------------(| -_- |)------------------ * ------------------O\ = /O------------------ * ---------------____/`---'\____--------------- * -------------.' \\| |// `.------------- * ------------/ \\||| : |||// \------------ * -----------/ _||||| -:- |||||- \----------- * -----------| | \\\ - /// | |----------- * -----------| \_| ''\---/'' | |----------- * -----------\ .-\__ `-` ___/-. /----------- * ---------___`. .' /--.--\ `. . __---------- * ------."" '< `.___\_<|>_/___.' >'"".------- * -----| | : `- \`.;`\ _ /`;.`/ - ` : | |----- * -----\ \ `-. \_ __\ /__ _/ .-` / /----- * ======`-.____`-.___\_____/___.-`____.-'====== * -------------------`=---=' * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * ---------佛祖保佑---hugeinfo---永无BUG---------- */