What is Base64 Encoding?
Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format. It translates the data into a radix-64 representation. In simpler terms, it takes 8-bit computer bytes and converts them into an alphabet of 64 standard, printable characters (A-Z, a-z, 0-9, +, and /).
Why do Developers use Base64?
- Safe Data Transfer: Many older protocols (like SMTP for email) were only designed to handle text, not raw binary data (like images or compiled files). Base64 converts that complex data into safe text so it doesn't get corrupted in transit.
- Data URIs in Web Dev: Web developers often use Base64 to embed small images or fonts directly inside HTML or CSS files. This reduces the number of HTTP requests a browser has to make, potentially speeding up initial page loads.
- Basic Authentication: HTTP Basic Auth passes credentials via the HTTP header as a Base64 encoded string of
username:password. Note: This is not encrypted, which is why HTTPS is required to protect it! - JSON Web Tokens (JWT): The header and payload of a standard JWT are encoded using the URL-safe variant of Base64.
Standard vs. URL-Safe Base64
Standard Base64 outputs padding characters (=) and utilizes the plus (+) and slash (/) symbols. If you try to pass these characters inside a web URL query string, the browser will interpret them as command operators, breaking your link. URL-Safe Base64 replaces the plus with a hyphen (-) and the slash with an underscore (_), while usually stripping the padding, ensuring your data survives the URL routing process.