This is the problem:
hexString.append(Integer.toHexString(0xFF & bytes[i]));
This will lose any leading 0s - in other words, any byte less than 16 will come out as a single hex digit instead of two.
There are plenty of fixes for this. For example:
- Manually append
0
if the value is between 0 and 15 (ick) - Use
String.format("%02x", bytes[i] & 0xff)
- Use a full "byte array to hex conversion" method in a utility library (there are loads around)