Skip to content
Crypto Write-up Advanced

Beware of Java's String.getBytes

Java's String.getBytes(UTF_8) silently replaces malformed UTF-8 sequences with '?', so two different strings can produce identical byte arrays. In Swiss Post's e-voting system this led to a hash collision, potentially allowing bypass of checks that rely on string equality.

Ruben Santamarta Follow

Published on reversemode.com

March 31, 2023

Ruben Santamarta (@reversemode), on reversemode.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Impact

This could lead to hash collisions, potentially allowing bypass of security checks that rely on string equality, though the text only demonstrates the collision in a PoC.

What the attacker needed

Attacker can control a string input; system uses String.getBytes for hashing or comparison

The fix

The bug was fixed in version 1.3.0 of the Swiss Post e-voting system.

How it was found

  1. Create a string containing a malformed UTF-8 sequence (e.g., a lone surrogate).
  2. Call stringToByteArray on it, which uses getBytes and replaces the bad sequence with '?'.
  3. Observe that the resulting byte array is identical to that of a valid string with '?' in that position.
  4. Compare the original strings with equals() and see they are different, but their hashes collide.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When Java converts text to bytes, it sometimes swaps bad characters for a question mark instead of stopping with an error. That means two different pieces of text can turn into the same bytes, which can confuse systems that rely on those bytes being unique. Here it caused a hash collision in an e-voting system.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More by Ruben Santamarta

View all

3 more disclosed reports, best first.

More from Swiss E-Voting

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.