MyCATApache/Mycat-Server · error · IllegalArgumentException
Illegal character
Error message
Illegal character {c} What it means
base64toInt performs a table lookup (alphaToInt[c]) mapping each Base64 character to its 6-bit value; unmapped characters map to negative sentinels. If a character is not in the expected Base64 alphabet, it throws IllegalArgumentException('Illegal character <c>').
Solutions
- Strip all whitespace/newlines from the string before decoding: s.replaceAll("\\s","")
- If the data is URL-safe Base64, replace '-' with '+' and '_' with '/' first
- Verify the alphabet: this decoder supports a standard and an alternate table; ensure the encoding matches the one selected
- Re-encode the original bytes with a standard Base64 encoder if the input source is untrusted
Example fix
// before
decode(rawBase64FromFile); // contains trailing \n
// after
decode(rawBase64FromFile.replaceAll("\\s", "")); Defensive patterns
Strategy: validation
Validate before calling
static String sanitizeBase64(String s){
if (s == null) return null;
return s.replaceAll("\\s","").replace('-','+').replace('_','/');
} Try / catch
try { return decode(sanitized); }
catch (IllegalArgumentException e) { throw new IllegalArgumentException("non-Base64 character in input at decode", e); } Prevention
- Strip all whitespace/newlines (MIME wrapping) before decoding
- Convert URL-safe Base64 alphabet characters to the standard alphabet
- Ensure the string is read with the right charset so no non-ASCII chars slip in
- Validate with a regex over the standard alphabet before calling the decoder
When it happens
Trigger: Decoding a string containing characters outside the standard (or alternate) Base64 alphabet — whitespace, newlines, '\r', '\n', '-', '_', '!', or non-ASCII characters — passed into the decoder reached via decrypt flows.
Common situations: MIME-wrapped Base64 with embedded line breaks; URL-safe Base64 ('-','_') fed to the standard-alphabet decoder; trailing newline from file reads; corrupted ciphertext.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- String length must be a multiple of four.
- Failed to get public key
- host ,user passwrod need to decrype ,but decrype password…
- A batchSize of: with batchStatsAt of: will overrun the…
- Array has wrong size:
AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11).
Data as JSON: /api/errors/0d139d9bfe415612.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/io/mycat/util/DecryptUtil.java:387
int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
}
}
// assert inCursor == s.length()-missingBytesInLastGroup;
// assert outCursor == result.length;
return result;
}
/**
* Translates the specified character, which is assumed to be in the "Base 64 Alphabet" into its equivalent 6-bit
* positive integer.
*
* @throw IllegalArgumentException or ArrayOutOfBoundsException if c is not in the Base64 Alphabet.
*/
private static int base64toInt(char c, byte[] alphaToInt) {
int result = alphaToInt[c];
if (result < 0) {
throw new IllegalArgumentException("Illegal character " + c);
}
return result;
}
/**
* This array is a lookup table that translates unicode characters drawn from the "Base64 Alphabet" (as specified in
* Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in the Base64
* alphabet but fall within the bounds of the array are translated to -1.
*/
private static final byte base64ToInt[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62,
-1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
/**
* This array is the analogue of base64ToInt, but for the nonstandard variant that avoids the use of uppercase
* alphabetic characters.View on GitHub (pinned to 65f8d8beb7)