Search code examples
javaheaderzipaes

Java AES decrypts zip files with wrong header


I faced a strange issue when I am decrypting a zip stream.

After I decrypted the given stream I have exactly the same array of bytes apart from 8 bytes of header.

It should be 50 4B 03 04 (according to Zip spec) but I get different.

When I decrypt the same stream in c# the header is correct. I use RijndaelManaged, mode CBC, padding ZeroBytePadding, and block size = 128

In Java I use AES/CBC/NoPadding block size=128 to decrypt.

I believe the cause is padding but then I don't understand why the rest of bytes are correct.


Solution

  • In CBC mode, the padding mode should only affect the last bytes of your message, not the first ones (i.e. from ZeroBytePadding to NoPadding you might get some added 0 bytes at the end).

    If your first block is different (but the rest matches), the most probable cause is a different initialization vector in encryption and decryption. Check this.