CryptoUtil { private static byte[] aes(byte[] input, byte[] key, int mode) { try { SecretKey secretKey = new SecretKeySpec(key, AES_ALG); Cipher cipher = Cipher.getInstance(AES_ALG); cipher.init(mode, secretKey); return cipher.doFinal(input); } catch (GeneralSecurityException e) { throw ExceptionUtil.unchecked(e); } }  static byte[] hmacSha1(byte[] input, byte[] key); static boolean isMacValid(byte[] expected, byte[] input, byte[] key); static byte[] generateHmacSha1Key(); static byte[] aesEncrypt(byte[] input, byte[] key); static byte[] aesEncrypt(byte[] input, byte[] key, byte[] iv); static String aesDecrypt(byte[] input, byte[] key); static String aesDecrypt(byte[] input, byte[] key, byte[] iv); static byte[] generateAesKey(); static byte[] generateAesKey(int keysize); static byte[] generateIV();  }
@Test public void aes() { byte[] key = CryptoUtil.generateAesKey(); assertThat(key).hasSize(16); String input = "foo message"; byte[] encryptResult = CryptoUtil.aesEncrypt(input.getBytes(), key); String descryptResult = CryptoUtil.aesDecrypt(encryptResult, key); System.out.println("aes key in hex :" + EncodeUtil.encodeHex(key)); System.out.println("aes encrypt in hex result :" + EncodeUtil.encodeHex(encryptResult)); assertThat(descryptResult).isEqualTo(input); }