one of the behavior changes introduced in Android 9 is more strict UTF-8 decoder. If we have an array of bytes that are not correct UTF-8 string (for example random bytes or some binary data) and try to create a string out of it:
return new String(bytes)
the Android will pick UTF-8 as preferred encoding (which is fine) but return a slightly different result on Android 9 than on older versions of Android.
I know that transforming random bytes into UTF-8 String doesn't sound like a good idea in a first place, but I need a backward compatibility for now.
Is there an option to get exactly the same String result on all Android versions?
Edit:
Steps to reproduce:
byte[] bytes = new byte[]{25, 17, 113, 18, 62, 121, -6, -71, 45, -126, -113, 122, 58, 49, -30, -53, -66, -7, 0, -41};
char[] password = new String(bytes).toCharArray();
byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
Log.d("TEST", "Bytes: ".concat(Arrays.toString(passKey)));
Output for Android <9.0:
[0, 25, 0, 17, 0, 113, 0, 18, 0, 62, 0, 121, -1, -3, 0, 45, -1, -3, -1, -3, 0, 122, 0, 58, 0, 49, -1, -3, 2, -2, -1, -3, 0, 0, -1, -3, 0, 0]
Output for Android 9.0:
[0, 25, 0, 17, 0, 113, 0, 18, 0, 62, 0, 121, -1, -3, -1, -3, 0, 45, -1, -3, -1, -3, 0, 122, 0, 58, 0, 49, -1, -3, 2, -2, -1, -3, 0, 0, -1, -3, 0, 0]
You can use this code, which was ported from the UTF-8 decoder of previous Android versions.
private static final char REPLACEMENT_CHAR = (char) 0xfffd;
public static char[] byteArrayToCharArray(byte[] data) {
char[] value;
final int offset = 0;
final int byteCount = data.length;
char[] v = new char[byteCount];
int idx = offset;
int last = offset + byteCount;
int s = 0;
outer:
while (idx < last) {
byte b0 = data[idx++];
if ((b0 & 0x80) == 0) {
// 0xxxxxxx
// Range: U-00000000 - U-0000007F
int val = b0 & 0xff;
v[s++] = (char) val;
} else if (((b0 & 0xe0) == 0xc0) || ((b0 & 0xf0) == 0xe0) ||
((b0 & 0xf8) == 0xf0) || ((b0 & 0xfc) == 0xf8) || ((b0 & 0xfe) == 0xfc)) {
int utfCount = 1;
if ((b0 & 0xf0) == 0xe0) utfCount = 2;
else if ((b0 & 0xf8) == 0xf0) utfCount = 3;
else if ((b0 & 0xfc) == 0xf8) utfCount = 4;
else if ((b0 & 0xfe) == 0xfc) utfCount = 5;
// 110xxxxx (10xxxxxx)+
// Range: U-00000080 - U-000007FF (count == 1)
// Range: U-00000800 - U-0000FFFF (count == 2)
// Range: U-00010000 - U-001FFFFF (count == 3)
// Range: U-00200000 - U-03FFFFFF (count == 4)
// Range: U-04000000 - U-7FFFFFFF (count == 5)
if (idx + utfCount > last) {
v[s++] = REPLACEMENT_CHAR;
continue;
}
// Extract usable bits from b0
int val = b0 & (0x1f >> (utfCount - 1));
for (int i = 0; i < utfCount; ++i) {
byte b = data[idx++];
if ((b & 0xc0) != 0x80) {
v[s++] = REPLACEMENT_CHAR;
idx--; // Put the input char back
continue outer;
}
// Push new bits in from the right side
val <<= 6;
val |= b & 0x3f;
}
// Note: Java allows overlong char
// specifications To disallow, check that val
// is greater than or equal to the minimum
// value for each count:
//
// count min value
// ----- ----------
// 1 0x80
// 2 0x800
// 3 0x10000
// 4 0x200000
// 5 0x4000000
// Allow surrogate values (0xD800 - 0xDFFF) to
// be specified using 3-byte UTF values only
if ((utfCount != 2) && (val >= 0xD800) && (val <= 0xDFFF)) {
v[s++] = REPLACEMENT_CHAR;
continue;
}
// Reject chars greater than the Unicode maximum of U+10FFFF.
if (val > 0x10FFFF) {
v[s++] = REPLACEMENT_CHAR;
continue;
}
// Encode chars from U+10000 up as surrogate pairs
if (val < 0x10000) {
v[s++] = (char) val;
} else {
int x = val & 0xffff;
int u = (val >> 16) & 0x1f;
int w = (u - 1) & 0xffff;
int hi = 0xd800 | (w << 6) | (x >> 10);
int lo = 0xdc00 | (x & 0x3ff);
v[s++] = (char) hi;
v[s++] = (char) lo;
}
} else {
// Illegal values 0x8*, 0x9*, 0xa*, 0xb*, 0xfd-0xff
v[s++] = REPLACEMENT_CHAR;
}
}
if (s == byteCount) {
// We guessed right, so we can use our temporary array as-is.
value = v;
} else {
// Our temporary array was too big, so reallocate and copy.
value = new char[s];
System.arraycopy(v, 0, value, 0, s);
}
return value;
}
I've unit tested this by creating a random array of 20 bytes, and comparing to the original new String(bytes).toCharArray()
implementation on previous Android version, and then repeated this a million times. I did not see any differences on multiple older Android versions.
The original source code is from here: https://android.googlesource.com/platform/libcore/+/a7752f4d22097346dd7849b92b9f36d0a0a7a8f3/libdvm/src/main/java/java/lang/String.java#245
To simplify it, I removed the part which deals with non-UTF8 charsets, if you use new String()
, then UTF-8 is the default charset which is used, so you should be fine.
This code will get you backward compatibility as you wanted. But as other people will suggest you, I advise to look for a simpler solution if possible, which is not dependent on Android versions (or other components, which are out of your control)