What Is Base64 Encoding?
Base64 is a way to write any binary data, such as an image, a PDF or a block of encrypted bytes, using only 64 printable characters: A-Z, a-z, 0-9, + and /. Because the output is plain text, it can travel through systems that only handle text, like email, JSON, XML, HTML and URLs. This tool encodes and decodes Base64 in your browser, for both text and files.
How Base64 Works
The encoder reads the input three bytes at a time. Three bytes are 24 bits, which it splits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and that number picks one character from the 64-character alphabet. So every 3 bytes become 4 characters, which makes Base64 output about 33% larger than the original.
When the input length is not a multiple of 3, the last group is short, and one or two = characters are added as padding. The text hello is 5 bytes and encodes to aGVsbG8=.
Common Uses of Base64
- Data URIs: embed a small image directly in HTML or CSS, such as
src="data:image/png;base64,...". - Email attachments: MIME encodes attachments as Base64 so they survive text-only mail systems.
- JWTs: the header and payload of a JSON Web Token are URL-safe Base64 strings.
- HTTP Basic auth: the
Authorizationheader carriesusername:passwordencoded as Base64. - Binary inside JSON: JSON has no binary type, so APIs often send files as Base64 strings.
URL-Safe Base64
The standard alphabet uses + and /, which have special meaning in URLs and file paths, and = padding, which also needs escaping in a query string. URL-safe Base64 swaps + for - and / for _, and usually drops the padding. Tick the URL-safe box to encode this way. The decoder accepts either alphabet, with or without padding.
Base64 Is Encoding, Not Encryption
Base64 has no key and no secret. Anyone can reverse it in a second, as this page shows. Never use it to hide passwords, tokens or personal data. Its only job is to make binary data safe to store or send as text.
Standard vs URL-Safe Base64
| Feature | Standard | URL-safe |
|---|---|---|
| Last two characters | + and / | - and _ |
| Padding | = to a multiple of 4 | Usually omitted |
| Defined in | RFC 4648, section 4 | RFC 4648, section 5 |
| Safe in URLs and filenames | No | Yes |
| Typical use | MIME email, data URIs | JWTs, URL parameters |
Base64 in JavaScript
The built-in btoa() and atob() only work on Latin-1 strings, so btoa() throws an error on emoji and most non-Latin text. Convert to UTF-8 bytes first:
const bytes = new TextEncoder().encode('héllo 👋');
const b64 = btoa(String.fromCharCode(...bytes));
const bin = atob(b64);
const text = new TextDecoder().decode(Uint8Array.from(bin, c => c.charCodeAt(0)));
In Node.js, use Buffer.from(text).toString('base64') and Buffer.from(b64, 'base64').toString(). This tool does the same UTF-8 step for you.
Base64 in Python
import base64
encoded = base64.b64encode('héllo'.encode('utf-8')) # b'aMOpbGxv'
decoded = base64.b64decode(encoded).decode('utf-8') # 'héllo'
safe = base64.urlsafe_b64encode(b'\xfb\xff') # b'-_8='
Base64 on the Command Line
macOS and Linux both ship a base64 command. Use printf rather than echo so a trailing newline does not get encoded too:
printf 'hello' | base64 # aGVsbG8=
printf 'aGVsbG8=' | base64 -d # hello
base64 photo.png > photo.txt # encode a file
base64 -d photo.txt > photo.png # decode it again
Linux wraps encoded output at 76 characters, so add -w 0 for a single line. macOS does not wrap by default.