What is Base64?

Base64 is a binary-to-text encoding: it represents bytes using a 64-character alphabet, so binary data can be carried through formats or transports that expect text. It changes the representation, not the underlying information, and it is not compression or encryption.[1][4]

For example, the ASCII bytes for foo encode as Zm9v and decode back to the same three bytes.[1] Try the Base64 Encoder/Decoder for a quick conversion in your browser.

Base64 decoding gives you the original bytes, not necessarily readable text. If those bytes represent text, interpret them with the character encoding specified by the surrounding format; Base64 itself does not identify that encoding.[1][4]

How Base64 works

Standard Base64 takes three bytes (24 bits) at a time, splits them into four 6-bit values, then maps each value to a character in the alphabet A–Z, a–z, 0–9, +, and /.[1]

When the final group has fewer than three input bytes, = padding completes the output group.[1]

  • f (one byte) → Zg==
  • fo (two bytes) → Zm8=
  • foo (three bytes) → Zm9v

The padding marks the incomplete final group; it is not extra input data. Under the general Base64 rules, encoders include the required padding unless the format using Base64 explicitly says to omit it.[1]

Where Base64 is used

Base64 is useful when a format needs to carry bytes using text characters. For example, MIME email defines Base64 as a content-transfer encoding; a data URL can include binary content in Base64 form; and compact JWTs use Base64URL for their parts.[2][3][4]

That does not mean every API, JSON field, or URL expects Base64. Check the format or API documentation and use the exact alphabet, padding, and wrapping rules it specifies.[1] If a JWT is what brought you here, see the JWT guide for the difference between encoding and token security.

Base64URL is not the same as URL encoding

Standard Base64 uses + and /, which can be awkward in URLs and filenames. Base64URL replaces those two characters with - and _.[1]

For example, the bytes 2E 8B DE FF 2A 2E encode as Love/you in standard Base64 and Love_you in Base64URL; the underlying bytes are identical.[1][8]

Whether to omit trailing = padding depends on the format: RFC 4648 allows a referring specification to omit it, and JWS compact serialization explicitly does so.[1][6]

Base64URL and percent-encoding solve different problems. Base64URL represents bytes with a URL-friendly Base64 alphabet; percent-encoding represents an octet in a URI component as % followed by two hexadecimal digits.[1][7] For example, the UTF-8 bytes for hello world become aGVsbG8gd29ybGQ= in standard Base64, while a URI component can represent that same text as hello%20world. Use the URL Encoder and read the URL encoding guide when you need URI escaping rather than Base64.

Size and security trade-offs

With standard padding, n input bytes produce 4 × ceil(n / 3) Base64 characters. Three bytes therefore become four characters, so large encoded values are roughly one-third longer; for small inputs, the exact increase varies.[1][4]

Base64 is not encryption: anyone who has the string can decode it, and the encoding provides no computational confidentiality.[1] It also does not authenticate the sender or detect tampering by itself; signatures and message authentication codes are separate mechanisms.[6] Treat decoded bytes as untrusted—turning them into text does not make their contents safe to display or run.

Base64 and Unicode in JavaScript

Base64 encodes bytes, not Unicode characters. To encode text, first convert it to bytes using a character encoding such as UTF-8. In browsers, btoa() expects a binary string whose character values each fit in one byte; passing arbitrary Unicode text directly can throw an error.[5]

For a short input such as café, encode the text as UTF-8 bytes before using btoa(). This example produces Y2Fmw6k= and decodes the bytes back to café.[5] For larger buffers, Uint8Array.prototype.toBase64() encodes directly from bytes without first building a binary string; check support if older browsers matter.[8]

const bytes = new TextEncoder()
  .encode("café");
const binary = String
  .fromCharCode(...bytes);
const base64 = btoa(binary);

const raw = atob(base64);
const decoded = Uint8Array.from(
  raw,
  c => c.charCodeAt(0)
);
const text = new TextDecoder()
  .decode(decoded);

The same rule applies outside JavaScript: agree on the source text encoding first, then Base64-encode those bytes. When decoding, recover the bytes and interpret them using the encoding the sender used.[5]

Frequently asked questions

What is Base64 used for?

It represents arbitrary bytes with a restricted text alphabet when a format or transport expects text. MIME email and data URLs are examples; the receiving format still determines the exact rules.[1][2][4]

Does Base64 encrypt data?

No. Base64 is reversible encoding, not encryption. Anyone can decode it, so it does not keep a password or other secret confidential.[1]

Why does Base64 sometimes end with =?

Padding marks an incomplete final group when the input byte count is not divisible by three. Standard Base64 uses the required padding; a protocol may specify an unpadded form.[1]

Is Base64URL the same as Base64?

It is a Base64 variant: it changes + and / to - and _. Padding behavior depends on the format; JWS compact serialization omits it.[1][6]

How much larger is Base64 than the original data?

For padded Base64, the output length is 4 × ceil(n / 3) characters for n input bytes. Large inputs are about 33% longer, while very short inputs can grow by more.[1][4]

Can JavaScript btoa() encode Unicode text?

Not directly for arbitrary Unicode. Convert the text to UTF-8 bytes first, then pass a byte-oriented string to btoa(); decode the bytes with TextDecoder afterward.[5]

Sources

[1] https://www.rfc-editor.org/rfc/rfc4648 — RFC 4648 — The Base16, Base32, and Base64 Data Encodings [2] https://www.rfc-editor.org/rfc/rfc2045 — RFC 2045 — MIME Part One: Format of Internet Message Bodies [3] https://www.rfc-editor.org/rfc/rfc7519 — RFC 7519 — JSON Web Token (JWT) [4] https://developer.mozilla.org/en-US/docs/Glossary/Base64 — MDN Web Docs — Base64 [5] https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa — MDN Web Docs — Window.btoa() [6] https://www.rfc-editor.org/rfc/rfc7515 — RFC 7515 — JSON Web Signature (JWS) [7] https://www.rfc-editor.org/rfc/rfc3986 — RFC 3986 — URI: Generic Syntax [8] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64 — MDN Web Docs — Uint8Array.prototype.toBase64()