DevFormatter

Base64

 

Base64 Encoder & Decoder is a free browser-based tool that converts any text or binary data into Base64 format and decodes Base64 strings back to plain text. It supports standard Base64, URL-safe Base64 (used in JWTs and OAuth tokens), and Unicode input including emoji — with no data ever sent to a server.

Base64 Encoder & Decoder

How it works

Last updated: November 2026

Base64 is a binary-to-text encoding scheme that represents binary data using 64 printable ASCII characters (A–Z, a–z, 0–9, +, /). It was designed to safely transmit binary data through systems that only handle text, such as email, JSON APIs, and HTML attributes.

Paste any text into the left panel and click Encode to convert it to Base64. Paste a Base64 string and click Decode to convert it back. Unicode characters and emoji are fully supported — the tool converts text to UTF-8 bytes before encoding, avoiding the common btoa() Unicode bug.

All processing runs in your browser using the built-in btoa(), atob(), TextEncoder, and TextDecoder APIs. Nothing is sent to any server.

Base64 Quick Reference

Variant Alphabet Use case
Standard Base64 A–Z a–z 0–9 + / = MIME email, data URIs, general use
URL-safe Base64 A–Z a–z 0–9 - _ (no =) JWTs, OAuth tokens, URL query params
MIME Base64 Standard + line breaks every 76 chars Email attachments (RFC 2045)

Size overhead

Every 3 input bytes produce 4 Base64 characters, so Base64 output is ~33% larger than the original. A 1 MB file becomes ~1.37 MB encoded.

Padding characters

If the input length is not divisible by 3, one or two = padding characters are appended. One = means the last group was 2 bytes; == means 1 byte.

Tips & Tricks

btoa() breaks on Unicode — use TextEncoder instead

The browser's btoa() throws a InvalidCharacterError for any character above U+00FF. The correct fix is btoa(String.fromCharCode(...new TextEncoder().encode(str))) — or just use Node.js Buffer.from(str).toString('base64').

JWTs use URL-safe Base64 without padding

JWT header and payload sections use Base64URL (- and _ instead of + and /), and omit the = padding. Before decoding, replace - with +, _ with /, and add missing padding: str.length % 4 tells you how many = to add.

Data URIs bloat HTML — use them sparingly

Embedding images as data:image/png;base64,... prevents the browser from caching the image separately and makes the HTML 33% larger. Prefer external image URLs except for tiny icons (<1 KB) or email templates where external URLs are blocked.

HTTP Basic Auth is just Base64, not encryption

The Authorization: Basic header contains base64(username:password). Anyone who intercepts the header can decode it instantly. Basic Auth is only safe over HTTPS — never over plain HTTP.

Whitespace in Base64 strings causes decode failures

MIME Base64 inserts a line break every 76 characters (RFC 2045). Many decoders reject these line breaks. Strip all whitespace with str.replace(/\s/g, '') before decoding if you copied from an email or multipart body.

Check if a string is Base64 before decoding

A valid standard Base64 string matches /^[A-Za-z0-9+/]*=2$/ and its length is a multiple of 4. For URL-safe Base64 use /^[A-Za-z0-9_-]*$/. Testing the pattern first prevents throwing on invalid input.

Code Examples

Node.js

const { Buffer } = require('buffer');

// Encode
const encoded = Buffer.from('Hello, World!').toString('base64');
console.log(encoded); // SGVsbG8sIFdvcmxkIQ==

// Decode
const decoded = Buffer.from('SGVsbG8sIFdvcmxkIQ==', 'base64').toString('utf8');
console.log(decoded); // Hello, World!

// URL-safe Base64 (no +, /, = chars)
const urlSafe = Buffer.from('Hello, World!').toString('base64url');
console.log(urlSafe); // SGVsbG8sIFdvcmxkIQ

Python

import base64

# Encode
text = "Hello, World!"
encoded = base64.b64encode(text.encode('utf-8')).decode('ascii')
print(encoded)  # SGVsbG8sIFdvcmxkIQ==

# Decode
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==').decode('utf-8')
print(decoded)  # Hello, World!

# URL-safe variant (uses - and _ instead of + and /)
url_safe = base64.urlsafe_b64encode(text.encode()).decode()

CLI (macOS / Linux)

# Encode (macOS / Linux)
echo -n "Hello, World!" | base64
# SGVsbG8sIFdvcmxkIQ==

# Decode
echo "SGVsbG8sIFdvcmxkIQ==" | base64 --decode
# Hello, World!

# Encode a file
base64 image.png > image.b64

# Decode a file
base64 --decode image.b64 > image.png

Frequently Asked Questions

What is Base64 encoding?
Base64 is a binary-to-text encoding scheme that represents binary data as a string of 64 printable ASCII characters: uppercase A–Z (26), lowercase a–z (26), digits 0–9 (10), plus (+), and forward slash (/), with the equals sign (=) used for padding. It was designed to safely transmit binary data through text-only channels such as email (MIME) and JSON APIs. The name comes directly from the 64-character alphabet it uses. Base64 encodes every 3 bytes of input as exactly 4 ASCII characters, increasing the data size by approximately 33%. The standard is defined in RFC 4648 and is used by virtually every programming language and platform. Common applications include MIME email attachments, HTTP Basic Authentication headers, embedding images as data URIs in HTML and CSS, and encoding the header and payload sections of JSON Web Tokens (JWTs).
What is Base64 used for?
Base64 is used to embed images in HTML or CSS as data URIs (data:image/png;base64,...), encode binary email attachments (MIME), transmit binary payloads in JSON or XML, store credentials in HTTP Basic Authentication headers (Authorization: Basic dXNlcjpwYXNz), and encode the header and payload sections of JWTs. It is not encryption — it is purely an encoding scheme that converts binary data into a text-safe representation. For example, the HTTP Basic Auth header Authorization: Basic dXNlcjpwYXNz decodes to user:pass in plain text — anyone who intercepts it can decode it instantly. For security-sensitive use cases such as authentication, always pair Base64 with HTTPS. In Node.js the built-in Buffer class handles Base64 encoding natively; in Python the base64 module from the standard library covers all standard variants.
Is Base64 the same as encryption?
No. Base64 is an encoding, not encryption. Anyone can decode a Base64 string without a key in under a millisecond — there is no secret, no algorithm complexity, and no protection. It adds zero security whatsoever. For example, the string SGVsbG8sIFdvcmxkIQ== decodes instantly to Hello, World! using any Base64 decoder or even the browser console with atob('SGVsbG8sIFdvcmxkIQ=='). To actually protect sensitive data use symmetric encryption such as AES-256-GCM, or rely on TLS for data in transit. A common misconception is that "encoded" data is "hidden" — it is not. Base64 is a reversible transformation with no key, comparable to ROT13 in terms of security value. Libraries such as the Web Crypto API (crypto.subtle.encrypt) or Python's cryptography package provide real encryption.
What is URL-safe Base64?
URL-safe Base64 (also called Base64URL, defined in RFC 4648 §5) replaces + with - and / with _ so the encoded string can appear in URLs, filenames, and HTTP headers without percent-encoding. The standard + and / characters have special meaning in URLs — + represents a space in form data and / separates URL path segments — so they must be replaced for safe embedding. Padding = characters are also typically omitted in URL-safe Base64 since = is a query-string delimiter. JWTs use URL-safe Base64 for their header and payload sections, which is why JWT strings contain - and _ but no + or /. In Node.js: Buffer.from(data).toString('base64url'); in Python: base64.urlsafe_b64encode(data). OAuth tokens, PKCE code challenges, and many cookie values also use this variant.
Why does Base64 output end with == or =?
Base64 encodes data in 3-byte groups producing exactly 4 output characters each. Because not every input length is divisible by 3, one or two = padding characters are added to make the total output length a multiple of 4. One = means the last input group contained 2 bytes (6 bits are unused); == means the last group had only 1 byte (12 bits are unused). For example, encoding the single byte M (0x4D) produces TQ==. The padding ensures that decoders always know how many bytes the last group represents, which is important when processing streams. URL-safe Base64 and many modern implementations omit padding since the length can be inferred mathematically as Math.ceil(length * 4 / 3). When decoding a padless string, you must re-add padding before calling atob() or most standard decoders.
How much larger is Base64 output?
Base64 output is approximately 33% larger than the original binary input, because every 3 bytes of input produce exactly 4 ASCII characters — a ratio of 4:3. A 1 MB image becomes roughly 1.37 MB when Base64-encoded. MIME Base64 (used in email) also inserts a line break every 76 characters, adding a further small overhead. This overhead is why Base64 is used selectively — only when binary data must pass through a text-only transport layer, such as embedding a small PNG icon in a CSS file as a data URI (background: url(data:image/png;base64,...)). For large files or high-traffic APIs, the 33% size penalty is significant; use binary transports such as HTTP multipart or presigned S3 upload URLs instead. In Node.js, Buffer.byteLength(encoded, 'base64') gives the decoded byte size without actually decoding.
Can Base64 encode Unicode and emoji?
Yes, but the text must first be converted to UTF-8 bytes before Base64 encoding, because Base64 operates on bytes — not characters. This tool does that automatically using the browser's TextEncoder API, which produces a Uint8Array of UTF-8 bytes that is then encoded. A very common bug is passing a Unicode string directly to the browser's btoa() function: it throws an InvalidCharacterError for any character with a code point above U+00FF (Latin-1 range), which means emoji like 🦊 or non-Latin scripts like 日本語 cause an error. The safe pattern is btoa(String.fromCharCode(...new TextEncoder().encode(str))). In Node.js (v10+), Buffer.from(str, 'utf8').toString('base64') handles Unicode correctly and is the idiomatic approach. The Python equivalent is base64.b64encode(text.encode('utf-8')).decode('ascii').
How do I encode a file to Base64?
On macOS or Linux run: base64 filename.ext > output.b64 (or pipe to a file). On Windows PowerShell: [Convert]::ToBase64String([IO.File]::ReadAllBytes('filename.ext')). In Node.js: const fs = require('fs'); fs.readFileSync('filename.ext').toString('base64'). In Python: import base64; base64.b64encode(open('filename.ext','rb').read()).decode(). All of these read the file as raw bytes and convert those bytes to Base64, which is the correct approach for binary files such as images, PDFs, and archives. Opening in text mode ('r' rather than 'rb') on Windows can corrupt binary files due to CRLF line-ending conversion. To decode the file back, use base64 --decode output.b64 > filename.ext on Linux, or base64.b64decode(encoded) in Python with the result written in binary mode.
What is a Base64 data URI?
A data URI embeds file content directly into an HTML or CSS attribute using the format data:[mediatype];base64,[encoded data]. Example: <img src="data:image/png;base64,iVBORw0KGgo...">. It eliminates the separate HTTP request for that resource, which can reduce latency for tiny assets. However, it has two important downsides: the browser cannot cache the embedded asset separately from the HTML document, and the file is 33% larger due to Base64 overhead. This makes data URIs practical only for small, frequently reused assets — typically under 1–2 KB — such as small SVG icons or single-pixel tracking pixels. Email templates are a common use case because email clients often block external image URLs for privacy reasons, so embedding images as data URIs ensures they always display. In CSS: background-image: url('data:image/svg+xml;base64,...') is the standard pattern.
How do I decode a Base64 JWT token?
A JWT has three dot-separated parts: header.payload.signature. Each part is encoded with URL-safe Base64 (Base64URL) and has its padding stripped. To decode the payload section manually: take the middle segment (the part between the first and second dot), replace all - with + and all _ with /, then add = padding until the string length is a multiple of 4, then call atob() and JSON.parse(). In JavaScript: JSON.parse(atob(payload.replace(/-/g,'+').replace(/_/g,'/'))). This tool accepts URL-safe Base64 directly — paste the full token or just the payload segment, and the decoder handles the padding and character substitution automatically. Never trust a decoded JWT payload in security-sensitive code without first verifying the signature on the server side.
Why does my Base64 decode fail?
Common causes of Base64 decode failures include: (1) Whitespace or line breaks embedded in the string — MIME Base64 (RFC 2045) inserts a hard line break every 76 characters, so strip all whitespace with str.replace(/\s/g, '') before decoding. (2) Missing or extra = padding — standard decoders require the string length to be a multiple of 4; add padding if needed. (3) URL-safe characters (- and _) passed to a standard decoder that expects + and /; replace them first. (4) Truncated or corrupted input — a valid Base64 string's length mod 4 must equal 0, 2, or 3 (never 1). This tool auto-normalises URL-safe Base64 and strips extraneous whitespace before decoding, so pasting raw JWT sections or MIME email data works without manual preprocessing.
Is my data sent to a server?
No. All Base64 encoding and decoding runs entirely in your browser using JavaScript's built-in btoa(), atob(), TextEncoder, and TextDecoder APIs — no network requests are made and no data is ever transmitted to any server. This design is intentional: developers often need to decode Base64 payloads that contain API keys, JWT tokens, credentials, or private configuration values, and sending those to a third-party server would be a security risk. You can verify the tool's privacy by opening your browser's Network tab in DevTools — it will show no outbound requests when you encode or decode. No account, sign-up, or login is required, and the tool works fully offline once the page has loaded.

Related Tools

Other free developer tools available on DevFormatter.