TheAlgorithms/Java · error · IllegalArgumentException

Invalid Base64 input length; must be multiple of 4

Error message

Invalid Base64 input length; must be multiple of 4

What it means

Base64.decode enforces RFC 4648 strict compliance: the input length must be a multiple of 4 characters. Standard Base64 encodes every 3 bytes into 4 characters and pads with '=' to reach a multiple of 4, so any other length indicates corruption, truncation, or non-standard encoding (e.g., URL-safe without padding).

Source

Thrown at src/main/java/com/thealgorithms/conversions/Base64.java:117

    /**
     * Decodes the given Base64 encoded string to a byte array.
     *
     * @param input the Base64 encoded string to decode
     * @return the decoded byte array
     * @throws IllegalArgumentException if input is null or contains invalid Base64 characters
     */
    public static byte[] decode(String input) {
        if (input == null) {
            throw new IllegalArgumentException("Input cannot be null");
        }

        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

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Restore padding: while (input.length() % 4 != 0) input += "=";
  2. Switch to java.util.Base64.getDecoder() (lenient) or Base64.getUrlDecoder() for URL-safe input.
  3. Strip whitespace/newlines before decoding: input.replaceAll("\\s", "").

Example fix

// before
byte[] out = Base64.decode("SGVsbG8"); // length 7, not multiple of 4

// after
String s = "SGVsbG8";
while (s.length() % 4 != 0) s += "=";
byte[] out = Base64.decode(s);
Defensive patterns

Strategy: validation

Validate before calling

String s = input == null ? "" : input.replaceAll("\\s", "");
while (s.length() % 4 != 0) s += "=";
byte[] out = Base64.decode(s);

Type guard

static boolean isValidBase64Length(String s) {
    s = s.replaceAll("\\s", "");
    return s.length() % 4 == 0;
}

Try / catch

try {
    byte[] out = Base64.decode(input);
} catch (IllegalArgumentException e) {
    // try re-padding or switch to java.util.Base64
    out = java.util.Base64.getDecoder().decode(input);
}

Prevention

When it happens

Trigger: Passing a Base64 string that was truncated by a transport that strips '=' padding. Passing URL-safe Base64 (no padding). Passing a string with whitespace or newlines that shifted the length. Manually constructed strings of the wrong length.

Common situations: JWT or data URIs where padding was stripped. Copy-paste truncation. Logs that wrapped lines. Bases64 variants (base64url) that omit padding.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/51e9dc1e7c27551b. Report an issue: GitHub.