How to encode or decode Base64
- Text: type or paste into the Input box and choose Encode, Encode (URL-safe) or Decode. Text is handled as UTF-8, so accents and emoji work.
- Files and images: open File to Base64 and drop the file to get its Base64 string and a data URI.
- Base64 to file: paste the Base64 and choose Decode. If it's a file rather than text, a preview and a Download button appear.
- Use Encode each line separately for lists, or Wrap at 76 characters for email (MIME) and PEM-style output.
How Base64 works
Base64 reads data 3 bytes (24 bits) at a time and splits them into four 6-bit groups. Each group is a number from 0 to 63,
which picks one character from the alphabet A–Z a–z 0–9 + /. Here's Man:
| Step | M | a | n |
|---|---|---|---|
| Bytes | 77 | 97 | 110 |
| Bits | 01001101 | 01100001 | 01101110 |
Regrouped into 6 bits: 010011 010110 000101 101110 = 19, 22, 5, 46, which are the characters
TWFu. When the input isn't a multiple of 3 bytes, the output is padded with
=: Ma becomes TWE= and M becomes TQ==.
Where Base64 is used
| Use | Example |
|---|---|
| Email attachments (MIME) | Files are Base64-encoded and wrapped at 76 characters per line |
| Data URIs | <img src="data:image/png;base64,iVBORw0…"> embeds small icons in HTML or CSS |
| HTTP Basic authentication | Authorization: Basic dXNlcjpwYXNz is user:pass encoded, not encrypted |
| JSON Web Tokens | The header and payload are URL-safe Base64 |
| Certificates and keys (PEM) | The text between -----BEGIN CERTIFICATE----- lines |
| APIs and JSON | Sending images or files inside a JSON field |
Standard vs URL-safe Base64
| Standard | URL-safe (Base64URL) | |
|---|---|---|
| Characters 62 and 63 | + and / | - and _ |
| Padding | = required | Usually omitted |
| Used in | Email, data URIs, PEM files | URLs, file names, JWTs |
Base64 in code and on the command line
| Where | Encode | Decode |
|---|---|---|
| JavaScript | btoa(str) (Latin-1 only; use TextEncoder for UTF-8) | atob(b64) |
| Python | base64.b64encode(data) | base64.b64decode(b64) |
| C# / .NET | Convert.ToBase64String(bytes) | Convert.FromBase64String(b64) |
| Java | Base64.getEncoder().encodeToString(bytes) | Base64.getDecoder().decode(b64) |
| PHP | base64_encode($data) | base64_decode($b64) |
| Linux / macOS | base64 file.png | base64 -d file.txt > file.png |
| Windows PowerShell | [Convert]::ToBase64String([IO.File]::ReadAllBytes("file.png")) | [IO.File]::WriteAllBytes("file.png", [Convert]::FromBase64String($b64)) |
When not to use Base64
- For security. Base64 hides nothing. Encrypt secrets and hash passwords instead.
- For large images on web pages. Embedded images are 33% larger, can't be cached separately and block rendering. Keep data URIs for tiny icons under a few KB.
- For compression. It always makes data bigger. Compress first (for example with gzip), then encode if you must send it as text.
Need to escape text for a URL or a web page instead? Use the URL encoder or the HTML entity encoder.
Comments 0
No comments yet.