What Is Base64: How It Works, What It's For, and Examples
Base64 turns bytes into text using 64 characters. See how it works step by step, why it's 33% larger, when to use it, and why it isn't encryption.
You open the JSON response from an API and find a field holding something like iVBORw0KGgoAAAANSUhEUgAA... that runs for dozens of lines. Or you paste a token and notice it ends in ==. A lot of people's first reaction is "this is encrypted." It isn't. It's Base64 — an encoding, not a cipher. Understanding that distinction is what separates people who use Base64 safely from people who bake a false sense of protection into their systems.
Short answer: Base64 is a way to represent any sequence of bytes (image, PDF, key, text) using only 64 safe text characters: A–Z, a–z, 0–9, + and /. It exists to carry binary data through channels that only accept text — email, JSON, HTML, HTTP headers — at the cost of making the data about 33% larger.
The rest of this guide shows how the algorithm works step by step, where it shows up, why it isn't encryption, the Base64URL variant, how to encode and decode in JavaScript, Python and the terminal, and when not to use it. Every encoding example below was run in Node, Python and GNU coreutils base64.
How Base64 works, step by step
Instead of working byte by byte (8 bits), Base64 groups the input bits into 6-bit chunks. Since 2⁶ = 64, each 6-bit chunk maps to exactly one of the 64 characters in the table. Three input bytes (24 bits) become four output characters (4 × 6 = 24 bits).
Take the string Man:
Text: M a n
Byte: 77 97 110
Binary: 01001101 01100001 01101110
In 6 bits: 010011 010110 000101 101110
Index: 19 22 5 46
Base64: T W F u
Result: TWFu. That's exactly what any library returns. Each index maps to a character through this table, defined in RFC 4648:
| Indexes | Characters |
|---|---|
| 0–25 | A to Z |
| 26–51 | a to z |
| 52–61 | 0 to 9 |
| 62 | + |
| 63 | / |
| padding | = |
What the trailing = means
Input almost never has a byte count that's a multiple of 3. When there's a remainder, the last group is completed with zero bits only as far as needed to finish the last 6-bit character, and the positions that would be empty in the group of 4 become =. RFC 4648 (§4) defines both cases:
"Man" (3 bytes, 0 left over) → TWFu
"Ma" (2 bytes, 2 left over) → TWE= 3 characters + "="
"M" (1 byte, 1 left over) → TQ== 2 characters + "=="
"Hello" (5 bytes = 3 + 2) → SGVsbG8=
In other words: 1 leftover byte → ==; 2 leftover bytes → =. It has nothing to do with encryption — it's just the size adjustment that keeps the output a multiple of 4.
What Base64 is used for
Many internet protocols were designed for text. The classic case is email: SMTP was built for 7-bit text messages, and arbitrary bytes could be altered or broken in transit. MIME solved it by encoding attachments in Base64 (RFC 2045), and that's still how it works today. The same reasoning applies whenever binary data has to live inside a text format:
- Data URIs in HTML and CSS —
data:image/png;base64,...embeds a small image directly in the markup, with no extra HTTP request. For the way in, the image to Base64 converter produces the data URL; for the way back, Base64 to image previews and saves the file. - JSON and REST APIs — JSON has no binary type, so files, hashes and keys travel as Base64 strings. For large files,
multipart/form-datais better (more on that below). - JWT — header, payload and signature are Base64URL, separated by dots. The payload isn't encrypted: anyone can decode and read it.
- HTTP Basic Authentication —
user:passwordgoes in Base64 in theAuthorizationheader (RFC 7617). That doesn't protect the password; HTTPS is what keeps it secret. - PEM certificates and keys — the block between
-----BEGIN CERTIFICATE-----and-----END CERTIFICATE-----is the binary (DER) certificate in Base64. - Databases — you can store binary as Base64 in a
TEXTcolumn, butBYTEA(PostgreSQL) orBLOB(MySQL) store native binary without the extra 33%.
When the question goes the other way — "I have a Base64 string, can I save it as a file?" — yes: decode it to bytes and write the bytes. The step-by-step for images, PDFs and other files is in how to convert Base64 to text, image and file.
Why Base64 is 33% larger
Every 3 bytes become 4 characters, so the output size is 4 × ceil(n / 3), where n is the input byte count. A 100 KB file (102,400 bytes) becomes 136,536 characters, about 133 KB. A 1 MB image becomes ~1.33 MB. A 500 MB upload would become ~667 MB of string in memory.
In email the overhead is a bit higher: MIME limits each line to 76 characters and inserts a line break (CRLF) between them. GNU coreutils base64 mimics this by default — it wraps output every 76 characters; use base64 -w 0 for a single line.
Beyond size, a long Base64 string is opaque: it clutters logs, breaks diffs and doesn't compress as well as the original binary. That weighs on the decision to embed or not.
Base64 vs Base64URL
Standard Base64 uses + and / as characters 62 and 63, and both mean something in URLs: / separates path segments, and in a query string in application/x-www-form-urlencoded format + decodes as a space. Standard Base64 dropped into a URL parameter without percent-encoding reaches the server with spaces where the + signs were, and decoding fails.
The Base64URL variant (RFC 4648 §5) swaps + for - and / for _:
bytes FB FF → Base64: +/8=
→ Base64URL: -_8 (no padding)
Padding is optional in the URL variant and often omitted; JWT, for example, requires Base64URL without = (RFC 7515). That's why a JWT never contains +, / or =. If you're choosing between Base64 and percent-encoding for a value in a URL, the post on URL encoding compares the two.
Base64 is not encryption
Base64 is fully reversible and uses no key. There's no secret involved:
// This does NOT protect the password. Anyone reverses it.
const fake = btoa("admin:supersecreta123"); // "YWRtaW46c3VwZXJzZWNyZXRhMTIz"
atob(fake); // "admin:supersecreta123"
The rule: Base64 is for transport and representation, not confidentiality. To protect data, use real cryptography — symmetric or asymmetric, depending on the case. For passwords, not even that: use hashing with bcrypt, scrypt or Argon2. Base64 can come afterward, to package already-encrypted output as text — never as the protection layer.
Base64 is not UTF-8
Another common mix-up: treating Base64 as a "text encoding" on the same level as UTF-8. UTF-8 maps characters to bytes; Base64 maps bytes to ASCII text. To encode a string with accents, it first becomes bytes in UTF-8, then Base64. The browser's btoa skips that step and treats each character as a Latin-1 byte — which produces two different bugs:
btoa("€"); // InvalidCharacterError: outside Latin-1, won't even encode
btoa("ação"); // "Yefjbw==" — encodes, but as Latin-1, not UTF-8
// The UTF-8 Base64 of "ação" is "YcOnw6Nv"
The second is the sneaky one: no error, and the other side decodes it as UTF-8 and gets garbage. That step just before Base64 is exactly the UTF-8 encoding trap.
How to encode and decode Base64 in JavaScript, Python and the terminal
JavaScript in the browser, with correct UTF-8:
// Text → Base64 (UTF-8)
const bytes = new TextEncoder().encode("ação");
const b64 = btoa(String.fromCharCode(...bytes)); // "YcOnw6Nv"
// Base64 → text (UTF-8)
const back = Uint8Array.from(atob(b64), c => c.charCodeAt(0));
new TextDecoder().decode(back); // "ação"
Current browsers have a native API for this: Uint8Array.prototype.toBase64() and Uint8Array.fromBase64(), Baseline 2025 (available across major browsers since September 2025), with Base64URL support:
new TextEncoder().encode("ação").toBase64(); // "YcOnw6Nv"
new TextDecoder().decode(Uint8Array.fromBase64("YcOnw6Nv")); // "ação"
new Uint8Array([0xfb, 0xff]).toBase64({ alphabet: "base64url", omitPadding: true }); // "-_8"
In Node, I tested it: the API exists in Node 25 but not yet in Node 24. In Node, the classic route is Buffer:
Buffer.from("ação").toString("base64"); // "YcOnw6Nv"
Buffer.from("YcOnw6Nv", "base64").toString("utf8"); // "ação"
Buffer.from([0xfb, 0xff]).toString("base64url"); // "-_8"
Python:
import base64
base64.b64encode("ação".encode("utf-8")) # b'YcOnw6Nv'
base64.b64decode("YcOnw6Nv").decode("utf-8") # 'ação'
base64.urlsafe_b64encode(bytes([0xFB, 0xFF])) # b'-_8=' (keeps the padding)
Terminal (GNU coreutils):
printf 'Hello' | base64 # SGVsbG8=
echo 'YcOnw6Nv' | base64 -d # ação
base64 -w 0 file.pdf # whole file on a single line
A padding detail that trips people up: the browser's atob and Node's Buffer accept SGVsbG8 without the trailing =, but GNU base64 -d answers invalid input and Python raises Incorrect padding. When you receive Base64URL without padding, add = until the length is a multiple of 4 before decoding with those tools. To inspect a pasted string without writing code, the Base64 decoder accepts data URLs and Base64URL, and to look inside a token the JWT decoder splits the three parts.
When not to use Base64
- Large files. A video upload as Base64 inside JSON forces the server to hold the whole string (33% larger) in memory before processing.
multipart/form-dataexists for this and allows streaming. - A channel that already accepts binary. HTTP carries binary bodies with the right
Content-Type(application/octet-stream,image/png), in any protocol version. WebSocket has binary frames. gRPC has the Protocol Buffersbytestype. If you control both sides and the channel takes binary, Base64 only adds overhead. - Large images as Data URIs. They bloat the HTML, aren't cached separately and delay first render. Data URIs are worth it for icons of a few KB.
- Protection. As we saw: it protects nothing.
- Data that's already text. Encoding JSON in Base64 just wastes space, unless the channel mangles special characters.
To compare with the other common way of representing bytes as text, see Base64 vs hex: hex is more readable and doubles the size; Base64 is more compact.
Frequently asked questions
Is Base64 encryption?
No. Base64 is reversible, keyless encoding: anyone with the string recovers the original data in a second. Encryption uses a secret key to make data unreadable without it. Confusing the two is the most common security mistake made with Base64.
Why do Base64 strings end in "=" or "=="?
It's padding. The output is always a multiple of 4 characters; when the input isn't a multiple of 3 bytes, the last group is completed with =. If 1 byte is left over, it becomes 2 characters + ==; if 2 bytes are left over, they become 3 characters + =. Hello (5 bytes) becomes SGVsbG8=.
Why does a JWT have dots in the middle of the Base64?
Because a JWT isn't a single Base64 string: it's three Base64URL parts separated by dots — header.payload.signature. Each part is encoded separately, without padding. The header describes the algorithm, the payload carries the claims, and the signature is computed over the first two.
Can I store passwords in Base64?
No. Base64 is reversible with no extra information. Passwords are stored with a slow, salted hash function — bcrypt, scrypt or Argon2 — that can't be reversed.
Is Base64 a tool or a program?
Neither: it's a standardized encoding format (RFC 4648). Every language has a function for it, Unix systems ship a base64 command, and there are online tools that encode and decode in the browser.
Base64 changes the form of the data, never the secret
Base64 is a translator between the binary world and the text world: 3 bytes become 4 characters, = closes the last group, and the result is 33% larger. It makes sense when the channel only speaks text and the data is small — email, Data URIs, binary fields in JSON, parts of a JWT. It hurts when the file is large, when the channel already accepts binary, or when someone expects it to protect something. Use Base64URL in URLs, go through UTF-8 before encoding text, and check the padding when decoding.
Note: the editorial content ends here. What follows is a pointer to a related tool.
Related tool
To encode text or a whole file without writing code, the Base64 encoder converts UTF-8 text and files (PDF, ZIP, DOCX) to Base64 or Base64URL right in the browser, without sending anything to a server.
- 01 CPU Cores vs. Threads: What Actually Changes in Practice Understand the difference between CPU cores and threads and what actually changes for performance, multitasking, gaming, and development.
- 02 MIT, Apache, and GPL Licenses: Practical Differences for Developers MIT, Apache 2.0, and GPL are not the same thing. Understand permissive vs copyleft, patent protection, and compatibility before shipping code to production.