Conversation
String.getBytes() and getBytes(charset) both encoded UTF-16LE regardless
of the requested charset. Java's default on Android is UTF-8, and
getBytes("UTF-8") is the common case in the obfuscated string/crypto code
this VM is used to analyse. Encoding UTF-16LE returned twice as many
bytes, so every downstream digest, cipher, or Base64 over the result was
wrong.
Honour the charset argument when present and default to UTF-8; keep the
surrogatepass handling. Example: "hi".getBytes("UTF-8") now yields
[0x68,0x69] instead of [0x68,0x00,0x69,0x00].
Co-Authored-By: Martin Monperrus (AI-assisted) <martin.monperrus+ai@gnieh.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
String.getBytes()/getBytes(charset)in_builtin_virtual_hooksencoded UTF-16LE unconditionally, ignoring the requested charset.Java's default charset on Android is UTF-8, and
getBytes("UTF-8")is the common case in the obfuscated string/crypto code this VM is used to analyse. Encoding UTF-16LE returned twice as many bytes, so every downstream digest, cipher, or Base64 over the result was wrong — e.g."hi".getBytes("UTF-8")returned[0x68,0x00,0x69,0x00]instead of[0x68,0x69].Fix: honour the charset argument when present (normalising e.g.
UTF_8→utf-8), default to UTF-8, fall back to UTF-8 on an unknown charset, and keep the existingsurrogatepasshandling. One hook, no API change.Found while using DaliVM to recover strings from a real APK; happy to adjust naming/comments.