Base64
A binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation.
Base64 is a binary-to-text encoding scheme that is commonly used to encode binary data (like images, documents, or cryptographic keys) into an ASCII string format. This is particularly useful when transferring data over media that are designed to deal with text rather than binary data, such as email (SMTP) or HTTP headers.
How it works
The term "Base64" comes from the fact that it uses 64 characters to represent data. These 64 characters are typically:
- 26 uppercase letters (
A-Z) - 26 lowercase letters (
a-z) - 10 digits (
0-9) - 2 symbols (usually
+and/)
An equal sign (=) is used as a padding character at the end of the encoded string if the original binary data wasn't a multiple of 3 bytes.
Base64 works by taking 3 bytes (24 bits) of binary data and dividing it into 4 chunks of 6 bits each. Since 2^6 is 64, each 6-bit chunk can represent a value from 0 to 63, which maps directly to one of the 64 characters in the Base64 alphabet.
Base64 vs Base64URL
Standard Base64 contains the + and / characters, which have special meanings in URLs and file systems. Base64URL is a modified version of Base64 where:
+is replaced by-(hyphen)/is replaced by_(underscore)- Padding (
=) is often omitted
Base64URL is used extensively in technologies like JSON Web Tokens (JWT) to ensure the token can be passed safely via URLs without requiring additional URL encoding.