Base64 Encoder / Decoder
Encode text to Base64 or decode Base64 strings back to plain text. Supports full Unicode including emoji, CJK characters, and special symbols. See character count and byte size for both input and output.
Enter text above to see the result
Base64 Encoding: How It Works and Why It Matters for Developers
Base64 is one of the most widely used binary-to-text encoding schemes in computing. It converts arbitrary binary data into a string of printable ASCII characters, making it safe to transmit through systems that only handle text. Despite being invented decades ago for email attachments, Base64 remains essential in modern web development, API design, and data interchange. Understanding how it works — and when to use it — is a foundational skill for developers working with any network protocol or data format.
How Base64 Encoding Works
Base64 encoding works by splitting the input data into groups of three bytes (24 bits). Each group of 24 bits is then divided into four 6-bit segments. Each 6-bit segment maps to one of 64 printable ASCII characters: A-Z (0-25), a-z (26-51), 0-9 (52-61), + (62), and / (63). This is the standard Base64 alphabet defined in RFC 4648.
When the input length is not a multiple of three bytes, padding is applied. If one byte remains after the last full group, two Base64 characters are produced followed by two = padding characters. If two bytes remain, three Base64 characters are produced followed by one = character. The padding ensures that the encoded output length is always a multiple of four, which simplifies decoding.
The result is a string that is approximately 33% larger than the original binary data. Three input bytes produce four output characters — a 4:3 ratio. This size overhead is the fundamental tradeoff of Base64: you gain compatibility with text-only transport channels at the cost of increased data size.
UTF-8 and Unicode Support
The original Base64 specification operates on bytes, not characters. When encoding text that contains non-ASCII characters — such as emoji, Chinese/Japanese/Korean characters, or accented letters — the text must first be converted to bytes using a character encoding. UTF-8 is the standard encoding for the modern web.
In JavaScript, the built-in btoa() function only handles Latin-1 characters (byte values 0-255). To encode Unicode text, you must first use TextEncoder to convert the string to UTF-8 bytes, then encode those bytes with btoa(). This tool handles that conversion automatically, so you can encode and decode any Unicode text including multi-byte characters without worrying about the underlying byte representation.
Common Use Cases in Web Development
Data URIs are one of the most visible uses of Base64 on the web. By encoding an image as a Base64 string and embedding it directly in CSS or HTML — for example, background-image: url(data:image/png;base64,...) — you eliminate an HTTP request at the cost of a larger document size. This technique is most beneficial for small images, icons, and SVGs where the HTTP overhead would exceed the Base64 size increase.
HTTP Basic Authentication encodes the username and password as a Base64 string in the Authorization header. It is important to understand that Base64 is not encryption — anyone can decode the string — so Basic Auth should only be used over HTTPS connections. JSON Web Tokens (JWTs) also use a URL-safe variant of Base64 to encode their header and payload sections.
Email attachments rely on Base64 through the MIME standard. Binary files — images, PDFs, executables — are encoded to Base64 before being embedded in the text-based email format. The receiving mail client decodes the Base64 back to the original binary data. This is why email attachments are often slightly larger than the original files.
Base64 Variants
The standard Base64 alphabet uses + and / as the 62nd and 63rd characters. These characters have special meaning in URLs (+ means space, / is a path separator), so a URL-safe variant replaces them with - and _ respectively. This variant, called base64url, is defined in RFC 4648 Section 5 and is used in JWTs, URL parameters, and filename-safe encodings.
Some implementations omit the padding characters (=) since the original data length can be inferred from the encoded length. This is common in base64url encoding, where trailing = characters would need to be percent-encoded in URLs. Whether padding is required depends on the specific protocol or library being used.
Performance Considerations
Base64 encoding and decoding are computationally inexpensive operations — they involve only bit shifting and table lookups. The primary cost is the 33% size increase, which affects bandwidth, storage, and memory usage. For small payloads (under a few kilobytes), this overhead is negligible. For large files, it becomes significant.
In web applications, embedding large images as Base64 data URIs can be counterproductive. A 100 KB image becomes approximately 133 KB in Base64, and it cannot be cached separately from the document it is embedded in. For images larger than about 2-5 KB, serving them as separate files with proper HTTP caching headers is usually more efficient. Build tools like webpack and Vite can automate this decision by inlining small assets and loading larger ones separately.
Security: Base64 Is Not Encryption
A persistent misconception is that Base64 provides some form of security or obfuscation. It does not. Base64 is a reversible encoding — anyone with access to the encoded string can decode it instantly without any key or password. Storing passwords, API keys, or sensitive data in Base64 provides zero protection.
If you need to protect data in transit, use TLS/HTTPS encryption. If you need to protect data at rest, use authenticated encryption algorithms like AES-GCM. Base64 is a data format tool, not a security tool. Treat it accordingly, and do not rely on it for any form of confidentiality.
Frequently Asked Questions
What is Base64 encoding?
Base64 is a binary-to-text encoding scheme that converts arbitrary data into a string of printable ASCII characters. It uses a 64-character alphabet (A-Z, a-z, 0-9, +, /) to represent binary data. Every 3 bytes of input produce 4 characters of output, resulting in approximately 33% size increase. It is used wherever binary data needs to be transmitted through text-only channels.
Is Base64 encryption?
No. Base64 is a reversible encoding, not encryption. Anyone can decode a Base64 string without any key or password. It provides no security or confidentiality. For protecting sensitive data, use proper encryption (such as AES or TLS). Base64 is a data format tool used for compatibility with text-based systems.
Why is Base64 output larger than the input?
Base64 encodes 3 bytes of input into 4 ASCII characters, creating a 4:3 ratio — approximately 33% larger. This overhead is the cost of representing arbitrary binary data using only 64 printable characters. Padding characters (=) may add additional bytes when the input length is not a multiple of 3.
Can Base64 encode Unicode and emoji?
Yes, but the text must first be converted to UTF-8 bytes before encoding. JavaScript's built-in btoa() only handles Latin-1 characters, so encoding Unicode text requires using TextEncoder to convert to UTF-8 first. This tool handles that conversion automatically, supporting any Unicode text including emoji, CJK characters, and special symbols.
What is the difference between Base64 and base64url?
Standard Base64 uses + and / as the 62nd and 63rd characters in its alphabet. base64url, defined in RFC 4648 Section 5, replaces these with - and _ to make the output safe for use in URLs and filenames. base64url is used in JSON Web Tokens (JWTs) and URL parameters. Some implementations also omit the padding (=) characters.
Related Calculators
AI Token Cost Calculator
Estimate API costs for GPT-4o, Claude, Gemini, and other LLMs based on token usage.
AI Token & Word Count Calculator
Convert between AI tokens, words, and characters with cost estimation.
API Rate Limit Calculator
Plan your API usage by calculating max throughput, operations per day, delay between requests, and burst capacity.