What is Base64? Encoding Explained with Examples

Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. It is not compression and it is not encryption — it is a way to safely pass binary content through systems that only handle text.

Why Base64 Exists

Many protocols and formats — email (SMTP), HTTP headers, JSON, and XML — were designed for text. Embedding raw binary data (an image, a certificate, a PDF) directly in a text medium causes problems: certain byte sequences conflict with control characters or special syntax.

Base64 solves this by converting every 3 bytes of binary data into 4 printable ASCII characters. The output contains only letters (A–Z, a–z), digits (0–9), and two special characters (+ and /).

How It Works

Base64 works by taking 3 bytes (24 bits) at a time and splitting them into four 6-bit groups. Each 6-bit group maps to one character in the Base64 alphabet (64 characters = 6 bits).

If the total input length is not a multiple of 3, padding characters (=) are added to make the output length a multiple of 4. This is why Base64 strings often end with = or ==.

URL-Safe Base64

Standard Base64 uses + and / which have special meaning in URLs. URL-safe Base64 (RFC 4648) replaces + with - (minus) and / with _ (underscore). Padding may be omitted. URL-safe Base64 is used in JWT tokens, OAuth parameters, and any context where the encoded value appears in a URL.

Base64 is Not Encryption

Base64 is an encoding, not a cipher. Anyone can decode a Base64 string without a key. Do not use Base64 to protect sensitive data — use proper encryption (AES, TLS) for that purpose. Base64 is used for transport compatibility, not security.

Common Uses of Base64

Base64 is used to embed images directly in HTML or CSS as data URIs (data:image/png;base64,...), to send binary attachments in email (MIME multipart), to encode API keys and credentials in HTTP Basic Auth headers, to store binary blobs in JSON APIs, and to encode cryptographic keys and certificates in PEM format.

Example

Encoding a Basic Auth credential

The username "alice" and password "secret" are combined as "alice:secret", then Base64-encoded to "YWxpY2U6c2VjcmV0". The HTTP request includes the header: Authorization: Basic YWxpY2U6c2VjcmV0

Practical notes

  • •Base64 increases data size by approximately 33% compared to the original binary.
  • •Padding (=) at the end can be safely stripped in URL-safe contexts and added back during decoding.

Try the tools

Frequently asked questions

Can I Base64-encode an entire file?

Yes, but the output will be 33% larger than the original. Text-based tools (including this one) handle text encoding. Encoding binary files (images, PDFs) requires reading the raw bytes — typically done in code rather than a text-input tool.

Is Base64 the same as hex encoding?

Both convert binary to printable characters, but differently. Hex uses 2 characters per byte (16² = 256 values) — it doubles the size. Base64 uses about 1.33 characters per byte — more efficient than hex for large data.