Base64 is a way of writing any bytes, whether text, images or compressed files, using only 64 ordinary characters. It exists because many systems were built to carry text safely but can corrupt arbitrary bytes, from early email to HTTP headers and URLs.
How it works
Base64 takes the input 3 bytes (24 bits) at a time, splits them into four groups of 6 bits, and writes each group as one character from this alphabet:
A–Zfor 0–25,a–zfor 26–51,0–9for 52–61+for 62 and/for 63
For example, the text Hi! is the bytes 48 69 21 (hexadecimal), which becomes SGkh. When the input length is not a multiple of 3, the last group is completed with = padding: Hi encodes as SGk=, and H as SA==.
Because every 3 bytes become 4 characters, Base64 output is about 33% larger than the input, plus any line breaks.
Text must become bytes first
Base64 encodes bytes, not letters, so text is first converted to bytes with a character encoding, nearly always UTF-8 today. The same text can therefore produce different Base64 if it was encoded with a different character set. The browser’s built-in btoa() function only accepts characters up to code point 255, which is why it fails on many accented letters outside Western European languages and on emoji; tools that handle text properly convert it to UTF-8 first. See character encoding and UTF-8.
Variants
The current standard is RFC 4648. It defines two alphabets:
| Variant | Characters 62 and 63 | Padding | Typical use |
|---|---|---|---|
| Base64 | + and / | = | Email attachments, data URIs, many APIs |
| base64url | - and _ | Often omitted | URLs, file names, JSON Web Tokens |
MIME email, defined in RFC 2045, also breaks Base64 into lines of at most 76 characters. Decoders generally ignore those line breaks.
Where you will see it
- Email attachments, encoded as Base64 inside the message.
- Data URIs such as
data:image/png;base64,iVBORw0…, which embed small files in HTML or CSS. - HTTP Basic authentication, which sends
username:passwordBase64-encoded in a header. - JSON Web Tokens, made of three base64url parts separated by dots; the first two are JSON.
Base64 is not encryption
Anyone can decode Base64 instantly; it has no key. HTTP Basic authentication, for example, is only safe over HTTPS, because the Base64 in the header reveals the password to anyone who can read it. Treat Base64-encoded secrets exactly as you would the plain secret.
Base64 versus URL encoding
Both make data safe to transport, but they are different. URL encoding leaves ordinary letters readable and only escapes special characters, which suits text in URLs. Base64 rewrites everything, which suits binary data. To put Base64 inside a URL, use the base64url variant.
Encode and decode text with the Base64 Encoder and Base64 Decoder.