What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It encodes data using 64 printable characters (A-Z, a-z, 0-9, +, /) making it safe for transmission over text-based protocols.
How It Works
Base64 encoding converts binary data (8-bit bytes) into groups of 6 bits, then maps each 6-bit group to one of 64 printable characters:
- Input: Binary data split into 24-bit groups (3 bytes)
- Process: Each 24-bit group split into four 6-bit groups
- Output: Each 6-bit group mapped to a Base64 character
- Padding: '=' characters added if input isn't divisible by 3
- Example: "Hello" โ "SGVsbG8=" (5 bytes โ 8 characters)
Common Use Cases
Base64 encoding is widely used in modern computing:
- Email (MIME): Encoding attachments for safe transmission
- Data URLs: Embedding images directly in HTML/CSS
- Basic Authentication: HTTP Basic Auth encodes credentials
- JSON/XML: Embedding binary data in text formats
- Cryptography: Encoding keys, certificates, and encrypted data
- URL Parameters: Safely passing binary data in URLs
Standard vs URL-Safe Base64
There are two main Base64 variants:
- Standard (RFC 4648): Uses +, /, and = characters
- URL-Safe (RFC 4648 ยง5): Uses -, _, and no padding
URL-safe Base64 replaces + with - and / with _ to prevent issues when encoding data for URLs, where + and / have special meanings.
Size Overhead
Base64 encoding increases data size by approximately 33%:
- Every 3 bytes of input โ 4 bytes of output
- 1000 bytes of binary โ ~1333 bytes of Base64
- Padding characters may add 1-2 extra bytes
Encoding Process Example
Let's encode "Hi!" step by step:
i = 01101001 (105)
! = 00100001 (33)
000110 = G (6)
100100 = k (36)
100001 = h (33)
SGkh
Security Note
Important: Base64 is an encoding method, not encryption. It does not provide any security or privacy. Anyone can decode Base64 data instantly. Never use Base64 alone to protect sensitive information!