Understanding What Is Base 64 and Its Uses

Author

Reads 785

An artist's illustration of artificial intelligence (AI). This image represents storage of collected data in AI. It was created by Wes Cockx as part of the Visualising AI project launched ...
Credit: pexels.com, An artist's illustration of artificial intelligence (AI). This image represents storage of collected data in AI. It was created by Wes Cockx as part of the Visualising AI project launched ...

Base 64 is a way to represent binary data using only the 64 characters A-Z, a-z, 0-9, and +/. It's used to send binary data over text-based systems, like email or text messages.

This is because binary data, which is made up of 0s and 1s, can't be sent directly over text-based systems. Base 64 encoding converts the binary data into a string of characters that can be sent over these systems.

Base 64 is commonly used for encoding images, audio files, and other binary data. It's also used for encoding credentials, like usernames and passwords, in URLs.

Related reading: Base 64 Encoding Python

How Base 64 Works

Base64 is a binary-to-text encoding scheme that represents data in ASCII string format, allowing it to be transmitted over channels that expect text. This is especially useful when dealing with binary data that can't be directly sent over text-based channels.

Base64 works by breaking down binary data into 6-bit segments, which are then mapped to one of 64 characters. This is done to ensure that humans can read the printable data. The 64 characters used in Base64 encoding include 10 numeric values, 26 uppercase alphabets, 26 lowercase alphabets, and two special characters.

Credit: youtube.com, Base64 Encoding

Base64 uses 6-bit characters grouped into 24-bit sequences. When the original binary can't be divided into a 24-bit sequence, padding is needed. This is represented by printed equal signs (=) at the end of the encoded string.

Here's a breakdown of the 64 characters used in Base64 encoding:

  • 10 numeric values: 0, 1, 2, 3, ..., 9
  • 26 uppercase alphabets: A, B, C, D, ..., Z
  • 26 lowercase alphabets: a, b, c, d, ..., z
  • Two special characters: + and / (or - and _ in URL-safe Base64)

Base64 encoding is often used to convert binary data into ASCII text format, making it safe to transmit over communication channels that expect text. This is why you'll often see Base64 encoded data in email attachments or web forms.

Base64 encoding takes a binary sequence as input and outputs another binary sequence. It doesn't care what the original bytes stood for, it just converts every 6-bit chunk into a safe textual character.

Uses for

Base64 is used for a variety of purposes, and it's essential to understand these uses to appreciate its value.

In addition to safely encoding image and media data, Base64 is used for SSL certificates, email transmissions, and virtually any transfer of information that requires special control characters to be escaped.

Data Codes through Eyeglasses
Credit: pexels.com, Data Codes through Eyeglasses

Base64 is also necessary when transmitting incompatible data through the network, such as attaching an image to a textual message in an email.

Incompatible data is often stored in files or elsewhere, like embedding non-textual data in a textual file like JSON, XML, or HTML.

Base64 can be used to store something fancy in a browser cookie, which must be only text.

Here are some examples of uses for Base64:

Limitations and Considerations

Base 64 has its limitations, and it's essential to consider them when using this encoding scheme. It can only encode binary data, which means it's not suitable for encoding non-binary data like text.

The maximum length of a Base 64 encoded string is 76 characters, which can be a challenge when working with large datasets. This is because each line of Base 64 encoded text must be 76 characters or less to maintain readability.

In addition, Base 64 encoded strings are not human-readable, which can make them difficult to debug or troubleshoot.

Why 64 Characters Limit

Abstract representation of a futuristic digital processor with glowing elements.
Credit: pexels.com, Abstract representation of a futuristic digital processor with glowing elements.

The 64 characters limit in Base64 is actually a result of excluding non-printable characters from the encoding alphabet, which leaves us with around 90+ printable ones.

ASCII is 128 characters long, but the authors of Base64 chose to use only half of them to ensure a safe and reliable encoding.

Printable characters can still have special meaning in different systems, such as SMTP, where "@" and "<" can cause issues.

The choice of 64 characters was also influenced by the fact that it's easier to work with algorithmically.

In reality, there are only 62 characters that are guaranteed to have no special meaning in all systems, including digits, English small letters, and English capital letters.

To make up for the remaining two characters, "+" and "/" are commonly used, but they can still break things in certain situations, like URLs.

That's why we have a "base64url" variant that uses "_" and "-" instead, which is safer in those cases.

Memory Cost by Byte Length

Credit: youtube.com, Bits and Bytes ๐Ÿ’พ Data Storage Measurements Explained โ€“ DIY in 5 Ep 164

Using only 7 bits per character can make base64 less wasteful in terms of memory usage.

The main theory that base64 causes a memory overhead of 33% only applies to an 8-bit scenario. This is because base64 converts every 6-bit chunk of original data into a single ASCII character, wasting 2 bits per chunk in an 8-bit scenario.

In a 7-bit character scenario, the loss is about half the size, making base64 more efficient.

Characters can be wrong for at least two reasons: they can be invalid, or they can be valid but mistakenly recognized as a special character.

Do I Need HTTP/1.1?

HTTP actually allows all kinds of data in the body of a message, not just textual characters.

The body of an HTTP message is not restricted to ASCII characters, but HTTP headers are, making it a good practice to use only ASCII.

Base64 is recommended to use in HTTP headers when carrying non-ASCII stuff, such as username and password in a basic authentication scheme.

An artistโ€™s illustration of artificial intelligence (AI). This image represents how machine learning is inspired by neuroscience and the human brain. It was created by Novoto Studio as par...
Credit: pexels.com, An artistโ€™s illustration of artificial intelligence (AI). This image represents how machine learning is inspired by neuroscience and the human brain. It was created by Novoto Studio as par...

This is not because HTTP protocol forbids binary sequences, but because HTML and CSS files have to be only text to be properly interpreted by text editors and browsers.

The extra bytes created by base64 can easily nullify the performance gain of using Data URLs, which aim to improve performance by sending an image without an extra HTTP request.

Encoding and Decoding

Base64 encoding is a binary-to-text encoding scheme that converts binary data into an ASCII string. This allows binary data to be safely transmitted over text-based channels, such as email or web forms.

The encoding process involves breaking down binary data into 6-bit segments, which are then mapped to one of 64 characters using the Base64 index table. This table includes 10 numeric values, 26 uppercase alphabets, 26 lowercase alphabets, and two special characters: + and /. Variants of Base64 may replace these special characters with - and _.

To encode a string, you can follow these steps: count the number of characters in the string and pad with "=" if necessary, encode the string in ASCII format, convert the ASCII to binary format, divide the binary data into chunks of 6 bits, and finally, convert each chunk to a decimal number and then to a string using the Base64 index table.

For your interest: Using Oembed in Base Html

Credit: youtube.com, Base64 Encoding/Decoding explained

Here's a summary of the Base64 encoding process:

Decoding Base64 involves reversing the process, converting the string back to binary data, and then to the original binary information.

Encoding

Encoding is a crucial step in converting binary data into a text format that can be safely transmitted over channels that expect text. This process is essential for sending images, videos, and other binary files over email or the web.

Base64 encoding is a binary-to-text encoding scheme that converts binary data into an ASCII string, allowing it to carry data stored in a binary format across text-based channels. This scheme uses 64 characters to encode any string.

The Base64 encoding algorithm breaks binary data into 6-bit segments, which are then mapped to one of 64 characters to ensure that humans can read the printable data. This is why Base64 is considered a radix-64 representation.

The 64 characters used in Base64 encoding include 10 numeric values (0-9), 26 uppercase alphabets (A-Z), 26 lowercase alphabets (a-z), and two special characters (+ and /). Some variants, like URL-safe Base64, replace these special characters with - and _.

Credit: youtube.com, Security Testing (Basics) - Encoding and Decoding

To encode a string, the first step is to count the number of characters in the string. If it's not a multiple of three, we need to pad it with a special character, such as "=" to make it a multiple of three.

Here's a step-by-step guide to encoding a string using Base64:

1. Count the number of characters in the string.

2. Encode the string in ASCII format.

3. Convert the ASCII string to binary format, 8 bits each.

4. Divide the binary data into chunks of 6 bits each.

5. Convert the 6-bit chunks to decimal numbers.

6. Use the Base64 index table to convert the decimal numbers to a string.

Let's take a look at an example of encoding a string "THS" using Base64. We can see that the encoded output is "VEhTCg==". If we run the echo -n THS | base64 command on the CLI, we'll get the same output.

Here's a list of the steps involved in encoding a string using Base64:

  • Count the number of characters in the string
  • Encode the string in ASCII format
  • Convert the ASCII string to binary format, 8 bits each
  • Divide the binary data into chunks of 6 bits each
  • Convert the 6-bit chunks to decimal numbers
  • Use the Base64 index table to convert the decimal numbers to a string

Note that padding is necessary to make the string a multiple of three. This is done by adding "=" characters to the end of the string.

Decoding

Credit: youtube.com, Encode and Decode Strings - Leetcode 271 - Python

Decoding is the process of converting encoded data back into its original form. It's essentially the reverse of encoding.

To decode a Base64 string, you need to use the same lookup table used for encoding. This table maps each character to its corresponding 6-bit binary group.

Let's take the example from the previous section, where we decoded the string "aGk=" using the lookup table:

  • โ€œaโ€ => 011010
  • โ€œGโ€ => 000110
  • โ€œkโ€ => 100100
  • โ€œ=โ€ => padding

This leaves us with three 6-bit binary groups, which we can then convert back to 8-bit groups. The result is the original ASCII string "hi".

Decoding is an essential step in working with encoded data, and it's often used in web development and other fields where data needs to be securely transmitted or stored.

Frequently Asked Questions

Is Base64 safe to use?

Base64 is not completely safe to use, as it can be vulnerable to special character issues in certain systems. However, it can be made safer by sticking to a limited character set of 64 characters, including digits, English letters, and a few extra characters.

Danny Orlandini

Writer

Danny Orlandini is a passionate writer, known for his engaging and thought-provoking blog posts. He has been writing for several years and has developed a unique voice that resonates with readers from all walks of life. Danny's love for words and storytelling is evident in every piece he creates.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.