Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to decrypt in Java after having encrypted via defuse/php-encryption #331

Closed
gillesvrstrtn opened this issue Mar 24, 2017 · 7 comments
Labels

Comments

@gillesvrstrtn
Copy link

Hi,
As explained in this stackoverflow post ; we stumbled upon the same issue while trying to decrypt a chain using built-in cryptographic functions in Java after having encrypted this chain at first using defuse/php-encryption.

The update of this post gave us a workaround which is to by-pass the prehash in sha256 step and thus directly pass secret to Core::pbkdf2 function.

As mentioned into inline comments, this prehash step appears to prevent a DoS attack documented in the GitHub issue #230 but somehow, this makes the decryption in a Java implementation impossible.

Many thanks though, cheers.

@defuse
Copy link
Owner

defuse commented Mar 25, 2017

I think the Java code in the stackoverflow post is wrong, because it tries to interpret a SHA256 hash as a UTF-8 string in the line String prehashString = new String(prehash, StandardCharsets.UTF_8).

@gillesvrstrtn
Copy link
Author

Maybe but we tried to implement in Java without doing this UTF-8 interpretation of the prehash you mentioned and it didn't give the expected result, meaning we can't decrypt the chain.

@defuse defuse added the support label Mar 25, 2017
@defuse
Copy link
Owner

defuse commented Mar 25, 2017

Googling around a bit, you might be able to get by parsing the hash as ISO-8859-1 instead of UTF-8, or even simply just create a char array from the byte array by looping and charArray[i] = byteArray[i] & 0xFF or something like that. What matters is that the low 8 bits of the chars have to be the same as the 8 bits of the bytes.

Since this library doesn't aim to be compatible with other implementation languages (libsodium is a good replacement for that use case), I'm going to close this ticket as "wontfix, it's Java's fault if its abstractions prevent you from computing the function on inputs that are valid according to the RFC."

@defuse defuse closed this as completed Mar 25, 2017
@gillesvrstrtn
Copy link
Author

Alright, I understand your point.

Thank you for your answers, I will try to parse using the ISO-8859-1 charset.

Regards,
GV.

@defuse
Copy link
Owner

defuse commented Mar 25, 2017

Thanks! Please let us know if it works :-)

@gillesvrstrtn
Copy link
Author

gillesvrstrtn commented Mar 25, 2017 via email

@gillesvrstrtn
Copy link
Author

gillesvrstrtn commented Mar 27, 2017

Unfortunately, this doesn't solve the problem ; the char array given to PBEKeySpec Java class is now identical to what we can find in php.

Like you suggest, we first have ensured that the low 8 bits of the chars are the same as the 8 bits of the bytes doing this : charArray[i] = byteArray[i] & 0xFF but still the generated key is incorrect.
So, as explained in the aforementioned StackOverflow post, as a workaround, we bypass the prehash and input secret directly to Core::pbkdf2 function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants