Base64 Encoding

A way to carry any bytes through systems that only accept text, by writing every 6 bits as one of 64 safe characters.

Why Base64 exists

Many older protocols and formats were built to carry text, not arbitrary bytes. Email bodies, HTTP headers, XML attributes and JSON strings all expect printable characters, and some systems along the way may strip the high bit, rewrite line endings or choke on a zero byte. Raw binary data such as an image or a cryptographic key does not survive that trip reliably.

Base64 solves this by mapping any sequence of bytes onto 64 characters that are safe almost everywhere: the uppercase letters, the lowercase letters, the ten digits, and + and /. The = sign is used for padding at the end. The scheme is defined in RFC 4648, and the decoder reverses it exactly, so no information is lost.

The 64-character table

Each character stands for a 6-bit number from 0 to 63. Six bits is the largest group that fits in a set of 64 symbols, because 2 to the power 6 is 64.

ValuesCharacters
0 to 25A to Z
26 to 51a to z
52 to 610 to 9
62+
63/

Step by step: "Man" becomes "TWFu"

The encoder works on three bytes at a time, because three bytes are 24 bits and 24 splits evenly into four 6-bit groups. Take the ASCII text Man. First write each byte in binary (the page on number bases covers this conversion):

CharacterByte valueBits
M7701001101
a9701100001
n11001101110

Join the bits into one 24-bit string, 010011010110000101101110, and cut it into four groups of six. Look up each group's value in the table:

6 bitsValueCharacter
01001119T
01011022W
0001015F
10111046u

The result is TWFu. Three input bytes always produce four output characters. Decoding runs the same steps backwards: look up each character's value, join the 6-bit groups and cut them into bytes again.

Padding with =

Input rarely comes in an exact multiple of three bytes. When one or two bytes are left over, the encoder adds zero bits to fill the last 6-bit group, then appends = signs so the output length stays a multiple of four. One leftover byte gives two characters and ==; two leftover bytes give three characters and =.

InputBytes6-bit groups (zero-filled)Base64
M1010011 010000TQ==
Ma2010011 010110 000100TWE=
Man3010011 010110 000101 101110TWFu

The padding tells a decoder how many real bytes the final group holds. Some formats drop it because the length of the string already gives that away, and many decoders accept unpadded input. Others are strict and reject it, so check what the receiving side expects.

Size overhead

Every 3 bytes become 4 characters, so encoded data is about four thirds the size of the original, roughly one third larger. The exact length is 4 times the input length divided by 3 and rounded up. These lengths come from Python's base64.b64encode:

Input bytesBase64 characters
14
24
34
3040
100136
10001336
30004000

Some encoders also insert a line break every 76 characters, as MIME email requires, which adds a little more. That overhead is why embedding large images as Base64 in HTML or CSS usually makes pages heavier, not lighter.

The base64url variant

Two characters in the standard set, + and /, have special meaning in URLs and file names. The base64url variant, also defined in RFC 4648, replaces them with - and _. Everything else stays the same. For the six bytes FA FB FC FD FE FF, standard Base64 gives +vv8/f7/ while base64url gives -vv8_f7_.

JSON Web Tokens use base64url and leave out the padding. A standard decoder will fail on such a string, so convert - and _ back and restore the = signs first, or use a decoder built for the URL-safe form. Do not confuse this with URL percent-encoding, which is a different scheme.

Base64 is not encryption

Base64 has no key and no secret. Anyone can decode SGVsbG8sIHdvcmxkIQ== back to Hello, world! with a single standard library call. It hides nothing from a person who looks. Credentials in an HTTP Basic Authorization header, for instance, are only Base64-encoded, which is why that scheme must run over HTTPS. If data needs to be secret, encrypt it; if it needs to be tamper-proof, sign it. Base64 may then be used to carry the encrypted or signed bytes as text.

A data: URI example

A data: URI puts a small file directly inside a URL. The part after the comma is the content, and the ;base64 flag says it is encoded. The text Hello! as a plain-text data URI is:

data:text/plain;base64,SGVsbG8h

Browsers accept this form for small icons and fonts. Keep it for tiny assets; larger files are cached and compressed more effectively as separate requests.

Quick checklist

  • Output length should be a multiple of 4 when padding is used. If it is not, the string was cut short or the padding was stripped.
  • Seeing - or _ means base64url; seeing + or / means the standard set. Use the matching decoder.
  • Whitespace and line breaks inside the string may need removing before a strict decoder will accept it.
  • After decoding text, you still have bytes. Decode them with the right character encoding, usually UTF-8.
  • Expect roughly one third more size, and never treat the output as protected.