Search code examples
androidencryptionrijndael

Decrypting an AES256-rijdeal encrypted file with initialization vector


I've got a tricky situation at the moment.

I need to create an android method that will decrypt an encrypted file that has been encrypted like this:

  • AES256 (Rijndael)
  • Cipher-Block-Chaining (CBC) using an initialization vector of 16 bytes that look like:

{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 122, 100 }

I also know the encryption key to the file, but for now let's say it's: "Boat"

I've been searching around on Google but I could not find a online that used this combination of encryption. Some involved the Rijndael algorithm but didn't let me choose an initialization vector, while others did not support AES256 at all.

Can someone point me into the right direction where to find an example or post some sample code that does the above?

N.B. I don't know if it's important to mention but the output file is always .pdf


Solution

  • This is a very standard configuration, hard to believe you didn't find any samples. All you need to do is create an IvParameterSpec from your IV bytes and initialize the Cipher with it. Something like this:

    SecretKey key = getEncryptionKey(); 
    byte[] iv = new byte[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, (byte)144, (byte)233, 122, 100 };
    byte[] cipherBytes = readEncryptedFile();
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
    byte[] plaintext = cipher.doFinal(cipherBytes);
    

    BTW, 'Rijndael' is the same as AES, so you'd get better results if you just search for 'AES'.