fjordtools / Errors / Base64: invalid character

java.lang.IllegalArgumentException: Illegal base64 character 24

Base64 only uses letters, digits, plus, slash and trailing equals signs. Java's decoder names the first other character, as a hex code: 24 is a dollar sign. Typical culprits are a data: prefix, a quote, a space or line break, or the URL-safe characters - and _ given to a standard decoder. Strip them, then decode.

Fjordtools team · Seen in Java and Python 3 · Published · 1 min read

How browsers word it

Javajava.lang.IllegalArgumentException: Illegal base64 character 24
Python 3 (validate=True)binascii.Error: Only base64 data is allowed
Chrome and Node (atob)InvalidCharacterError: The string to be decoded is not correctly encoded.

Common causes

  1. A stray symbol

    A quote, a dollar sign, an asterisk or any other symbol is not part of the alphabet. Java reports its code in hex, so 24 is $ and 2a is *.

    Input

    SGVsbG8h$

    Base64 says

    Contains a character that is not Base64: "$"
  2. A data: URL prefix

    Browsers and APIs often hand you the whole data URL. Only the part after the comma is Base64.

    Input

    data:text/plain;base64,SGVsbG8=

    Base64 says

    Contains a character that is not Base64: ":"
  3. A non-ASCII character

    A letter such as é is never Base64. It usually means the text was altered on the way, for example by an editor or a mail client.

    Input

    SGVé

    Base64 says

    Contains a character that is not Base64: "é"
  4. A space or a line break

    Java's basic decoder and Python with validation refuse whitespace. This tool ignores it, so wrapped Base64 decodes.

    Input

    SGVs
    bG8=

    Base64 says

    Hello
  5. The URL-safe alphabet

    Tokens and URLs use - and _ in place of + and /. A standard decoder rejects them. This tool accepts both forms.

How to fix it

  1. Find the character the message names and remove it, or decode only the part after the comma of a data: URL.
  2. Take out spaces and line breaks, or use Java's getMimeDecoder, which skips them.
  3. For - and _, use the URL-safe decoder: Base64.getUrlDecoder in Java, urlsafe_b64decode in Python.
  4. Paste the text into the Base64 tool: it names the first character that does not belong.

Related errors

More error explainers are coming

See all errors.