0

I have been given an encrypted file along with a base64 symmetric key and base 64 IV and have been asked to decrypt it using Java. The encryption used on the data file was AES. However, when I run the encrypted file, symmetric key and IV into my code, I get the following error:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher 
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:913)

Here is my code:

String encryptedData = "C:\\EncryptedDataFile.data";
FileInputStream fis = null;
File file = new File(encryptedData);

//Convert file into array of bytes
byte[] encryptedDataBytes = new byte[(int) file.length()];

try 
{           
    // Read in array of bytes
    fis = new FileInputStream(file);
    fis.read(encryptedDataBytes);
    fis.close();     
} 
catch (FileNotFoundException ex) 
{
    ex.printStackTrace();
} 
catch (IOException ex) 
{
    ex.printStackTrace();
} 

// AES Key
byte[] decodedKey = Base64.getDecoder().decode("50rofsdb0TnQAQCb702wKz8m6XQeLNj6lamEvivKsh8=");

// decode the base64 encoded string
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); // rebuild key using SecretKeySpec

// IV
byte[] initVecBytes = Base64.getDecoder().decode("OUXLZq4SpyhzNGIei0nerA==");

// Decrypt the cipher text        
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
IvParameterSpec ivParameterSpec = new IvParameterSpec(initVecBytes);
cipher.init(Cipher.DECRYPT_MODE, originalKey, ivParameterSpec);
byte[] original = cipher.doFinal(encryptedDataBytes);
String s = new String(original);
System.out.println(s);

If someone could provide some help to me on this matter it would be much appreciated

  • 1
    Your code looks OK at a glance. The problem is with the input data. Send it back where it came from with the error message. – user207421 Mar 07 '16 at 19:37
  • you sure you want to post your keys to the world? – jtahlborn Mar 07 '16 at 19:44
  • are you sure you are using the same cipher which was used to encrypt the data? Also, you need to ensure that you read the file contents completely. – jtahlborn Mar 07 '16 at 19:45
  • Is the encrypted data a multiple of the block size (16-bytes) – zaph Mar 07 '16 at 19:48
  • @jtahlborn - I changed the keys before I posted. Also, I was only told it was AES encryption therefore assumed that was the cipher used - I will try another. Lastly, would the code I provided not read in the full file contents completely? – Luke_Skywalker007 Mar 07 '16 at 19:53
  • 1
    no, you should read up on how to handle streams correctly. the read method is free to return fewer bytes than you ask for (there is a return param to deal with that). – jtahlborn Mar 07 '16 at 20:18
  • Ah ok jtahlborn, ill take a look at reading in the data in another way and take it from there. Thanks again – Luke_Skywalker007 Mar 07 '16 at 20:53
  • Could you provide the ciphertext from the file `EncryptedDataFile.data` in base 64 as well? Do you know that technically you should not use a single read as it may return fewer bytes? Who has told you that CBC mode was used? – Maarten Bodewes Mar 08 '16 at 23:34
  • Hi folks, just a quick update to inform you all that I managed to get the file decrypted. I took all your comments on board and made the following changes to my code: – Luke_Skywalker007 Mar 09 '16 at 15:49
  • I made the following changes to my code: * Changed the cipher to 'AES/CBC/NoPadding' * Used the following method to read in the data: Files.readAllBytes(path) * Checked the length of the cipher text and noticed that there seemed to be an added byte at the end of the data. When I removed it the length was then divisible by 16. – Luke_Skywalker007 Mar 09 '16 at 16:02
  • Just to add, I would add my updated code but it seems the number of characters I use doesnt permit me to do so - therefore if someone could advise I will add the new code. Thanks – Luke_Skywalker007 Mar 09 '16 at 16:04

0 Answers0