What is Base64?
Base64 is an encoding scheme that converts binary data into a string using 64 printable ASCII characters (AβZ, aβz, 0β9, +, /). It was designed to safely transmit binary data over text-based systems like SMTP email and HTTP headers that weren't built to handle raw bytes.
The name comes from the 64 characters used. Each character encodes 6 bits (2βΆ = 64). The output is always about 33% larger than the original.
How encoding works
- Take the binary input (bytes of an image, string, or file).
- Group into 3-byte chunks (24 bits each).
- Split each 24-bit chunk into four 6-bit groups.
- Map each 6-bit group to one of the 64 Base64 characters.
- Pad with
=characters if the input length isn't divisible by 3.
Example: the string Hi! encodes to SGkh. You can verify this in ToolBite's Base64 encoder.
Common use cases
- API authentication β Basic Auth sends
username:passwordencoded in Base64 in the Authorization header. - JWT tokens β Header and payload sections are Base64url-encoded (a URL-safe variant).
- Data URIs β Embedding small images directly in HTML:
src="data:image/png;base64,iVBORw..." - Email attachments β MIME uses Base64 to encode binary attachments for SMTP transit.
- Binary in JSON β JSON is text-only; binary data (files, images, keys) is Base64-encoded before storing in JSON fields.
Base64 is NOT encryption
Seeing a Base64 string doesn't mean the data is encrypted β it's just encoded for safe transport. Anyone can decode it in one second. For actual security use proper encryption (AES-256) or hashing (SHA-256, bcrypt for passwords).
Base64 vs Base64url
Comparison
- Base64 uses
+and/β these are special in URLs and need percent-encoding. - Base64url replaces
+with-and/with_β safe in URLs and HTTP headers without escaping. - JWT tokens use Base64url. Data URIs use standard Base64.
JavaScript quick reference
- Encode:
btoa(str)β works for ASCII strings only. - Decode:
atob(str)β returns the original string. - For Unicode:
btoa(unescape(encodeURIComponent(str))). - For files/binary: use
FileReader.readAsDataURL().
Quick reference facts
- Base64 output is ~33% larger than input.
- Strings end with 0, 1, or 2 padding
=characters. - Base64 β encryption β never use it for security.
- JWT tokens use Base64url (not standard Base64).