package com.proxgrind.chameleon.utils.security; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; /** * AES 加密方法,是对称的密码算法(加密与解密的密钥一致),这里使用最大的 256 位的密钥 */ public class AesUtils { private static final int size = 128; /** * 获得一个 密钥长度为 256 位的 AES 密钥, * * @return 返回经 BASE64 处理之后的密钥字符串 */ public static byte[] getStrKeyAES() throws NoSuchAlgorithmException, UnsupportedEncodingException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = new SecureRandom(String.valueOf(System.currentTimeMillis()).getBytes(Charset.forName("UTF-8"))); keyGen.init(size, secureRandom); // 这里可以是 128、192、256、越大越安全 SecretKey secretKey = keyGen.generateKey(); return secretKey.getEncoded(); } /** * 将使用 原生的类型byte的数组 secretKey 转为 SecretKey * * @param keyBytes raw key byte. * @return SecretKey */ public static SecretKey strKey2SecretKey(byte[] keyBytes) { return new SecretKeySpec(keyBytes, "AES"); } /** * 将使用 byte 类型的 secretKey 转为 SecretKey * * @param bytesKey * @return SecretKey */ public static SecretKey bytesKey2SecretKey(byte[] bytesKey) { return new SecretKeySpec(bytesKey, "AES"); } /** * 加密 * * @param content 待加密内容 * @param secretKey 加密使用的 AES 密钥 * @return 加密后的密文 byte[] */ public static byte[] encryptAES(byte[] content, SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(content); } /** * 解密 * * @param content 待解密内容 * @param secretKey 解密使用的 AES 密钥 * @return 解密后的明文 byte[] */ public static byte[] decryptAES(byte[] content, SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); return cipher.doFinal(content); } }