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

  1. Strip all whitespace/newlines from the string before decoding: s.replaceAll("\\s","")
  2. If the data is URL-safe Base64, replace '-' with '+' and '_' with '/' first
  3. Verify the alphabet: this decoder supports a standard and an alternate table; ensure the encoding matches the one selected
  4. 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

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


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)