Base64 is an encoding scheme that converts binary data (bytes) into a string of printable ASCII characters. It was designed to solve a specific problem: many systems that handle text can't reliably transmit raw binary data — email was an early example. Base64 converts binary into text characters that can safely pass through any text-based system.
Why Base64 Exists
Binary files (images, documents, executables) contain byte values that can conflict with control characters in text protocols. Email, for instance, uses certain byte patterns to mark message boundaries. Raw binary data containing those patterns gets corrupted.
Base64 converts every sequence of 3 bytes into 4 ASCII characters, using only 64 safe characters: A–Z, a–z, 0–9, + and / (plus = for padding). Any text system can handle these safely.
When You'll Encounter Base64
- Email attachments: MIME encoding uses Base64 for attachments.
- Data URLs: images embedded directly in HTML or CSS:
data:image/png;base64,iVBORw0KGgo... - JWT tokens: the three sections of a JSON Web Token are Base64-encoded.
- API authentication: Basic authentication encodes
username:passwordin Base64. - Storing binary data in JSON: JSON can't contain raw binary; Base64 converts it to a string.
- Font files in CSS: web font data embedded in stylesheets.
A Short Example
The text "Hello" in Base64 is SGVsbG8=
Decoding SGVsbG8= gives back "Hello"
Base64 Is Not Security
A common misunderstanding: Base64 provides no security. Anyone can decode it instantly. When you see Basic authentication credentials encoded in Base64, they are not protected — Basic auth must be transmitted over HTTPS to be secure.
Encoding and Decoding
Use Base64 Encoder/Decoder to:
- Encode any text or paste data for transmission.
- Decode Base64 strings you've received.
- Convert files to Base64 for embedding.
For URL-encoded content (the %20 and similar patterns seen in URLs), that's a different encoding — see URL encoding explained: why %20 appears in links.
Base64 in Laravel and PHP
In PHP and Laravel, base64_encode() and base64_decode() handle encoding and decoding. A common use is embedding images in email templates so they don't require external hosting: convert the image to Base64, embed it as a data URI in the HTML, and the email client renders it without an external request. For JWT tokens (which use Base64URL — a slightly modified version), see how JWT tokens work. For any Base64 conversion without code, Base64 Encoder/Decoder handles encoding and decoding directly in the browser.
URL-Safe Base64
Standard Base64 uses + and / which have special meanings in URLs. URL-safe Base64 (Base64URL) replaces + with - and / with _, and typically omits the = padding. JWT tokens use Base64URL for their header and payload sections. When building APIs or processing tokens, confirm which variant is in use before encoding or decoding. Base64 Encoder/Decoder supports both standard and URL-safe Base64 modes.