package com.izouma.nineth.utils; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; /** *

AES加密处理工具类

* * @author licoy.cn * @version 2018/9/5 */ public class AESEncryptUtil { private final static String password = "xRvGxVMeIbuQxICtstQCPjtyNuBluyyC"; /** * AES加密 * * @param content 字符串内容 */ public static String encrypt(String content) throws Exception { return aes(content, Cipher.ENCRYPT_MODE); } /** * AES解密 * * @param content 字符串内容 */ public static String decrypt(String content) throws Exception { return aes(content, Cipher.DECRYPT_MODE); } /** * AES加密/解密 公共方法 * * @param content 字符串 * @param type 加密:{@link Cipher#ENCRYPT_MODE},解密:{@link Cipher#DECRYPT_MODE} */ private static String aes(String content, int type) throws Exception { KeyGenerator generator = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(password.getBytes()); generator.init(128, random); SecretKey secretKey = generator.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(type, key); if (type == Cipher.ENCRYPT_MODE) { byte[] byteContent = content.getBytes("utf-8"); return Hex2Util.parseByte2HexStr(cipher.doFinal(byteContent)); } else { byte[] byteContent = Hex2Util.parseHexStr2Byte(content); return new String(cipher.doFinal(byteContent)); } } }