What is Base64?
Base64 is an encoding scheme used to convert binary data into a text format. It represents data using 64 ASCII characters, which include uppercase letters (A-Z), lowercase letters (a-z), numbers (0-9), and two additional symbols (typically + and /). The = symbol is used for padding at the end.
How does it work?
Base64 works by taking binary data and dividing it into 6-bit chunks. Each 6-bit chunk is then mapped to one of the 64 characters in the Base64 alphabet. Since 6 bits can represent 64 different values (2⁶ = 64), this allows any binary data to be safely represented as text.
Here’s a simplified example:
- Take the text
"Hi"in ASCII →H = 72andi = 105→ binary:01001000 01101001. - Combine into a single binary stream:
0100100001101001. - Split into 6-bit chunks:
010010 000110 1001(add padding to make full 6-bit groups). - Map each chunk to a Base64 character →
"SGk=".
What is Base64 used for?
Base64 is commonly used to encode data so it can be safely transmitted or stored in systems that only support text, such as:
- Email attachments (MIME encoding)
- Embedding images or files in HTML, CSS, or JSON
- Encoding authentication tokens or API keys in URLs
- Storing binary data in databases that support only text fields
It’s important to note that Base64 is not encryption. It only transforms data into a text-friendly format and can be easily decoded back to the original binary form.