01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class AESUtils { private static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding" ; // 获取 cipher private static Cipher getCipher( byte [] key, int model) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES" ); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); cipher.init(model, secretKeySpec); return cipher; } // AES加密 public static String encrypt( byte [] data, byte [] key) throws Exception { Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE); return Base64.getEncoder().encodeToString(cipher.doFinal(data)); } // AES解密 public static byte [] decrypt( byte [] data, byte [] key) throws Exception { Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE); return cipher.doFinal(Base64.getDecoder().decode(data)); } } |
这个工具类比较简单,加密后的数据可能不具备可读性,因此一般需要对加密后的数据再使用 Base64 算法进行编码,获取可读字符串。换言之,上面的 AES 加密方法的返回值是一个 Base64 编码之后的字符串,AES 解密方法的参数也是一个 Base64 编码之后的字符串,先对该字符串进行解码,然后再解密。