What Base64 is for
Base64 turns arbitrary bytes into text made up of only 64 printable ASCII characters (A–Z, a–z, 0–9, "+", "/"), plus "=" for padding. That makes it safe to embed binary data — or text using characters some systems can't handle — inside formats that only guarantee plain ASCII will survive: email (MIME attachments), URLs, JSON strings, and configuration files. The trade-off is size: Base64 output is roughly a third larger than the original data.
It is not encryption. Encoding and decoding require no secret, so Base64 should never be used as a way to hide sensitive data — only to safely transport it through systems that would otherwise mangle raw bytes.
Example
Encoding Hello, world! produces:
SGVsbG8sIHdvcmxkIQ==Where you'll run into Base64
- HTTP Basic Authentication — the
Authorization: Basic …header isusername:passwordBase64-encoded (not encrypted — HTTPS is what protects it in transit). - Data URIs —
data:image/png;base64,…embeds an image directly inside HTML or CSS. - JWTs — each of the three dot-separated segments is Base64URL-encoded JSON.
- Email attachments — MIME encodes binary files as Base64 so they survive text-only mail transport.
Doing it in code
JavaScript
const encoded = btoa(unescape(encodeURIComponent(text))); // legacy pattern
const decoded = decodeURIComponent(escape(atob(encoded)));
// or, more directly, on modern engines:
const encoded2 = Buffer.from(text, "utf8").toString("base64"); // Node.jsPython
import base64
encoded = base64.b64encode(text.encode("utf-8")).decode("ascii")
decoded = base64.b64decode(encoded).decode("utf-8")