Java 7 -> Java 8: AES Causes exception: “BadPaddingException: Given final block not properly padded” in conjunction with BufferedReader & ZipStreams

核能气质少年 提交于 2019-12-04 02:40:17

Java 1.8 CipherInputStream throws a BadPaddingException if you don't completely consume the stream. This may be the case when using ZipInputStream, since consuming a zip in streaming fashion doesn't need to read the zip index at the end of the file.

I recommend wrapping the CipherInputStream in a facade implementation of InputStream that ignores BadPaddingException when delegating the close() method. Don't do this if authentication of the contents of the stream is required for your use case, of course, or if some kind of timing oracle attack is possible.

Looks like you have hit https://bugs.openjdk.java.net/browse/JDK-8061619. There was a good reason for the change in behaviour (see http://blog.philippheckel.com/2014/03/01/cipherinputstream-for-aead-modes-is-broken-in-jdk7-gcm/)

You should explicitly specify the padding in your cipher instance (e.g. AES/GCM/NoPadding, but check the suitability for your application. Incorrect padding is behind a number of attacks on SSL)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!