Have you ever looked at the content of an email header, JWT token, or the result returned by an API call, and saw some mysterious letters, numbers, and characters like SGVsbG8gV29ybGQh? Then you have stumbled upon Base64 encoding. Knowing how to decode it – and what it is used for – is a tiny but important ability that all web developers need.
In this guide, we will take a closer look at what Base64 decoding is, how it is performed, when you will come across it in practice, and how to decode Base64 correctly avoiding common mistakes.
What Is Base64?
Base64 is a type of encoding that represents binary data as text using only 64 printable ASCII characters, namely the letters from A to Z in both upper case and lower case forms, digits 0 to 9, and two special characters (usually + and /).
It is important to understand that Base64 is not an encryption tool used to secure data; it is a method of encoding used to ensure that binary data can be transmitted via text-based environments. For example, images, files, and other binary data will disrupt email or URL protocols when transmitted directly, so Base64 encodes them into text form.
What Is Base64 Decoding?
The opposite of Base64 encoding is Base64 decoding. This is the process by which Base64-encoded content is converted to its original form. The original form can be either binary or plain text.
Here is an example:
•Original text: Hello World!
•Base64 encoding: SGVsbG8gV29ybGQh
•Base64 decoding: Hello World!
The need for Base64 decoding arises when a computer receives Base64-encoded information and has to use its original contents. The original content may be an image, a file, a hashed password, or even some JSON content.How Base64 Encoding and Decoding Actually Work
1.The data is grouped into blocks of 3 bytes (24 bits).
2.These 24 bits are divided into four 6-bit blocks since 6 bits allow for 64 possible values from 0 to 63. Each of the four blocks is a single character in the Base64 alphabet.
3.The 6-bit value is used to find a corresponding letter, digit, or symbol in the Base64 character table.
4.= symbol is added to the output in case the data does not divide equally by 3 bytes; the decoder will know what to do with the last block.
The decoding is done in reverse order, that is, characters are converted back into their 6-bit representations, grouped again into bytes, and finally, the original binary or text data is restored.
Since Base64 encoding is based on a mathematically-defined set of rules and is not an encrypted message with a key, everyone can decode the message without knowing any password or key.
Why Base64 Encoding Increases File Size ?
Base64 Encoding and the Effect on Size Increase of Files.There is one aspect worth mentioning regarding Base64 encoding – it makes your data bigger by approximately 33%. The reason is simple – for every 3 bytes in binary format, you get 4 bytes as text result. If you plan to embed files in Base64 format into your web page, remember about the overhead involved.
Common Use Cases for Base64 Decoding
1. Email attachments (MIME)
The protocols of sending emails do not work well for binary attachments. Base64 is used to send PDF, image and document attachments in a secure way. Your email program automatically decodes the Base64 attachment.
2. Embedding Images in HTML/CSS
Programmers prefer to use base64 encoded images and embed them directly in the HTML code. This decreases the number of requests, although it makes your file bigger.
3. Authentication in API Calls/Access Tokens
Some APIs use basic authentication when making API calls. It requires a combination of username:password to be Base64 encoded and sent in a request header. JSON Web Tokens also use Base64 in order to encode header and payload of a token (Base64URL).
4. Data URIs and Web Development
Base64 encoding is used for embedding binary data such as font, icon and other media in JavaScript, CSS or JSON without additional file requests.
5. Text Based Formats for Binary Data
Sometimes databases, configuration files and text based storage needs to store binary data (images, encryption keys etc.). For these purposes Base64 encoding can be used.
6. Debugging, Inspecting
Base64 strings are usually decoded manually by developers during debugging and analyzing JWTs and cookies.
How to Decode Base64 (Multiple Methods)
This is the quickest way if you need to decode something once: copy and paste the code string into the decoder available on the web and see the result in front of you right away. However, this is not a good idea if your data is sensitive, as you transmit it to a server belonging to someone else.
Base64 Decoding Using the Command Line (Linux/macOS)
echo "SGVsbG8gV29ybGQh" | base64 --decode
The result will be: Hello World!
Base64 Decoding in Python
import base64
encoded = "SGVsbG8gV29ybGQh"
decoded = base64.b64decode(encoded).decode("utf-8")
print(decoded) # Hello World!
Base64 Decoding in JavaScript
const encoded = "SGVsbG8gV29ybGQh";
const decoded = atob(encoded);
console.log(decoded); // Hello World!
Base64 Decoding in Java
byte[] decodedBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
String decodedString = new String(decodedBytes);
Most modern programming languages include built-in Base64 libraries, so you rarely need to implement the algorithm yourself.
Base64 Is Not Encryption — A Critical Distinction
This is possibly the most crucial point to realize when it comes to Base64 encoding: Base64 is not a security tool whatsoever. Base64 does not encrypt, obscure or disguise data at all; it only rewrites it into another format.
Any individual who encounters Base64-encoded text would be able to decode it within seconds using readily available software without needing any password or key. When encountering Base64-encoded login credentials, API keys or personal information, think of it as readable text.
Common Mistakes To Avoid While Decoding Base64
•Since it is not encrypted, thinking that it is secure. This point has been explained in detail above.
•Not considering padding. The padding character (=) should be taken into consideration since missing or invalid padding will result in decoding problems in strict decoders.
•Mixing up Base64 and URL-safe Base64. In order to use the former type, there should be no + and / but - and _ symbols. The former decoder cannot handle the latter format and vice versa.
•Trying to decode corrupt or invalid data. Decoding an altered, cut, and invalid Base64 will result in failure or gibberish.
•Not providing the character encoding while decoding. While decoding to text, always provide the correct character encoding.
Conclusion
Base64 encoding/decoding is just one aspect of many in web programming and data management, but it’s worth knowing how to use it efficiently and effectively because it will always come in handy whenever you want to debug an API response, inspect a JWT, include an image inline, or simply decode a strange-looking string of characters. Just remember that Base64 encoding is all about compatibility, not security, and that decoding is fast and easy.