What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's commonly used to encode data that needs to be stored or transferred over media designed to handle text, ensuring data remains intact without modification during transport.
Common Use Cases
- Email Attachments: MIME email protocol uses Base64 to encode binary attachments
- Data URLs: Embed images and files directly in HTML/CSS using data URIs
- API Authentication: Basic authentication headers use Base64 encoding
- JSON Web Tokens: JWT tokens use Base64URL encoding
- XML/JSON Data: Encode binary data for transmission in text-based formats
- Configuration Files: Store binary data in text configuration files
How Base64 Works
Base64 encoding converts binary data into a set of 64 ASCII characters (A-Z, a-z, 0-9, +, /). Every 3 bytes of input data is converted into 4 Base64 characters. If the input length isn't divisible by 3, padding characters (=) are added to make it so.
Important Notes
- Base64 is encoding, not encryption - it doesn't provide security
- Encoded data is approximately 33% larger than the original
- Base64 is reversible - anyone can decode it
- Use Base64URL variant for URLs (replaces + with - and / with _)
- Always validate and sanitize decoded data for security
Example
Input: Hello, World!
Base64: SGVsbG8sIFdvcmxkIQ==