package com.zzg.common.utils; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * aes 加密的工具类 * 1.存储 加密的秘钥key * 2.实现 aes 加密 * 3.实现aes解密的功能 */ public class AESUtil { // 定义 aes 加密的key // 密钥 必须是16位, 自定义, // 如果不是16位, 则会出现InvalidKeyException: Illegal key size // 解决方案有两种: //1.需要安装Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files(可以在Oracle下载). //2.设置设置key的长度为16个字母和数字的字符窜(128 Bit/8=16字符)就不报错了。 private static String key = "congzhouland8088"; //定义加密的编码 private static String charset = "utf-8"; // 偏移量 private static int offset = 16; private static String transformation = "AES/CBC/PKCS5Padding"; private static String algorithm = "AES"; /** * 加密 * * @param content * @return */ public static String encrypt(String content) { return encrypt(content, key); } /** * 解密 * * @param content * @return */ public static String decrypt(String content) { return decrypt(content, key); } /** * 加密 * * @param content 需要加密的内容 * @param key 加密密码 * @return */ public static String encrypt(String content, String key) { if (StringUtils.isEmpty(content)) { return ""; } try { SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm); IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset); Cipher cipher = Cipher.getInstance(transformation); byte[] byteContent = content.getBytes(charset); cipher.init(Cipher.ENCRYPT_MODE, skey, iv);// 初始化 byte[] result = cipher.doFinal(byteContent); return new Base64().encodeToString(result); // 加密 } catch (Exception e) { // LogUtil.exception(e); } return null; } /** * AES(256)解密 * * @param content 待解密内容 * @param key 解密密钥 * @return 解密之后 * @throws Exception */ public static String decrypt(String content, String key) { try { SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm); IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化 byte[] result = cipher.doFinal(new Base64().decode(content)); return new String(result); // 解密 } catch (Exception e) { //LogUtil.exception(e); } return ""; } public static void main(String[] args) { System.out.println(encrypt("431222199408086874")); } }