Base64 Encode/Decode

Convert text and files to Base64 or restore Base64 to original data.

Base64 Encoding
Convert text or files to Base64
Length: 0 charactersSize: 0 bytes

What is Base64?

Base64 is an encoding method that represents binary data in ASCII string format. It's used for email attachments, embedding images in HTML, storing complex data in XML or JSON, and more.

Base64 uses only 64 safe characters (A-Z, a-z, 0-9, +, /) to convert any binary data to text. This ensures data can be safely transmitted between various systems.

Common Uses of Base64

  • Email Attachments: Converting binary files to text for transmission via MIME protocol.
  • Data URLs: Embedding images or fonts directly in HTML or CSS (e.g., data:image/png;base64,...).
  • API Communication: Transferring binary data in text-based formats like JSON or XML.
  • Cookies and Web Storage: Safely storing information containing binary data or special characters.
  • JWT (JSON Web Tokens): Encoding authentication information for secure transmission.

How Base64 Works

  1. Binary to ASCII Conversion: Base64 converts binary data to text using 64 characters from the ASCII character set (A-Z, a-z, 0-9, +, /).
  2. Encoding Process:
    • Binary data is divided into 3-byte (24-bit) chunks.
    • Each 3-byte chunk is split into four 6-bit groups.
    • Each 6-bit group is converted to the corresponding character in the Base64 character set.
  3. Padding:
    • If the total number of bits to encode doesn't divide evenly by 6, padding characters (=) are added to make the final data chunk fit the Base64 format.
    • When the number of bytes is not divisible by 3, padding is needed. If there is 1 byte left over, two = characters are added. If there are 2 bytes left over, one = character is added.
  4. Decoding:
    • To decode Base64 data, perform the process in reverse: convert each character to its corresponding 6-bit binary value.
    • Combine these binary values to recreate the original binary data.

Base64 Example Analysis

Let's examine how the phrase "Hello" is encoded in Base64:

ASCII Values:

H = 72
e = 101
l = 108
l = 108
o = 111

Binary Values:

H = 01001000
e = 01100101
l = 01101100
l = 01101100
o = 01101111

Binary Data (connected bits):

01001000 01100101 01101100 01101100 01101111

Grouped into 6-bit segments:

010010 000110 010101 101100 011011 000110 111100

Converting each 6-bit segment to Base64 character:

010010 = S
000110 = G
010101 = V
101100 = s
011011 = b
000110 = G
111100 = 8(4 bits remaining, = padding added)

Final Result:

The Base64 encoding of "Hello" is "SGVsbG8="

URL-Safe Base64

Standard Base64 encoding uses + and / characters, which have special meanings in URLs and can cause problems. URL-safe Base64 replaces these characters:

  • + is replaced with - (minus)
  • / is replaced with _ (underscore)
  • Sometimes = padding is omitted to further reduce URL length

This allows Base64 data to be safely used in URLs, filenames, API keys, etc.

Common Mistakes with Base64

  • Encoding/Decoding Order Confusion: Data is encoded then decoded to restore the original. Confusing the order will cause errors.
  • Ignoring Padding Characters: The = padding characters are important and needed for decoding. Removing them can cause decoding errors.
  • Binary Data vs. String Confusion: Treating binary data as text or vice versa can cause encoding issues.
  • Character Encoding Confusion: The encoding of the original string (UTF-8, ASCII, etc.) must be considered before encoding.
  • Ignoring Newlines: Some Base64 implementations add newlines every 76 characters. These should be removed during decoding.