Base64 Encoding & Decoding: Complete Developer Guide
Base64 is one of those fundamental encoding schemes that shows up everywhere in web development — JWT tokens, data URIs, email attachments, API responses, and binary data transmission. This guide explains exactly how it works, when to use it, and how to encode and decode it in any language.
What Is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data using only 64 printable ASCII characters: A–Z (26), a–z (26), 0–9 (10), and + and / (2). The = character is used for padding.
The name “Base64” comes from the mathematical base — this encoding uses 64 distinct symbols, just as Base10 (decimal) uses 10 symbols and Base16 (hexadecimal) uses 16.
Why 64? Because 64 characters can be represented in exactly 6 bits (2⁶ = 64), which makes the encoding efficient. Every 3 bytes (24 bits) of binary data maps to exactly 4 Base64 characters (4 × 6 bits = 24 bits).
How Base64 Encoding Works
The encoding process takes 3 bytes at a time and converts them to 4 Base64 characters:
- Take 3 bytes of binary data (24 bits total)
- Split into four 6-bit groups
- Look up each 6-bit value in the Base64 alphabet table (A=0, B=1, … Z=25, a=26, … z=51, 0=52, … 9=61, +=62, /=63)
- Output the corresponding 4 characters
- If input is not a multiple of 3, pad with
=characters
Example: the text Man (bytes: 77, 97, 110) encodes to TWFu.
Common Use Cases for Base64
- Data URIs — embedding images directly in HTML/CSS:
src="data:image/png;base64,...". Eliminates an HTTP request but increases HTML size by ~33%. - JWT tokens — the header and payload of a JWT are Base64URL-encoded JSON objects. See our JWT guide for the full breakdown.
- Email attachments (MIME) — email protocols (SMTP) only handle 7-bit ASCII. Binary attachments (PDFs, images) are Base64-encoded before transmission.
- Basic HTTP authentication — the
Authorization: Basic <credentials>header uses Base64-encodedusername:password. - Binary data in JSON — JSON doesn't have a binary type. When APIs need to transmit binary data (files, cryptographic keys, hashes), they Base64-encode it.
- Storing binary in text systems — databases, config files, and environment variables that only support text.
Base64 Encode / Decode in Code
JavaScript (Browser)
Node.js
Python
Command Line
Base64 vs Base64URL
Standard Base64 uses + and /, which have special meaning in URLs. Base64URL is a URL-safe variant:
| Feature | Base64 | Base64URL |
|---|---|---|
| Character 62 | + | - |
| Character 63 | / | _ |
| Padding | = required | = omitted |
| URL safe? | No | Yes |
| Used in | Email, data URIs | JWTs, URL params |
Related Tools
FAQ
What is Base64 encoding?
Base64 is a binary-to-text encoding scheme that converts binary data (bytes) into a string of 64 printable ASCII characters. It uses A–Z, a–z, 0–9, +, and / (with = as padding). It's used to safely transmit binary data through text-based systems like email, JSON APIs, and HTML.
How do I decode Base64 online?
Use DevKit's free Base64 tool — paste any Base64-encoded string and it decodes it instantly in your browser. The tool handles standard Base64, URL-safe Base64, and full UTF-8 text. Nothing is sent to a server.
Does Base64 encrypt data?
No. Base64 is encoding, not encryption. Anyone can decode a Base64 string — there's no key or secret involved. It converts binary data to text for transmission compatibility. Do not use Base64 to protect sensitive data.
Why do JWTs use Base64?
JWTs use Base64URL encoding (a variant of Base64 that replaces + with - and / with _ to make the token URL-safe) to encode the header and payload sections. The signature is also Base64URL-encoded. This makes JWTs compact and safe to use in HTTP headers, URL parameters, and cookies.
How much does Base64 increase file size?
Base64 encoding increases the size of binary data by approximately 33%. Every 3 bytes of binary data becomes 4 characters of Base64 text. A 1 MB image becomes roughly 1.33 MB when Base64-encoded. This is why Base64 data URIs in CSS/HTML should be used sparingly.
What is Base64URL?
Base64URL is a variant of Base64 that replaces + with - and / with _, and omits the = padding. This makes the output safe for use in URLs and HTTP headers without needing percent-encoding. JWT tokens use Base64URL encoding.