@Test public void aesEncrypt() throws Exception { String src = "123456"; String key = "abcdefghabcdefghabcdefghabcdefgh"; byte[] cipher = RmCryptoHelper.aesEncrypt(src.getBytes(), key.getBytes()); byte[] plain = RmCryptoHelper.aesDecrypt(cipher, key.getBytes()); System.out.println("src=" + src + ", after encrypt and edcrypt, plain=" + new String(plain)); assertArrayEquals(src.getBytes(), plain); }
public static byte[] aesEncrypt(byte[] src, byte[] key) throws Exception { Key keyAES = new SecretKeySpec(key, AES); if (key.length != 32) { throw new IllegalArgumentException("key.length should is 32, but is" + key.length); } Cipher cipher = Cipher.getInstance(AES_MODE); cipher.init(Cipher.ENCRYPT_MODE, keyAES); byte[] iv = cipher.getIV(); assert iv.length == 12; byte[] cipherText = cipher.doFinal(src); assert cipherText.length == src.length + 16; byte[] message = new byte[12 + src.length + 16]; System.arraycopy(iv, 0, message, 0, 12); System.arraycopy(cipherText, 0, message, 12, cipherText.length); return message; }
RmCryptoHelper { public static byte[] aesEncrypt(byte[] src, byte[] key) throws Exception { Key keyAES = new SecretKeySpec(key, AES); if (key.length != 32) { throw new IllegalArgumentException("key.length should is 32, but is" + key.length); } Cipher cipher = Cipher.getInstance(AES_MODE); cipher.init(Cipher.ENCRYPT_MODE, keyAES); byte[] iv = cipher.getIV(); assert iv.length == 12; byte[] cipherText = cipher.doFinal(src); assert cipherText.length == src.length + 16; byte[] message = new byte[12 + src.length + 16]; System.arraycopy(iv, 0, message, 0, 12); System.arraycopy(cipherText, 0, message, 12, cipherText.length); return message; } }
RmCryptoHelper { public static byte[] aesEncrypt(byte[] src, byte[] key) throws Exception { Key keyAES = new SecretKeySpec(key, AES); if (key.length != 32) { throw new IllegalArgumentException("key.length should is 32, but is" + key.length); } Cipher cipher = Cipher.getInstance(AES_MODE); cipher.init(Cipher.ENCRYPT_MODE, keyAES); byte[] iv = cipher.getIV(); assert iv.length == 12; byte[] cipherText = cipher.doFinal(src); assert cipherText.length == src.length + 16; byte[] message = new byte[12 + src.length + 16]; System.arraycopy(iv, 0, message, 0, 12); System.arraycopy(cipherText, 0, message, 12, cipherText.length); return message; }  }
RmCryptoHelper { public static byte[] aesEncrypt(byte[] src, byte[] key) throws Exception { Key keyAES = new SecretKeySpec(key, AES); if (key.length != 32) { throw new IllegalArgumentException("key.length should is 32, but is" + key.length); } Cipher cipher = Cipher.getInstance(AES_MODE); cipher.init(Cipher.ENCRYPT_MODE, keyAES); byte[] iv = cipher.getIV(); assert iv.length == 12; byte[] cipherText = cipher.doFinal(src); assert cipherText.length == src.length + 16; byte[] message = new byte[12 + src.length + 16]; System.arraycopy(iv, 0, message, 0, 12); System.arraycopy(cipherText, 0, message, 12, cipherText.length); return message; }  static byte[] aesEncrypt(byte[] src, byte[] key); static byte[] aesDecrypt(byte[] src, byte[] key); static String digestPassword(String password, String seed); static String encryptAesBase64(String value); static String decryptAesBase64(String value); static String encodeBase64(String value); static String decodeBase64(String value); }
RmCryptoHelper { public static byte[] aesEncrypt(byte[] src, byte[] key) throws Exception { Key keyAES = new SecretKeySpec(key, AES); if (key.length != 32) { throw new IllegalArgumentException("key.length should is 32, but is" + key.length); } Cipher cipher = Cipher.getInstance(AES_MODE); cipher.init(Cipher.ENCRYPT_MODE, keyAES); byte[] iv = cipher.getIV(); assert iv.length == 12; byte[] cipherText = cipher.doFinal(src); assert cipherText.length == src.length + 16; byte[] message = new byte[12 + src.length + 16]; System.arraycopy(iv, 0, message, 0, 12); System.arraycopy(cipherText, 0, message, 12, cipherText.length); return message; }  static byte[] aesEncrypt(byte[] src, byte[] key); static byte[] aesDecrypt(byte[] src, byte[] key); static String digestPassword(String password, String seed); static String encryptAesBase64(String value); static String decryptAesBase64(String value); static String encodeBase64(String value); static String decodeBase64(String value);  }