TheAlgorithms/Java · error · IllegalArgumentException
A padding '=' must not be followed by a non-padding characte
Error message
A padding '=' must not be followed by a non-padding character
What it means
Base64.decode rejects input where a '=' padding character is followed by a non-'=' character (e.g., "AB=CD"). RFC 4648 requires that once padding begins, all subsequent characters must also be padding. This catches strings where padding appears, then a data character follows, indicating corruption.
Source
Thrown at src/main/java/com/thealgorithms/conversions/Base64.java:128
if (input.isEmpty()) {
return new byte[0];
}
// Strict RFC 4648 compliance: length must be a multiple of 4
if (input.length() % 4 != 0) {
throw new IllegalArgumentException("Invalid Base64 input length; must be multiple of 4");
}
// Validate padding: '=' can only appear at the end (last 1 or 2 chars)
int firstPadding = input.indexOf('=');
if (firstPadding != -1) {
if (firstPadding < input.length() - 2) {
throw new IllegalArgumentException("Padding '=' can only appear at the end (last 1 or 2 characters)");
}
for (int i = firstPadding; i < input.length(); i++) {
if (input.charAt(i) != '=') {
throw new IllegalArgumentException("A padding '=' must not be followed by a non-padding character");
}
}
}
List<Byte> result = new ArrayList<>();
// Process input in groups of 4 characters
for (int i = 0; i < input.length(); i += 4) {
// Get up to 4 characters
int char1 = getBase64Value(input.charAt(i));
int char2 = getBase64Value(input.charAt(i + 1));
int char3 = input.charAt(i + 2) == '=' ? 0 : getBase64Value(input.charAt(i + 2));
int char4 = input.charAt(i + 3) == '=' ? 0 : getBase64Value(input.charAt(i + 3));
// Combine four 6-bit groups into a 24-bit number
int combined = (char1 << 18) | (char2 << 12) | (char3 << 6) | char4;
// Extract three 8-bit bytesView on GitHub (pinned to fdfb9a395b)
Solutions
- Strip all whitespace and newlines before decoding: input = input.replaceAll("\\s", "").
- Re-encode the source data as a single Base64 string instead of concatenating fragments.
- Validate with a regex like ^[A-Za-z0-9+/]*={0,2}$ before calling decode.
Example fix
// before String joined = frag1 + frag2; // frag1 ended with '=', frag2 starts with data byte[] out = Base64.decode(joined); // after byte[] raw = (decodeOrFetch(frag1) + decodeOrFetch(frag2)).getBytes(); String joined = Base64.encode(raw);
Defensive patterns
Strategy: validation
Validate before calling
String s = input.replaceAll("\\s", "");
int fp = s.indexOf('=');
if (fp != -1) {
for (int i = fp; i < s.length(); i++) {
if (s.charAt(i) != '=') throw new IllegalArgumentException("non-pad after pad");
}
}
byte[] out = Base64.decode(s); Type guard
static boolean paddingIsContiguousAtEnd(String s) {
s = s.replaceAll("\\s", "");
int fp = s.indexOf('=');
if (fp == -1) return true;
for (int i = fp; i < s.length(); i++) if (s.charAt(i) != '=') return false;
return true;
} Try / catch
try {
byte[] out = Base64.decode(input);
} catch (IllegalArgumentException e) {
throw new DomainException("Invalid Base64 padding: " + e.getMessage(), e);
} Prevention
- Strip whitespace/newlines that can appear between fragments.
- Re-encode concatenated fragments rather than joining raw strings.
- Pre-validate with a strict regex before decoding.
When it happens
Trigger: Input like "AB=C=" or "A=BC" where '=' is followed by a non-'=' character. Strings assembled from fragments where padding leaked into the middle. Base64 strings with trailing whitespace after padding that wasn't stripped.
Common situations: Concatenating two Base64 fragments without re-encoding. Log lines or wrapped text where '=' from one chunk precedes data from the next. Whitespace/newline characters appearing after padding.
Related errors
- Padding '=' can only appear at the end (last 1 or 2 characte
- Invalid Base64 input length; must be multiple of 4
- Input cannot be null
- Invalid Base64 character: {}
- Slope and intercept must be valid numbers.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/641c10639edf37dc.
Report an issue: GitHub.