🧰 ToolPilot

Base64 Encoder & Decoder

Encode text to Base64 or decode Base64 back to plain text. Supports full Unicode including emojis and non-Latin characters.

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that converts binary data into a string of printable ASCII characters. It uses a 64-character alphabet consisting of A-Z, a-z, 0-9, plus (+), and forward slash (/), with the equals sign (=) used for padding. The name "Base64" comes from this 64-character set.

The need for Base64 arose because many data transmission systems were designed to handle only text. Email protocols (SMTP), HTTP headers, and XML/JSON documents all operate on text. When you need to embed binary data (like images, files, or encrypted content) within these text-based systems, Base64 provides a safe encoding that preserves the data without corruption.

This tool encodes text to Base64 and decodes Base64 back to plain text, with full Unicode support. It uses UTF-8 encoding before Base64 conversion, which means it correctly handles emojis, Chinese characters, Arabic script, and any other non-ASCII text. All processing happens in your browser, so sensitive data like API keys, tokens, and credentials are never transmitted to a server.

How Base64 Encoding Works

Base64 encoding works by taking groups of 3 bytes (24 bits) from the input and splitting them into 4 groups of 6 bits each. Each 6-bit group maps to one of the 64 characters in the Base64 alphabet. Since 6 bits can represent values from 0 to 63, this maps perfectly to the 64-character set.

When the input length is not a multiple of 3, padding is added. If the last group has only 1 byte, two padding characters (==) are appended. If it has 2 bytes, one padding character (=) is appended. This ensures the output length is always a multiple of 4 characters. Some implementations use "base64url" encoding, which replaces + with - and / with _ to make the output safe for URLs and filenames.

Decoding is the reverse process: each Base64 character is converted back to its 6-bit value, the 6-bit groups are reassembled into 8-bit bytes, and padding characters are stripped. The result is the original binary data. For text content, this binary data is then interpreted as UTF-8 to recover the original string.

Common Use Cases

  • Data URIs in HTML and CSS — Embedding small images directly in HTML or CSS using data:image/png;base64,... eliminates extra HTTP requests. This is common for icons, logos, and small UI assets where the Base64 overhead is offset by reduced network roundtrips.
  • Email attachments (MIME) — The MIME standard uses Base64 to encode file attachments in emails. When you attach a PDF or image to an email, your email client Base64-encodes it so it can travel safely through SMTP servers that only handle 7-bit ASCII text.
  • API data transmission — JSON APIs sometimes require binary data (images, PDFs, certificates) to be included in request or response bodies. Since JSON is a text format that cannot contain raw binary data, Base64 encoding wraps the binary content in a JSON-safe string.
  • HTTP Basic Authentication — The HTTP Basic Auth scheme transmits credentials by Base64-encoding the string username:password and placing it in the Authorization header. This is encoding, not encryption, so it must always be used over HTTPS.
  • JWT tokens — JSON Web Tokens consist of three Base64url-encoded segments: header, payload, and signature. Decoding the first two segments reveals the token claims, which is useful for debugging authentication issues.
  • Certificate and key storage — PEM-format certificates and private keys use Base64 encoding between the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- markers. This makes binary cryptographic data safe to store in text files and transmit via email.
  • Source maps — JavaScript and CSS source maps can be embedded inline using Base64-encoded data URIs appended to the end of the file. This is common in development builds to avoid serving separate source map files.
  • Database storage — Some text-based databases or document stores use Base64 to store binary blobs alongside text data in a uniform format.

Tips and Best Practices

  • Base64 is not encryption. This is the most common misconception. Base64 is trivially reversible by anyone. Never use it to protect sensitive data. Use AES, RSA, or other proper encryption algorithms for security.
  • Account for the 33% size increase. Base64 encoding increases data size by approximately 33% (3 bytes become 4 characters). For large files, this overhead adds up. A 1 MB image becomes about 1.33 MB when Base64-encoded.
  • Use data URIs sparingly. Embedding large images as Base64 data URIs bloats your HTML/CSS files and prevents browser caching. The sweet spot is images under 10 KB. Larger assets should be served as separate files.
  • Decode JWT tokens for debugging. If you receive a JWT token and need to inspect its claims (expiration time, user ID, scopes), decode the second segment (payload) from Base64. You can use this tool for that purpose.
  • Handle Unicode correctly. When encoding text that contains non-ASCII characters, you must first convert to UTF-8 bytes before Base64-encoding. This tool handles that automatically, but manual implementations in code need to include the UTF-8 step to avoid data corruption.

Base64 vs Alternatives

Hex encoding (converting each byte to two hexadecimal characters) is simpler but less space-efficient: it increases data size by 100% compared to Base64's 33%. Hex is used where human readability of individual bytes matters (like color codes and hash values), while Base64 is preferred when size efficiency matters.

URL encoding (percent-encoding) replaces unsafe characters with percent-sign sequences. It is specifically designed for URLs and form data, not general binary-to-text encoding. For binary data in URLs, base64url encoding (a URL-safe variant of Base64) is the better choice.

For large file transfers, sending actual binary data with appropriate Content-Type headers is more efficient than Base64 encoding. Base64 is a workaround for text-only channels, not a replacement for proper binary transfer protocols. Use multipart/form-data for file uploads and direct binary responses for file downloads whenever the protocol supports it.

Frequently Asked Questions

Related Tools