Skip to content
FmtTools

Format reference

Base64: The Complete Encoding Guide

Three bytes become four characters — the arithmetic, the alphabets, the padding rules, the 33% overhead, and every place the encoding shows up in practice.

Updated
November 17, 2026
Reading time
9 min read
Length
1,785 words
Reviewer note
Checked against the published specification
A flat vector illustration of a binary byte stream passing through an encoder into a printable alphabet of letters, digits and the plus and slash characters.

Arithmetic

The problem Base64 solves, and the bits behind it

TL;DR. Base64 rewrites arbitrary bytes using 64 printable characters — it does not compress, detect corruption, or conceal anything. Three octets become four characters, always.

Base64 is a binary-to-text encoding: it rewrites an arbitrary sequence of octets using 64 characters that every text transport agrees to pass through unmangled, plus one filler character. RFC 4648 standardises five related encodings in one document, and its section 3 exists because the pre-RFC world disagreed about line breaks, padding and stray characters — still the source of most base64 bug reports. JSON has a milder version of the same problem: RFC 8259 §7 requires control characters to be escaped and §8.1 fixes UTF-8, so a JPEG, a signature or a key cannot appear in a JSON string as itself.

Three bytes, twenty-four bits, four characters

Everything else follows from one observation: 3 octets contain 24 bits, and 24 divides exactly by 6, so each output character carries 6 bits. The encoder reads three octets, concatenates them into one 24-bit number, cuts that into four 6-bit pieces and looks each up in the alphabet. Take the three characters M, a and n — the example RFC 4648 itself uses in §9.

worked example9 lines
M = 0x4D = 01001101
a = 0x61 = 01100001
n = 0x6E = 01101110

24-bit stream:  010011 010110 000101 101110
6-bit indices:      19     22      5     46
Alphabet lookup:     T      W      F      u

base64("Man") = "TWFu"   (3 octets in, 4 characters out)

The bit groups straddle octet boundaries on purpose: index 22 is the low two bits of M followed by the high four bits of a.

The regrouping never asks whether a byte is printable, valid UTF-8, or part of a multibyte sequence — base64 has no notion of characters, only octets. That is why the encoding is safe for compressed streams, image data and key material. The group size is three because nothing smaller fits: a single octet leaves four bits stranded and two leave two, so only at three does the fit become exact.

Three stages: the octets for Man in binary, the 24 bits regrouped into four six-bit indices, and the indices mapped through the RFC 4648 alphabet to TWFu.
The whole algorithm in one picture. Everything after this point is about what happens when the input is not a multiple of three, which alphabet is in force, and where line breaks may be inserted.

The 33 percent penalty

Base64 expands data by a third: four characters on the wire for three octets, hence 4/3. RFC 2045 §6.8 says so in as many words. The figure is asymptotic — small payloads pay worse, because padding adds a whole character for zero bits of input, so one octet becomes four characters and a 300% increase. Below about twenty bytes, quote the exact number rather than a third.

Exact encoded length and measured overhead
Input octetsOutput charactersOverheadInput octetsOutput charactersOverhead
14300%91233.3%
24100%121633.3%
3433.3%648837.5%
48100%1000133633.6%
5860%1024136833.6%

Output length for n octets is 4 * ceil(n / 3) with padding, ceil(n * 8 / 6) without. Text encoding and line wrapping add more on top.

octet
A single 8-bit byte. The unit of input. RFC 4648 always counts octets, not characters.
index
A 6-bit value from 0 to 63. The unit of output before it is rendered as a character.
alphabet
The table that maps an index to a character. §4 and §5 differ in exactly two rows.
variant
Which alphabet is in force: standard (plus and slash) or url (hyphen and underscore).
padding
The "=" filler that replaces output characters which had no input bits behind them.
canonical
An encoding whose leftover bits are zero, so the decoded value has exactly one spelling.

RFC 4648

Alphabets, padding and the two safe symbols

TL;DR. Standard base64 uses + and /; base64url swaps them for - and _ so the output is safe in URIs and filenames. Padding is a length signal, not data.

Section 4 defines the 64-character table: indices 0–25 are uppercase letters, 26–51 lowercase, 52–61 digits, 62 the plus sign and 63 the slash. The padding character is the equals sign.

index to character6 lines
 0 A    1 B    2 C    3 D  ...  16 Q   17 R   18 S   19 T   25 Z
 26 a   27 b   28 c   29 d  ...  46 u   47 v   48 w   49 x   51 z
 52 0   53 1   54 2   55 3  ...  60 8   61 9
 62 +   63 /        pad =

base64("foobar") = "Zm9vYmFy"      indices 25 38 61 47 24 38 5 50

These are zero-based positions, not character codes: A means 0 and a means 26. Base64 is case-sensitive — Q and q are indices 16 and 42.

The two final symbols are the interesting pair. Plus and slash are both reserved in URIs — slash is a path separator and plus means a space in form-encoded data. Section 5 is the answer for the web: index 62 becomes the hyphen and index 63 the underscore. That is the entire change; the bit grouping and padding rules are identical. Because hyphen and underscore are unreserved in RFC 3986 §2.3, a base64url value needs no percent-encoding in a path segment, query value or filename.

the same bytes in three spellings5 lines
bytes        = fb ff bf        (three octets chosen to hit indices 62 and 63)
standard     = +/+/            index 62 is "+",  index 63 is "/"
base64url    = -_-_            index 62 is "-",  index 63 is "_"
in a query   = "+" is read as a space by form decoders; "%2B%2F" is the safe spelling
in a path    = "/" splits the segment; "%2F" is decoded by some servers before routing

base64url is not a different encoding; it is the same encoding with the two characters that break transports removed.

The convention in practice is that base64url also drops padding. RFC 7515 defines the BASE64URL function used by JWS without the pad character, which is why a JSON Web Token never contains an equals sign. Reading base64url as standard base64 is the dangerous direction: a hyphen or underscore is not in the §4 alphabet, so a strict decoder errors while a lenient one ignores it and hands back a payload two or three octets short.

Padding is a length signal, not data

The complete padding rule
Octets mod 3Data characters in the last groupPad charactersExampleDecoded octets
0fournoneZm9v for "foo"3
1twotwoZg== for "f"1
2threeoneZm8= for "fo"2

A length of 4k + 1 can never occur, because one leftover character holds 6 bits and cannot carry a whole octet.

Padding is redundant information: the character count of an unpadded value determines its byte count exactly, which is why JWS omits it. The consequence surprises people who treat a base64 string as an identifier — the padded and unpadded spellings of the same payload are the same data, so a database of padded values cannot be joined against one of unpadded values without normalising first.

Why you would ever use base32

Base32 (§6) pays 60% instead of 33 to get an alphabet with no plus, no slash, and no glyph that looks like another glyph: no 0 confused with O, no 1 with I. That is why it is the choice for one-time-password secrets and identifiers a human may read aloud. Base32hex (§7) puts digits first, so encoded strings sort like the values behind them.

Five encodings from the same document
EncodingClauseSymbolsBits/charOverheadWhy you would pick it
base16§80-9 A-F4100%Hex dumps, anything read digit by digit
base32§6A-Z 2-7560%Spoken or typed codes; no confusing glyphs
base32hex§70-9 A-V560%Encoded values that must sort like their data
base64§4A-Z a-z 0-9 + /633%Default choice for binary in a text channel
base64url§5A-Z a-z 0-9 - _633%Same, but safe in URIs and file names

Only the alphabet changes between rows; the bit packing rule is the same. Overhead is 8/bits-per-char minus one.

Layout

Line wrapping: MIME rules and PEM armour

TL;DR. RFC 4648 base64 has no line breaks. MIME wraps at 76, PEM at exactly 64, JWS never wraps. Mixing those rules is how a certificate gets rejected.

Section 3.1 settles it for the core encoding: implementations MUST NOT add line feeds unless the referencing specification explicitly directs otherwise. Line breaks belong to the formats that embed base64, and each says something different.

Who wraps, at what width, and with which terminator
FormatClauseWrap widthTerminatorDecoder must ignore breaks?
RFC 4648 base64§3.1no wrapping at allnonenot applicable
MIME base64 CTERFC 2045 §6.8no more than 76 charactersCRLFyes — out-of-alphabet ignored
PEMRFC 7468 §2exactly 64 except the last linenewlineparsers SHOULD ignore whitespace
base64url / JWSRFC 7515 §7one line, alwaysnonea break would change the signed bytes

MIME says a maximum of 76; PEM says an exact 64. Treating those as the same rule is how a certificate file ends up rejected.

PEM armour over DER

PEM is not a key format. It is a transport wrapper: base64 with a labelled header and footer, defined by RFC 7468 §2. Between the armour sits DER — a binary encoding of ASN.1, so the payload of a PEM block is never text, however readable the base64 looks.

one key, three layers6 lines
-----BEGIN PRIVATE KEY-----        (1) PEM armour, RFC 7468 §2, label from §4
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcw...   (2) base64, wrapped at exactly 64
-----END PRIVATE KEY-----          characters per line, LF or CRLF

inside the base64:                 (3) DER-encoded ASN.1 PrivateKeyInfo
  SEQUENCE { version, AlgorithmIdentifier, OCTET STRING key }

Labels are meaningful: PRIVATE KEY is PKCS #8, RSA PRIVATE KEY is PKCS #1, CERTIFICATE is an X.509 RFC 5280 certificate.

Three traps live in that wrapper. First, diffing: parsers ignore whitespace, so two files with the same key can differ in line endings and both stay valid — text comparison answers the wrong question. Second, the label is a contract: BEGIN PRIVATE KEY and BEGIN RSA PRIVATE KEY describe different ASN.1 structures. Third, BEGIN and END labels must agree, because a mismatched footer is a truncated or spliced file.

§3.5

Canonical encoding and the leftover bits

TL;DR. A one-octet payload has sixteen valid-length encodings but only one canonical one. Compare decoded octets, never encoded text.

With one input octet the encoder needs twelve bits of capacity for eight bits of data, so four bits in the second character are not determined by the payload; with two octets, two bits of the third are free. Section 3.5 closes that hole: pad bits MUST be set to zero by conforming encoders, which gives exactly one encoding per value. Decoders MAY reject non-zero pad bits — most do not, which is why the ambiguity survives in the wild.

sixteen spellings, one octet10 lines
Zg==  011001 100000   pad bits 0000   canonical, decodes to 0x66
Zh==  011001 100001   pad bits 0001   decodes to 0x66
Zi==  011001 100010   pad bits 0010   decodes to 0x66
...
Zv==  011001 101111   pad bits 1111   decodes to 0x66

two-octet case, "fo" = 66 6F, canonical Zm8=:
Zm8=  pad bits 00   canonical
Zm9=  pad bits 01   same two octets
Zm/=  pad bits 11   same two octets

Each is accepted by a lenient decoder and produces identical bytes. Only the first is canonical.

The consequence: a cache keyed on encoded text stores the same object sixteen times; a signature over the encoded string is valid for one spelling and trivially transferable to the other fifteen; a validator that only checks whether a string is base64 accepts what a downstream library rejects — the classic normalisation-bypass shape.

Containers

Where Base64 actually shows up: data URIs and JWTs

TL;DR. Data URIs inline binary resources with a 33% overhead and no cache reuse. JWTs are three base64url segments joined by periods — the signature covers the encoded text, not the decoded JSON.

A data URI puts the bytes of a resource into a URL. RFC 2397 §3 gives the grammar: data, colon, an optional media type, an optional base64 indicator, a comma, and then the data. Without the semicolon-base64 marker the text after the comma is percent-encoded; with it, the text is base64 and the decoded octets are the resource.

the three forms, and what each one means4 lines
data:,Hello%20World                 plain text, percent-encoded, type defaults
data:text/plain;charset=UTF-8,caf%C3%A9    5 octets written as 15 characters
data:image/png;base64,iVBORw0KGgo...       PNG octets, 33% overhead, no escaping
data:image/svg+xml,%3Csvg ...%3E           percent form for text-based vectors

The semicolon-base64 marker is a flag on the whole document, not a charset. An inlined asset cannot be cached on its own and is re-sent with every document that references it.

JWTs and the JWS compact serialisation

A JSON Web Signature in compact form is three base64url values joined by periods. The encoding is the §5 alphabet (no plus, no slash), and the BASE64URL function omits padding, so an equals sign never appears — which makes segment length modulo four one of the fastest sanity checks available.

segment anatomy8 lines
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 . eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ . SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

header     {"alg":"HS256","typ":"JWT"}     36 chars, 36 mod 4 = 0, decodes to 27 octets
payload    sub, name, iat                 74 chars, 74 mod 4 = 2, decodes to 55 octets
signature  32 raw bytes                    43 chars, 43 mod 4 = 3, never parsed as JSON

signing input (RFC 7515 §5.1) = ASCII(BASE64URL(UTF8(header)) + "." + BASE64URL(payload))
the signature covers that ASCII string, not the decoded JSON

Because the signature is computed over the encoded text, editing a claim and re-encoding breaks verification. An alg value you did not choose is not a hint — RFC 8725 §3.1 requires verifiers to check the algorithm.

The length arithmetic tells you what a truncated token means: 4k+1 is impossible, so a segment of that length gained or lost a character in transit rather than being signed differently. Reading the segments needs no key, so anything confidential inside a JWS belongs in a JWE or behind a server-side reference.

In practice

Decoding it in code, and identifying the bytes

TL;DR. btoa takes a Latin-1 string, not text — use TextEncoder first. Decode with fatal: true to turn silent corruption into an exception.

The Web Platform defines two base64 functions whose type signature is the source of much confusion. btoa takes a string and treats each character code as one byte; atob returns a string where each character has a code point below 256. Since btoa interprets code points as byte values, it throws InvalidCharacterError as soon as any code point exceeds 0xFF — the argument is not text, it is a byte array wearing a string costume. The honest version is three explicit steps.

the round trip that does not lose data13 lines
const bytesToBase64 = (bytes) =>
  btoa(String.fromCodePoint(...bytes));              // one code point per octet

const base64ToBytes = (value) =>
  Uint8Array.from(atob(value), (c) => c.codePointAt(0));

const text = 'a \u0100 \u{10000} \u6587';        // 4 code points, 13 octets

const encoded = bytesToBase64(new TextEncoder().encode(text));
// "YSDEgCDwkICAIOaWhw=="   20 characters, two pad signs

new TextDecoder('utf-8', { fatal: true }).decode(base64ToBytes(encoded));
// back to the original string, or a TypeError rather than mojibake

TextEncoder produces UTF-8 octets; String.fromCodePoint over those octets makes the Latin-1 string btoa expects. The spread operator caps argument count, so a production version loops in chunks.

The failure is asymmetric: encoding text with code points above 0xFF throws, so you notice; decoding a Latin-1 payload as UTF-8 does not, because the platform decoder replaces invalid sequences with U+FFFD and hands back a string of the right shape and the wrong content. Only a fatal decoder tells binary bytes apart from text it cannot read.

Identifying the decoded bytes

Base64 carries no description of its payload, so the format of a decoded result has to come from somewhere. A filename is a guess and a declared media type is a claim; the only evidence in the payload itself is its leading octets.

Common signature openings, in the order a prefix matcher tries them
Leading octetsReported asExtensionNote
89 50 4e 47PNG imagepngUnambiguous; the first four bytes of every PNG
ff d8 ffJPEG imagejpgStart of Image followed by a marker segment
47 49 46 38GIF imagegifThe GIF87a or GIF89a magic
52 49 46 46RIFF containerwebpAlso WAV and AVI — container is certain, codec is not
50 4b 03 04ZIP archivezipDOCX, XLSX, APK and EPUB are all ZIP files
1f 8bgzip streamgzCommonly seen inside a base64 field that claims to be plain text
25 50 44 46 2dPDF documentpdfThe literal ASCII of %PDF-
7f 45 4c 46ELF executableelfA binary in a data URI should end the conversation
4d 5aWindows PEexeTwo bytes only, so expect occasional false positives
42 4dBitmap imagebmpAlso the ASCII of B and M, so short values deserve a second look
7b 22JSON objectjsonA two-byte heuristic — brace plus quote
3cXML / HTML / SVGxmlOne byte: all three start with a less-than sign

Matching is a lowercase hex prefix test against the decoded bytes. The first match wins. Absence of a match is not evidence of a problem — use this to catch a mismatch between what a field claims and what it carries.

This site

How this site handles these cases

TL;DR. The encoder exposes every decision — variant, padding, line length, input encoding — instead of burying it. The decoder reports measurements plus a diagnostic list, and keeps warnings, errors and informational notes apart.

You choose the variant, whether to pad, a line length, and how to read the input. UTF-8 is the default; Latin-1 is the btoa-compatible mode; hex input is validated separately, rejecting non-hex characters and an odd digit count rather than silently dropping the trailing nibble. The result carries the arithmetic: byte count, character count, group count, padded count and overhead percentage.

Variant detection is honest about its limits: a value made only of letters and digits is legal in both alphabets and cannot be classified from its contents, so the decoder looks for distinguishing characters and otherwise decodes as standard with hyphen and underscore mapped back. The decode report counts whitespace rather than swallowing it, names how many characters were ignored, and offers strict mode that turns the same input into an error — usable as a linter for values that are functionally fine and contractually wrong. Arithmetic faults get their own codes: pad before data, more than two pad characters, length congruent to 1 mod 4, characters outside the alphabet. A non-canonical tail is a warning with the §3.5 citation, and a round-trip flag records whether re-encoding reproduces the input.

The container work follows the same pattern. A value that splits into three base64url segments is recognised as a JWT: header and payload are decoded and parsed as JSON, claims like exp/iat/nbf get ISO timestamps, a numeric claim above 4102444800 is divided by a thousand so a millisecond expiry displays as a date, and the signature segment is measured but never verified — nothing in the browser holds your key. A value beginning with data: reports the media type and base64 flag. A PEM block is found by a matched BEGIN/END pair, so a mismatched footer is not treated as PEM.

Answers

Questions about Base64

Why did my Base64 decode to garbage?

Work through the common causes in order. A hyphen or underscore means you are holding base64url, and a lenient standard decoder silently drops those characters. A plus that became a space came through a query string. If the length is right but the text is wrong, the bytes were UTF-8 and you read them as Latin-1 or the reverse — a fatal UTF-8 decode turns that into an exception instead of replacement characters.

Can I store a secret as Base64?

You can store it, but you cannot keep it. A base64 value in a config file, cookie or log is readable by anyone who can see it, and the character shape is no barrier to a grep for credentials. Encode a secret only to move it through a text channel. Where it has to rest, keep it in a keystore or secret manager and store a reference.

Is there a maximum size for Base64 data?

The encoding itself has no ceiling: it is a streaming regrouping, so the length is always 4 * ceil(n / 3) characters. Every limit you hit belongs to the carrier: query strings, cookies, mail and proxy line limits, the parsed weight of an HTML document that inlines a data URI, and the argument-count cap that makes a naive browser helper throw RangeError on large input.

Which Base64 variant should I pick for a new format?

Pick by transport, not by taste. Standard base64 with padding is right for MIME, PEM and data URIs. base64url without padding is right for anything in a path segment, query value, cookie or filename, because hyphen and underscore are unreserved. base32 is worth its 60% overhead only when a human reads the value aloud or types it.

Can a Base64 value be corrupted without anyone noticing?

Yes. There is no checksum and no parity, so one flipped character silently produces different bytes, and characters dropped or added in transport usually still decode. If integrity matters, put a MAC or a hash over the decoded octets and verify it — which is exactly what signed tokens do around the encoded part.

Should I store Base64 in a text column or decode it first?

Store the decoded octets when the value is really data — a key, an image, an opaque identifier — because the text form costs a third more and any comparison over encoded text has to agree on one spelling of what may legally have fifteen. Keep the encoded form when the text itself is the artefact: a token segment whose signature covers the encoded bytes.

Related

Next up

Sources and further reading

  1. [1]RFC 4648 — The Base16, Base32, and Base64 Data Encodings — §3.1 line feeds, §3.2 padding, §3.3 non-alphabet characters, §3.5 canonical encoding, §4 base64, §5 base64url, §6 base32, §7 base32hex, §12 security. (accessed November 12, 2026)
  2. [2]RFC 2045 — MIME Part One, §6.8 Base64 Content-Transfer-Encoding — 76-character line rule, ignore-outside-alphabet decoder rule, "about 33 percent larger". (accessed November 12, 2026)
  3. [3]RFC 7468 — Text Encodings of PKCS #8 and Other Objects (PEM) — BEGIN/END label structure, 64 chars per line, parsers SHOULD ignore whitespace. (accessed November 12, 2026)
  4. [4]RFC 2397 — The "data" URL scheme — dataurl grammar, default text/plain US-ASCII. (accessed November 12, 2026)
  5. [5]RFC 7515 — JSON Web Signature (JWS) — §7 compact serialisation with BASE64URL; §5.1 signing input. (accessed November 12, 2026)
  6. [6]RFC 8725 — JSON Web Token Best Current Practices — §3.1 reject unexpected alg rather than trusting the header. (accessed November 12, 2026)
  7. [7]RFC 3986 — Uniform Resource Identifier: Generic Syntax — §2.3 unreserved characters (hyphen, period, underscore, tilde). (accessed November 12, 2026)
  8. [8]MDN — Window.btoa() / atob() — Latin-1 constraint, InvalidCharacterError, TextEncoder/TextDecoder pattern. (accessed November 12, 2026)
  9. [9]RFC 8259 — JSON Data Interchange Format — §7 forces control-character escaping, §8.1 fixes UTF-8 — why binary must be wrapped. (accessed November 12, 2026)