Base64 Explained: How Encoding Works, When to Use It and When Not To
Base64 turns binary data into text so it can travel through email, URLs and JSON — at a 33% size cost. Learn how it works, where it is used, and why it is never encryption.
Every developer meets Base64 sooner or later: in an email attachment, a JSON web token, an image embedded in CSS, or an API response that looks like alphabet soup. It looks like encryption. It is not. Base64 is a translation system — a way of writing binary data using only safe, printable text characters, so the data can pass through systems that were designed for text. This guide explains how the translation works, why it makes data about 33% larger, where Base64 is genuinely the right tool, where it causes harm, and how to fix the decode errors everyone hits eventually. What Base64 actually is Computers store files as bytes — values from 0 to 255. Many older and simpler systems, however, only handle text safely: email protocols, URLs, JSON strings, HTML and XML documents, and many configuration formats. Send raw bytes through them and control characters, quotes, or non-ASCII bytes may be mangled, interpreted as commands, or stripped. Base64 solves this by re-encoding bytes using an alphabet of just 64 characters: • A–Z (26 characters) • a–z (26 characters) • 0–9 (10 characters) • + and / (2 characters) That is 64 characters, defined in the standard RFC 4648. A 65th character, =, is used for padding at the end, as explained below. Because every character in that set is printable and has the same meaning in
virtually every text system, Base64 output survives being copied into an email, a JSON field, a URL parameter (with a variant, below), or a database text column. How the encoding works, step by step Base64 works in groups:
- Take 3 bytes of input. Three bytes are 24 bits.
- Split those 24 bits into 4 groups of 6 bits. Each 6-bit group has a value from 0 to 63.
- Map each value to a character in the 64-character alphabet.
- Repeat until the input is consumed. A short example. The text Man in ASCII is the bytes 77, 97, 110. Written in binary and regrouped into 6-bit chunks, those values map to the characters T, W, F, u — so: Man → TWFu You can test this pattern in any language: // JavaScript (browser / Node with Buffer) btoa("Man"); // "TWFu" (for Latin-1 text) Buffer.from("Man").toString("base64"); // "TWFu"
Python
import base64 base64.b64encode(b"Man") # b'TWFu' base64.b64decode("TWFu") # b'Man' Why padding = appears Input does not always divide neatly into 3-byte groups. If 1 byte remains, it produces 2 Base64 characters plus ==. If 2 bytes remain, they produce 3 characters plus =. Padding tells the decoder exactly how many real bytes the final group contains: M → TQ== Ma → TWE= Man → TWFu If you see = in the middle of a string, or a length that is not a multiple of 4, something has usually been truncated or corrupted in copying. Why Base64 is about 33% larger The maths is fixed by the design: 3 bytes become 4 characters. Four is one-third more than three, so encoded data is approximately 33% larger than the original, before any line breaks are added. A 3 MB image becomes roughly 4 MB as Base64 text. Remember this number — it decides several of the "when not to use it" cases below. Where Base64 is used correctly Email attachments (MIME) Email was built for text. Attachments are therefore Base64-encoded so a PDF or photo can travel inside a message. This is also why email attachment limits bite sooner than people expect: the file grows by about a third in transit. Data URIs for small images and assets
A data URI embeds content directly in HTML or CSS: <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." alt=""> This makes sense for tiny, critical assets — a small icon, for example — because it saves a separate HTTP request. For large images it is usually a mistake: the 33% overhead applies, the image cannot be cached separately by the browser, and the HTML or CSS file itself becomes bloated and hard to maintain. JSON Web Tokens (JWTs) A JWT has three parts separated by dots: header, payload, and signature. The first two parts are JSON encoded with Base64URL, a variant that replaces + with - and / with _, and usually omits padding, so the token is safe inside URLs and HTTP headers. Anyone can decode the header and payload of a JWT — that is by design, and it is why a token must never contain secrets in its payload. The signature exists to prove the content was not tampered with; it does not hide the content. Basic authentication and API examples HTTP Basic authentication sends credentials as Base64 of username:password. Because Base64 is reversible by anyone, Basic auth is only acceptable over HTTPS, where TLS provides the actual protection. The same applies whenever you see Base64 in an API example: it is a transport format, not a shield. Embedding binary in text formats Certificates, keys in PEM format, small binary payloads in JSON APIs, and test fixtures are all legitimate cases: the surrounding format is text, the payload is binary, and the size overhead is acceptable. When not to use Base64 • Never as encryption or security. Decoding requires no key and no password. Anyone who sees Base64 can recover the original in seconds. Never "protect" passwords, API keys, personal data, or licence files by encoding them. Use real encryption and proper secret storage for that. • Not for large files in web pages. A large data-URI image delays rendering, defeats separate caching, and inflates your HTML. Serve the file normally and let the browser cache it. • Not as compression. Base64 makes data larger, not smaller. Compress first (for example, optimise the image), then encode only if the transport requires it. • Not to store binary in a database long-term, if a binary column or file storage is available. You pay 33% more storage and lose the ability to treat the content as a file. • Not to obscure data you hope nobody notices. Obfuscation is not a control. Reviewers, scanners, and attackers decode Base64 routinely. Base64 variants you will meet Variant Difference Where you see it Standard Base64 Uses + and /, with = padding Email (MIME), PEM files, general encoding Base64URL Uses - and _, padding often omitted JWTs, URL parameters, filenames MIME Base64 Standard alphabet, output wrapped at 76 characters per line Email attachments Mixing variants is a common bug: decoding a Base64URL token with a strict standard decoder can fail on the - and _ characters or on missing padding. Common decode errors and how to fix them
- "Invalid character" errors. The string contains spaces, line breaks, or URL-escaped characters that the decoder does not accept. Trim whitespace, and check whether the source is Base64URL rather than standard Base64.
- Truncated copy-paste. The length is not a multiple of 4, or padding is missing in a decoder that requires it. Copy the complete string again — tokens and encoded blobs are often cut off by terminal wrapping or chat apps.
- Double encoding. Decoding once produces another Base64-looking string instead of readable data. It was encoded twice, sometimes by two different layers of a system. Decode again only if you are sure that is what happened, and fix the producing code.
- Decoding text with the wrong character encoding. Base64 decodes to bytes, not text. If those bytes are UTF-8 text containing characters beyond Latin-1, you must decode the bytes as UTF-8 afterwards. In JavaScript, btoa() on a Unicode string can throw or corrupt data; encode the UTF-8 bytes first, or use TextEncoder.
- Expecting the output to be private. Not an error message — a design error. If decoded content surprises you with how readable it is, that is the lesson: Base64 hides nothing. Common mistakes • Treating Base64 as encryption in a design, a code review, or a sales claim. • Putting large images into CSS as data URIs to "reduce requests" and making the page slower. • Forgetting the 33% overhead when estimating email, API payload, or storage sizes. • Manually editing an encoded string — one changed character corrupts the bytes that group represents. • Assuming every decoder accepts every variant, line break, and padding style. FAQ Is Base64 encryption? No. It is an encoding with a public, fixed alphabet and no key. Anyone can decode it instantly. Why is my Base64 string so long? Because 3 bytes become 4 characters — about 33% larger — plus possible line breaks in MIME format. Can Base64 contain = in the middle? In valid encoded output, = appears only as padding at the end. Seeing it in the middle usually means two encoded strings were joined, or the data was truncated. What is the difference between Base64 and Base64URL? Base64URL replaces + and / with - and _ so the result is safe in URLs, and it often drops padding. JWTs use Base64URL. Should I Base64-encode images for my website? Only very small ones, if at all. Normal image files are smaller in total, cacheable, and easier to maintain. Conclusion Base64 is a reliable translator between the binary world and text-only systems: 3 bytes in, 4 characters out, about 33% larger, completely reversible by anyone. Use it for email attachments, tokens, certificates, and small embedded assets. Never use it as encryption, compression, or a place to hide data. Understand that one distinction, and most Base64 bugs — and security mistakes — disappear. Sources: RFC 4648, The Base16, Base32, and Base64 Data Encodings, https://www.rfc-editor.org/rfc/rfc4648 ; RFC 7519, JSON Web Token (JWT), https://www.rfc-editor.org/rfc/rfc7519 ; MDN Web Docs, Base64 and btoa() / atob(), https://developer.mozilla.org/en-US/docs/Glossary/Base64
Related reading
- How to Fix Invalid JSON: 10 Common Errors With Examples
- JSON vs CSV vs XML: Which Data Format Should Your Project Use?
- AI for Developers: Debugging, Explaining Code and Writing Tests
Related QUORVLY tool
Put this into practice with the free Quorvly tools — no account needed, nothing uploaded.
All articles