Base64 is one of those things most developers have copy-pasted a hundred times without ever stopping to think about what it does. It shows up in data URLs, JWT tokens, email attachments, API payloads, and config files. Understanding it properly saves you from a surprising number of debugging dead-ends.
What Base64 actually is
Base64 is an encoding scheme that represents binary data using only 64 printable ASCII characters: A–Z, a–z, 0–9, plus + and /. The = character is used for padding.
The core idea is simple. It takes three bytes (24 bits) of input and splits them into four groups of 6 bits. Each 6-bit group maps to one of those 64 characters. Since 26 = 64, every possible 6-bit value has a character.
Crucially, Base64 is not encryption and it is not compression. It makes data larger — roughly 33% bigger than the original — because you're spending 4 output characters for every 3 input bytes. Anyone can decode it instantly. If you ever see a credential "protected" by Base64, that credential is effectively in plain text.
Why you'd use it
The whole point is to move binary data safely through systems that were designed for text. Plenty of protocols and formats choke on raw bytes — null characters, control codes, or anything outside the printable range can get mangled, truncated, or reinterpreted.
- Email (MIME): the original motivation. SMTP is a text protocol, so attachments get Base64-encoded to survive transit.
- Data URLs: embedding a small image or font directly in HTML/CSS with
data:image/png;base64,...to avoid an extra HTTP request. - JSON and XML payloads: JSON has no native binary type, so a file upload or a cryptographic blob often travels as a Base64 string.
- JWTs: each section of a JSON Web Token is Base64url-encoded (more on that variant below).
- Basic Auth headers:
Authorization: Basic base64(user:pass). Note that "basic" is doing a lot of honest work there — it's encoded, not secured.
A short worked example
Let's encode the three-byte string Man by hand, because seeing it once makes the whole scheme click.
M a n
01001101 01100001 01101110 (ASCII bytes)
Regroup into 6-bit chunks:
010011 010110 000101 101110
19 22 5 46
Map to the Base64 alphabet:
T W F u
Result: "TWFu"When the input length isn't a multiple of three, you pad with =. Encoding just M (one byte) gives TQ==, and Ma (two bytes) gives TWE=. That padding tells the decoder how many bytes were really there.
In practice you'll use your language's standard library rather than doing bit math:
// JavaScript (browser)
const encoded = btoa("Man"); // "TWFu"
const decoded = atob("TWFu"); // "Man"
// Node.js (handles binary properly)
const buf = Buffer.from("Man", "utf8");
const encoded2 = buf.toString("base64"); // "TWFu"
const decoded2 = Buffer.from(encoded2, "base64").toString("utf8");
// Python
import base64
base64.b64encode(b"Man") # b'TWFu'
base64.b64decode(b"TWFu") # b'Man'When I just need to check a token or sanity-test a payload without opening a REPL, I keep a browser tab on Devsly's Base64 Encoder / Decoder — paste in, read it out, done.
The gotchas that actually bite people
btoa and atob are not Unicode-safe
This is the classic JavaScript trap. btoa only handles characters in the Latin-1 range (0–255). Feed it an emoji or accented text and it throws an InvalidCharacterError:
btoa("café"); // throws in many engines
// Encode to UTF-8 bytes first:
const bytes = new TextEncoder().encode("café");
const b64 = btoa(String.fromCharCode(...bytes));
// Decode back:
const binary = atob(b64);
const arr = Uint8Array.from(binary, c => c.charCodeAt(0));
const text = new TextDecoder().decode(arr); // "café"Base64url is a different alphabet
JWTs, URLs, and filenames can't safely contain + and /. The Base64url variant swaps them for - and _, and often drops the = padding entirely. If you decode a JWT with a standard Base64 decoder and it fails, this is usually why. Convert before decoding:
const b64 = base64url
.replace(/-/g, "+")
.replace(/_/g, "/");Encoding is not the same as escaping
Base64 makes binary safe for text transport, but it does nothing for HTML, SQL, or shell contexts. A decoded value can still contain <script> or a SQL injection payload. Always escape for the destination context after you've decoded.
The size cost adds up
That 33% overhead is fine for a 2KB icon in a data URL. It's not fine for a 5MB image you inline on every page load, which also loses browser caching since it's baked into the HTML or CSS. Use Base64 inlining sparingly and measure.
Whitespace and line breaks
Some encoders (particularly MIME-oriented ones) insert line breaks every 76 characters. Strict decoders may reject that whitespace. If a decode fails on a string that "looks fine," strip the newlines and try again.
Rules of thumb
- Use Base64 to carry binary through text channels — nothing more.
- Never treat it as a security measure. Encoded is not hidden.
- Normalise to UTF-8 bytes before encoding text so you don't lose non-ASCII characters.
- Know whether you need standard or URL-safe Base64 for the target format.
- Keep inlined assets small to avoid bloating payloads and killing caching.
Base64 is boring technology in the best sense: it's been stable for decades and it does exactly one job well. Treat it as a transport format, respect the encoding of your text, and it'll never surprise you.
Photo by Jake Walker on Unsplash