Here we are going to see how to do encrypt and decrypt the data in java. Encrypt data using AES Cipher (CBC) with 128 bit key

EncDecr.java

public class EncDecr {

    private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING";
    private static int CIPHER_KEY_LEN = 16; //128 bits

    
    public static String encrypt(String keyStr, String ivStr, String data) {
        try {
            if (keyStr.length() < EncDecr.CIPHER_KEY_LEN) {
                int numPad = EncDecr.CIPHER_KEY_LEN - key.length();
                
                for(int i = 0; i < numPad; i++){
                    keyStr += "0"; //0 pad to len 16 bytes
                }
                
            } else if (keyStr.length() > EncDecr.CIPHER_KEY_LEN) {
                keyStr = keyStr.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
            }
            
            
            IvParameterSpec initVector = new IvParameterSpec(ivStr.getBytes("ISO-8859-1"));
            SecretKeySpec skeySpec = new SecretKeySpec(keyStr.getBytes("ISO-8859-1"), "AES");

            Cipher cipher = Cipher.getInstance(EncDecr.CIPHER_NAME);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initVector);

            byte[] encryptedData = cipher.doFinal((data.getBytes()));
            
            String base64_EncrData = Base64.getEncoder().encodeToString(encryptedData);
            String base64_IV_data = Base64.getEncoder().encodeToString(iv.getBytes("ISO-8859-1"));
            
            return base64_EncrData + ":" + base64_IV_data;
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

   
    public static String decrypt(String key, String data) {
        try {
            if (key.length() < EncDecr.CIPHER_KEY_LEN) {
                int numPad = EncDecr.CIPHER_KEY_LEN - key.length();
                
                for(int i = 0; i < numPad; i++){
                    key += "0"; //0 pad to len 16 bytes
                }
                
            } else if (key.length() > EncDecr.CIPHER_KEY_LEN) {
                key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
            }
            
            String[] parts = data.split(":");
            
            IvParameterSpec iv = new IvParameterSpec(Base64.getDecoder().decode(parts[1]));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("ISO-8859-1"), "AES");

            Cipher cipher = Cipher.getInstance(EncDecr.CIPHER_NAME);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] decodedEncryptedData = Base64.getDecoder().decode(parts[0]);

            byte[] original = cipher.doFinal(decodedEncryptedData);

            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

}

in the above the arguments are
key - key to use should be 16 bytes long (128 bits)
iv - initialization vector
data - data to encrypt

How to call these Encrypt/Decrypt meethods, Lets see

JavaAES.java

public class DemoDriver {

    public static void main(String[] args) {

        String keyStr = "0123456789abcdef"; // 128 bit key
        String initVector = "fedcba9876543210"; // 16 bytes IV

        System.out.println(decrypt(keyStr,
                encrypt(keyStr, initVector, "Hi")));
        
         System.out.println(decrypt(keyStr, "Kx+c85B7tWjre4j6cdXHJQ==:ZmVkY2JhOTg3NjU0MzIxMA==")); 
    }
}

Here key should be our encrypt key

initVector should be 16 bytes length