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.DecoderException; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; /** * @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"; private static final String iv = "1qaz#EDC123456(,"; private static final String Algorithm = "AES"; private static final String AlgorithmProvider = "AES/CBC/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); } } public static String decrypt(String enc, String uniqueKey) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidKeyException, DecoderException, IllegalBlockSizeException, BadPaddingException { byte[] key = uniqueKey.getBytes(); SecretKey secretKey = new SecretKeySpec(key, Algorithm); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8")); Cipher cipher = Cipher.getInstance(AlgorithmProvider); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); byte[] hexBytes = hexStringToBytes(enc); byte[] plainBytes = cipher.doFinal(hexBytes); return new String(plainBytes, "UTF-8"); } public static String encrypt(String plainText, String uniqueKey) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { byte[] key = uniqueKey.getBytes(); SecretKey secretKey = new SecretKeySpec(key, Algorithm); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8")); Cipher cipher = Cipher.getInstance(AlgorithmProvider); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8")); return Hex.encodeHexString(encryptedBytes); } /** * 将16进制字符串转换为byte数组 * * @param hexString * @return */ private static byte[] hexStringToBytes(String hexString) throws DecoderException { return Hex.decodeHex(hexString); } /** * 字节数组转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---------- */