I'm trying to encrypt a file using AES in Java, but it's not working. I put in some prints to see where it failed, the first two are printing but not the third. I checked that the key and IV are the right sizes, 256 bits and 128 bits respectively. If they are both valid I don't know what else could be going wrong. Are there any glaring mistakes in my code? I haven't done this before. Any help is greatly appreciated!
public static String encryptAES(byte[] data, byte[] key, byte[] iv) throws Exception {
Key k = new SecretKeySpec(key, "AES");
System.out.println("key set");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
System.out.println("cipher created");
c.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv));
System.out.println("cipher initialised");
byte[] encryptedDataBytes = c.doFinal(data);
String encryptedData = Base64.getEncoder().encodeToString(encryptedDataBytes);
return encryptedData;
}