How Base64 Encoding Works (RFC 4648 Specification)
Base64 is a binary-to-text encoding scheme defined by IETF RFC 4648. It was engineered to transmit arbitrary binary data (including imagery, cryptographic keys, tokens, and compressed files) safely across communication channels historically built exclusively for 7-bit ASCII plain text, such as SMTP email and HTTP headers.
1. The Mathematics of 6-Bit Encoding
Base64 groups incoming streams of 3 bytes (24 total bits) and subdivides them into 4 chunks of 6 bits each. Because 2^6 = 64, each 6-bit chunk maps directly to a standard 64-symbol alphabet:
- Uppercase letters
AthroughZ(indices 0 to 25) - Lowercase letters
athroughz(indices 26 to 51) - Numeric digits
0through9(indices 52 to 61) - Symbols
+and/(indices 62 and 63) - Padding: the equal sign
=appended when the input byte count is not an exact multiple of 3.
As a mathematical consequence of expanding 3 bytes into 4 characters, Base64 encoding imposes approximately 33% payload overhead compared to raw binary transfer.
2. What is URL-Safe Base64 (Base64Url)?
In web URLs, query parameters, and JSON Web Tokens (JWT), the traditional + and / characters collide with URL syntax (where + represents a space). The Base64Url variant substitutes + with hyphen (-) and / with underscore (_), omitting padding characters.