Base64 is a method of encoding binary data into a text format. It is widely used in web development, email protocols, and APIs. Let's explore how it works.
How Does Base64 Work?
Base64 converts every 3 bytes of input data into 4 text characters from the alphabet: A-Z, a-z, 0-9, +, and /. The = sign is used for padding.
For example: Hello -> SGVsbG8=
Where Is Base64 Used?
- Data URI β embedding images directly in HTML/CSS:
data:image/png;base64,... - Email (MIME) β transferring attachments via the text-based SMTP protocol
- JWT Tokens β header and payload are encoded in Base64url
- API Authentication β HTTP Basic Auth:
Authorization: Basic base64(user:pass) - Storing Binary Data β in JSON, XML, and other text formats
Online Tool
Use Base64 Encoder/Decoder Xuvero for instant encoding and decoding. It supports both text and files.
Encoding in Different Languages
JavaScript
// Encoding
btoa("Hello World"); // "SGVsbG8gV29ybGQ="
// Decoding
atob("SGVsbG8gV29ybGQ="); // "Hello World"
Python
import base64
# Encoding
base64.b64encode(b"Hello World").decode() # "SGVsbG8gV29ybGQ="
# Decoding
base64.b64decode("SGVsbG8gV29ybGQ=").decode() # "Hello World"
PHP
// Encoding
base64_encode("Hello World"); // "SGVsbG8gV29ybGQ="
// Decoding
base64_decode("SGVsbG8gV29ybGQ="); // "Hello World"
Base64 vs Base64url
Standard Base64 uses the characters + and /, which have special significance in URLs. Base64url replaces them with - and _, making it safe for URLs and file names.
Important: Base64 Is NOT Encryption!
Base64 is encoding, not encryption. Anyone can decode a Base64 string. Never use Base64 to protect passwords or sensitive data.