It seems like I'm having a thread safety issue with Cipher and / or PBEKeySpec.
- JDK: 1.8.0_102, 1.8.0_151 and 9.0.1 + 11
- PBKDF2 Algorithm: PBKDF2WithHmacSHA1
- Encryption Algorithm: AES / CFB / NoPadding
- Key Algorithms: AES
I know that these classes are not protected from the tread if we use the same instances, but this is not the case, I get a new instance on each decoder. But even this, sometimes decoding fails, there is no exception, just an unexpected decoded value.
I was able to reproduce the problem:
@Test public void shouldBeThreadSafe() { final byte[] encoded = { 27, 26, 18, 88, 84, -87, -40, -91, 70, -74, 87, -21, -124, -114, -44, -24, 7, -7, 104, -26, 45, 96, 119, 45, -74, 51 }; final String expected = "dummy data"; final Charset charset = StandardCharsets.UTF_8; final String salt = "e47312da-bc71-4bde-8183-5e25db6f0987"; final String passphrase = "dummy-passphrase";
Output:
Try
I do not understand how this code can fail, is there an error in the Cipher and / or PBEKeySpec classes? Or did I miss something in my test?
Any help would be greatly appreciated.
UPDATE
OpenJDK issue: https://bugs.openjdk.java.net/browse/JDK-8191177