TheAlgorithms/Java · error · IllegalArgumentException
Invalid Base64 character: {}
Error message
Invalid Base64 character: {} What it means
Thrown by the private Base64.getBase64Value(char) helper when a character is outside the standard Base64 alphabet (A-Z, a-z, 0-9, '+', '/'). This is RFC 4648 standard alphabet only; URL-safe characters ('-' and '_') are not accepted, and whitespace/newlines are not skipped.
Source
Thrown at src/main/java/com/thealgorithms/conversions/Base64.java:200
* Gets the numeric value of a Base64 character.
*
* @param c the Base64 character
* @return the numeric value (0-63)
* @throws IllegalArgumentException if character is not a valid Base64 character
*/
private static int getBase64Value(char c) {
if (c >= 'A' && c <= 'Z') {
return c - 'A';
} else if (c >= 'a' && c <= 'z') {
return c - 'a' + 26;
} else if (c >= '0' && c <= '9') {
return c - '0' + 52;
} else if (c == '+') {
return 62;
} else if (c == '/') {
return 63;
} else {
throw new IllegalArgumentException("Invalid Base64 character: " + c);
}
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- For URL-safe input, translate '-' -> '+' and '_' -> '/', or use java.util.Base64.getUrlDecoder().
- Strip all whitespace and newlines: input.replaceAll("\\s", "").
- Switch to java.util.Base64.getMimeDecoder() for MIME-wrapped input with line breaks.
Example fix
// before
byte[] out = Base64.decode("a-b_c"); // URL-safe chars rejected
// after
String std = input.replace('-', '+').replace('_', '/');
while (std.length() % 4 != 0) std += "=";
byte[] out = Base64.decode(std); Defensive patterns
Strategy: validation
Validate before calling
String s = input.replaceAll("\\s", "").replace('-', '+').replace('_', '/');
while (s.length() % 4 != 0) s += "=";
for (char c : s.toCharArray()) {
if (!(Character.isLetterOrDigit(c) || c == '+' || c == '/' || c == '=')) {
throw new IllegalArgumentException("invalid char: " + c);
}
}
byte[] out = Base64.decode(s); Type guard
static boolean isStandardBase64Alphabet(String s) {
for (char c : s.toCharArray()) {
if (!(Character.isLetterOrDigit(c) || c == '+' || c == '/' || c == '=')) return false;
}
return true;
} Try / catch
try {
byte[] out = Base64.decode(input);
} catch (IllegalArgumentException e) {
// try URL-safe or MIME decoder
out = java.util.Base64.getUrlDecoder().decode(input);
} Prevention
- Convert URL-safe chars ('-','_') to ('+','/') before decoding.
- Strip whitespace and newlines from wrapped (MIME) Base64.
- Use java.util.Base64.getMimeDecoder()/getUrlDecoder() for non-standard variants.
When it happens
Trigger: Passing URL-safe Base64 that uses '-' or '_' instead of '+' or '/'. Passing input with embedded whitespace, newlines, or carriage returns. Passing a character outside the 65-symbol alphabet (including padding in a data position).
Common situations: JWTs and URLs use base64url encoding which is incompatible. Base64 output wrapped at 76 columns (MIME) contains newlines. Copy-paste from sources that introduced stray characters.
Related errors
- Invalid Base64 input length; must be multiple of 4
- Padding '=' can only appear at the end (last 1 or 2 characte
- A padding '=' must not be followed by a non-padding characte
- invalid character:{}
- Input cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f985e20d9316b322.
Report an issue: GitHub.