Base64 Guides

These practical guides explain what Base64 does, when to use it, and how to troubleshoot real-world data. Examples use standard Base64 unless the guide says otherwise.

Related standalone tutorials

1. What Base64 is

Base64 is a binary-to-text encoding. It maps every group of three input bytes to four printable characters selected from A-Z, a-z, 0-9, plus (+), and slash (/). An equals sign (=) pads the final group when the original length is not divisible by three. Because each four-character group represents three bytes, Base64 usually increases the representation size by about one third. Encoding does not hide information: anyone who has the string can decode it.

The format is useful when a transport expects text but the data is binary. Email MIME parts, JSON fields, data URLs, configuration files, and API payloads are common examples. It is less suitable for large files because the size expansion adds bandwidth and memory overhead.

2. Encoding text correctly

Text must first be converted to bytes using a defined character set. UTF-8 is the safest default for modern applications because it represents Unicode consistently. The string café, for example, is not encoded from the visual characters directly; its UTF-8 bytes are encoded. If one system uses UTF-8 and another assumes Windows-1252 or a legacy Chinese encoding, decoding may produce replacement characters or unreadable text.

For a browser workflow, choose the source character set, paste the text, and inspect the result. Preserve line breaks deliberately: encoding the entire paragraph and encoding each line separately produce different strings. When exchanging data with an API, document the charset and whether newlines are normalized to LF or CRLF.

const text = "Hello, Base64!";
const encoded = btoa(unescape(encodeURIComponent(text)));
console.log(encoded); // SGVsbG8sIEJhc2U2NCE=
const decoded = decodeURIComponent(escape(atob(encoded)));
console.log(decoded);

3. Decoding text versus files

Decoded bytes are not always text. A PNG, PDF, ZIP archive, or certificate may decode successfully but display as meaningless characters in a text box. Use a file-oriented decoder when the input begins with a data URL such as data:application/pdf;base64,... or when you know the original content was binary. A correct decoder should preserve the bytes and let you save the reconstructed file.

Whitespace is often inserted into Base64 by email and documentation systems. Standard decoders can remove harmless line breaks, but accidental edits inside the alphabet can corrupt the data. Compare the decoded file's type signature and length when troubleshooting.

4. Base64URL and padding

URLs treat plus and slash specially, so Base64URL replaces + with - and / with _. Many Base64URL producers also omit trailing padding. A URL-safe decoder must restore the expected padding before converting the final bytes. Do not silently mix standard and URL-safe alphabets in the same value.

Base64URL is common in JSON Web Tokens and URL parameters. It is not encryption and it does not make a token trustworthy. Applications must still authenticate, authorize, validate length, and protect secrets with encryption or a secure token design.

5. Data URLs in HTML and CSS

A data URL embeds a media type and encoded payload in one string, for example data:image/png;base64,.... It can be convenient for a small icon, a test fixture, or a self-contained demo. Large data URLs make HTML and CSS harder to cache and maintain, so ordinary static assets are usually faster and easier to update.

When decoding a data URL, keep the metadata before the comma separate from the Base64 payload. The media type tells you how to interpret the resulting bytes; it does not prove that the bytes are valid. Validate file signatures before displaying untrusted content.

6. Base64 in APIs and JWTs

APIs often use Base64 for binary fields, but JSON itself is text and may not need Base64 for ordinary strings. JWTs use a Base64URL-encoded header and payload separated by periods. Those parts are readable encodings, not confidential storage. Never put passwords or private personal information in a JWT payload without an appropriate encrypted design.

When integrating an API, record whether padding is required, whether line breaks are allowed, the expected charset, and the maximum payload size. Test empty input, Unicode text, one- and two-byte values, and malformed characters.

import base64

value = "你好, Base64"
encoded = base64.b64encode(value.encode("utf-8")).decode("ascii")
decoded = base64.b64decode(encoded).decode("utf-8")
print(encoded)
print(decoded)

7. Security and privacy

Base64 provides obfuscation at most. It does not authenticate a message, prevent tampering, or protect a secret. Treat decoded content as untrusted input and apply normal validation before parsing, rendering, or executing it. A browser-based conversion tool can process ordinary values locally, but users should still avoid pasting credentials, access tokens, medical data, or other information into any website.

For confidential workflows, use a local command-line or offline library under your organization's controls. For transport security, use HTTPS and an encryption or authenticated-encryption scheme designed for the data.

8. Common errors and a repeatable checklist

“Invalid character” usually means a standard decoder received URL-safe characters or unrelated punctuation. “Incorrect padding” often means the string was truncated or its final equals signs were removed without a decoder that supports unpadded input. Garbled text usually indicates a charset mismatch, while a successful decode with an unreadable file preview may simply mean the result is binary.

When diagnosing a value, check it in this order: confirm the complete value was copied; identify standard versus URL-safe alphabet; remove only permitted whitespace; choose the original character set; compare the byte length; and inspect the expected file signature. Keep a small known-good test such as SGVsbG8= (the UTF-8 text “Hello”) to separate a tool problem from an input problem.

# Decode a text value on macOS or Linux
printf 'SGVsbG8=' | base64 --decode

# URL-safe decoding in Python
import base64
base64.urlsafe_b64decode("SGVsbG8")

Content reviewed: August 30, 2026