android-async-http/android-async-http · error · IllegalArgumentException

bad base-64

Error message

bad base-64

What it means

Base64.decode throws IllegalArgumentException('bad base-64') when Decoder.process rejects the input, meaning the byte array or flags combination is not valid Base64 (illegal characters, bad padding, or data truncated mid-quantum). This is a port of Android's android.util.Base64 bundled so the library can decode Basic-auth and other values.

Solutions

  1. Validate the input matches ^[A-Za-z0-9+/=]*$ (or URL-safe alphabet with -_) before decoding
  2. Strip non-Base64 framing such as 'Basic ' prefix, PEM headers, or data: URIs
  3. Use NO_WRAP and matching flags for the encoding used (URL_SAFE with URL-safe input)
  4. Use CRLF/NO_PADDING flags consistent with how the data was encoded

Example fix

// before
byte[] decoded = Base64.decode(headerValue.getBytes(), Base64.DEFAULT); // 'Basic dXNlcjpwYXNz'
// after
String b64 = headerValue.replaceFirst("^Basic\\s+", "").trim();
byte[] decoded = Base64.decode(b64.getBytes(), Base64.NO_WRAP);
Defensive patterns

Strategy: validation

Validate before calling

String b64 = raw.replaceFirst("^Basic\\s+", "").trim();
if (!b64.matches("^[A-Za-z0-9+/=_-]*$")) { throw new IllegalArgumentException("not base64"); }
byte[] decoded = Base64.decode(b64.getBytes(), Base64.DEFAULT);

Type guard

boolean looksLikeBase64(byte[] in) {
    for (byte c : in) {
        if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '=' || c == '-' || c == '_')) return false;
    }
    return true;
}

Try / catch

try { decoded = Base64.decode(input, Base64.DEFAULT); } catch (IllegalArgumentException e) { /* log and treat input as malformed */ }

Prevention

When it happens

Trigger: Calling Base64.decode with a byte[] containing characters outside the Base64 alphabet, incorrect padding, or a length not consistent with the flags; decoding data that was not actually Base64-encoded; mixing URL_SAFE vs standard flags with mismatched input.

Common situations: Decoding a Basic auth header segment extracted incorrectly (includes 'Basic ' prefix); decoding server data that contains whitespace/newlines with the wrong flag combination; input that was hex- or percent-encoded rather than Base64.

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 android-async-http/android-async-http@018a0b8d96 (2026-09-09). Data as JSON: /api/errors/185f12a59d442031. Report an issue: GitHub.

Appendix: source

Thrown at library/src/main/java/com/loopj/android/http/Base64.java:121

     * Decode the Base64-encoded data in input and return the data in a new byte array.
     * <p>&nbsp;</p> <p>The padding '=' characters at the end are considered optional, but if any
     * are present, there must be the correct number of them.
     *
     * @param input  the data to decode
     * @param offset the position within the input array at which to start
     * @param len    the number of bytes of input to decode
     * @param flags  controls certain features of the decoded output. Pass {@code DEFAULT} to decode
     *               standard Base64.
     * @return decoded bytes for given offset and length
     * @throws IllegalArgumentException if the input contains incorrect padding
     */
    public static byte[] decode(byte[] input, int offset, int len, int flags) {
        // Allocate space for the most data the input could represent.
        // (It could contain less if it contains whitespace, etc.)
        Decoder decoder = new Decoder(flags, new byte[len * 3 / 4]);

        if (!decoder.process(input, offset, len, true)) {
            throw new IllegalArgumentException("bad base-64");
        }

        // Maybe we got lucky and allocated exactly enough output space.
        if (decoder.op == decoder.output.length) {
            return decoder.output;
        }

        // Need to shorten the array, so allocate a new one of the
        // right size and copy.
        byte[] temp = new byte[decoder.op];
        System.arraycopy(decoder.output, 0, temp, 0, decoder.op);
        return temp;
    }

    /**
     * Base64-encode the given data and return a newly allocated String with the result.
     *
     * @param input the data to encode

View on GitHub (pinned to 018a0b8d96)