public AesUtilHelper(int keySize, int iterationCount) {
this.keySize = keySize;
this.iterationCount = iterationCount;
try {
cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”);
}
catch (Exception e) {
e.printStackTrace();
throw fail(e);

}

}

public String encrypt(String salt, String iv, String passphrase, String plaintext) {

    if(plaintext != null && !plaintext.isEmpty()){
         try {
    SecretKey key = generateKey(salt, passphrase);
    byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));
    return base64(encrypted);

         }catch (Exception e) {
    e.printStackTrace();
}

}
return null;
}

public static void main(String[] args) {
AesUtilHelper aesUtilHelper = new AesUtilHelper(128, 1000);

String plaintext = "arjun kumar ";

String encryptS = aesUtilHelper.encrypt("4b7ee9834043984fb6529a3c145ab67e", 
        "d7815d9d2aabc59e8524c92c723a749b", "asdf", plaintext);

String decryptS = aesUtilHelper.decrypt("4b7ee9834043984fb6529a3c145ab67e", 
        "d7815d9d2aabc59e8524c92c723a749b", "asdf", encryptS);
System.out.println("plain Text = "+plaintext);
System.out.println("encrpted text = "+ encryptS);
System.out.println("decrypted String ="+decryptS);

}
public String decrypt(String salt, String iv, String passphrase, String ciphertext) {

    if(ciphertext != null && !ciphertext.isEmpty()){
        try {
    SecretKey key = generateKey(salt, passphrase);
    byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
    return new String(decrypted, "UTF-8");
      } catch (Exception e) {
          e.printStackTrace();
      }
  }        
  return null;

}

private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {
try {

    cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
    return cipher.doFinal(bytes);
}
catch (Exception e) {
    e.printStackTrace();
    throw fail(e);
}

}

private SecretKey generateKey(String salt, String passphrase) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance(“PBKDF2WithHmacSHA1”);
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), “AES”);
return key;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String random(int length) {
byte[] salt = new byte[length];
new SecureRandom().nextBytes(salt);
return hex(salt);
}

public static String base64(byte[] bytes) {
return DatatypeConverter.printBase64Binary(bytes);
}

public static byte[] base64(String str) {
return DatatypeConverter.parseBase64Binary(str);
}

public static String hex(byte[] bytes) {
return DatatypeConverter.printHexBinary(bytes);
}

public static byte[] hex(String str) {
return DatatypeConverter.parseHexBinary(str);
}

private IllegalStateException fail(Exception e) {
return new IllegalStateException(e);
}
}