Data Structures & Algorithms

Understand Base64 Padding

So far, we have talked a lot about the Base64 encoding and decoding processes using different programming languages. We have learned the full-fledged methods of these conversions and the usage of some of the online tools used for performing the Base64 encoding and decoding very conveniently. Today, we will try to go a step further by learning a new concept associated with the Base64 encoding known as “padding.” However, before proceeding to its explanation, we will give you a brief overview of the Base64 encoding process. Then, with the help of discussing a few commonly raised concerns regarding the Base64 encoding, we will try to elaborate on the concept of padding. You will have to go through all the sections of this article to understand this concept most effectively.

What is Base64 Encoding?

Base64 is one of the most commonly used binary to text conversion formats. This encoding scheme converts the data to be encoded into streams of 24 bits where these bits can be perfectly mapped onto four 6 bits Base64 digits. Almost all the programming languages allow you to convert your desired data into the Base64 format.

Some of these programming languages have built-in Base64 encoding and decoding functions, whereas, in others, you have to code these functions manually. Obviously, in the former case, it is relatively easier to perform the Base64 conversion. Moreover, there are also different online tools that can help you achieve this goal. You can use these tools if you are not fond of programming. Also, apart from text, full-fledged documents, as well as image files, can be converted to the Base64 format very easily.

Characters used in the Base64 Encoding:

The total number of characters involved in Base64 encoding is 65. More specifically, 10 digits from 0 to 9, upper case alphabets from A to Z, lower case alphabets from a to z, “+” sign, “/” forward slash, and the padding character “=” are used. However, the reason behind calling this scheme “Base64” is that normally, 64 characters are involved in the Base64 encoding, whereas the 65th character, i.e., the padding character “=” is additional, i.e., it is used whenever needed.

Need to use the Base64 Encoding:

Since the Base64 encoding process involves converting binary data, it is performed in situations where the medium you are working with cannot handle the binary data well. In other words, that medium might alter that binary data so that its integrity is compromised. Therefore, before transmitting the binary data over any such medium, it is first encoded using the Base64 encoding scheme.

What is padding in General?

Padding refers to the addition of “0s” to the left of a number to satisfy the length requirements so that this addition will not affect the actual magnitude of the number. However, at times, any character other than “0” can also be added to the right or left of a text string to make its length equal to the required length.

Need for Padding in Base64:

We know that the Base64 encoding process involves converting data into sequences of 24 bits that are represented by four 6 bits Base64 digits. It is mandatory for a proper conversion into Base64 that the resulting data must be converted into sequences of 24 bits each. However, at times, it happens that this length is not satisfied, i.e., a few bits might not be there, or the total bits of the encoded data are fewer than 24. In this case, to satisfy the requirements of the Base64 padding, some special characters (“=” in the case of the Base64 encoding) are padded to compensate for the missing bits. In this way, the Base64 encoding process takes place correctly. It is considered a mandatory process for preserving the integrity of the data.

What to do with the Padded Characters while doing the Base64 Decoding?

While decoding the Base64 encoded data, all the padded characters are discarded first. Only then the decoding takes place correctly. Otherwise, if the data is decoded without removing the padded characters, you will never be able to reach your original data.

Why cannot we get rid of the Idea of Padding?

The concept of padding is used very extensively in networking. However, many people raise the question of why cannot we get rid of the idea of padding, or is there any way in which we can avoid it? Well, the answer to this question is a bit too tricky. Yes, there is a situation in which padding can be avoided. However, in this situation, you must know the length of the bytes that you will encode beforehand. So that later on, instead of doing the Base64 padding, you can simply add a fixed length of integers to your encoded data.

Unfortunately, we do not know this length most of the time prior to encoding the data. For example, a video is streaming live, and you are encoding that video on the go. In this case, you cannot pre-determine the data length to be encoded. Therefore, you cannot just append a fixed length of integers at the end of that data; instead, you will be forced to use the Base64 padding.


With the help of this discussion, we wanted to enlighten you on the concept of the Base64 padding. For that, we started by briefly explaining to you the process of the Base64 encoding. After that, we tried to explain the concept of the Base64 padding, making use of some of the common concerns associated with this concept. Hopefully, after going through this article, you will be in a good position to explain this concept to someone else.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.