What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that converts binary data into a string of ASCII characters. It uses 64 printable characters to represent binary data, making it safe for transmission over text-based protocols that might otherwise corrupt binary data.
The name "Base64" comes from the fact that it uses a 64-character alphabet. Each Base64 character represents exactly 6 bits of data, so 4 Base64 characters encode 3 bytes (24 bits) of original data.
How Base64 Encoding Works
The encoding process follows these steps:
- Convert to binary: The input data is converted to its binary representation (sequence of 0s and 1s).
- Split into 6-bit groups: The binary data is divided into groups of 6 bits each.
- Map to characters: Each 6-bit group (values 0-63) is mapped to a character from the Base64 alphabet.
- Add padding: If the input length isn't divisible by 3, padding characters (=) are added to make the output length divisible by 4.
When to Use Base64
- Email attachments: MIME encoding uses Base64 to embed binary files in emails.
- Data URIs: Embed images directly in HTML/CSS without separate file requests.
- API payloads: Safely include binary data in JSON or XML documents.
- Basic authentication: HTTP Basic Auth encodes credentials in Base64.
- Storing binary in text: Databases or config files that only support text.
Base64 is NOT Encryption
A common misconception is that Base64 provides security. It does not. Base64 is an encoding scheme, not encryption. Anyone can decode Base64 data instantly — it provides zero security.
Never use Base64 to "hide" sensitive information like passwords, API keys, or personal data. If you need to protect data, use proper encryption algorithms like AES or RSA.