How Does Base64 Encoding Work?

Some things you might have heard being thrown around in the cryptography space are encoding methods. Such encoding methods are normally used to turn binary data into text, typically so it can be used to transmit data over communication methods that do not allow raw binary data to be transmitted.

How does binary turn into Base64?

First, let’s say that we want to encode an image. We start by reading all the bits in the image, which, for this example, will result in a binary stream of 010010101101001000111111. It is important to note that most images are a lot larger than this and will contain hundreds of thousands of bits.

The encoder will first break the binary stream up into their own chunks, each containing six bits. We can do this to our example and get 010010 101101 001000 111111.

Then, we turn our binary chunks into decimals.

To do this, working right to left, we multiply each bit by 2 to the power of p, where p starts from zero and increments by one for every bit in the chunk.

For example, we can calculate 010010 by performing the expression (0 * 20) + (1 * 21) + (0 * 22) + (0 * 23) + (1 * 24) + (0 * 25). We can strip away any parts that multiply by zero, since those will always be zero. The simplified equation is (1 * 21) + (1 * 24) = 2 + 16 = 18. Therefore, 010010 is equivalent to the decimal 18.

Turning every single six-bit binary chunk into a decimal using the process shown above constitutes the numbers (in left-to-right order) 18, 45, 8, and 63.

After that, we can simply cross reference the numbers with whatever alphabet we should use. For example, the six-bit chunk 010010 turns into the decimal 18, which corresponds to S on the Base64 standard alphabet. Therefore, 010010 101101 001000 111111 turns into StI/.

How does binary turn into Base64?: fixing the padding

If the binary string is a multiple of 3, everything fits perfectly. However, if it is not, then we need to add padding to the encoded string until the string’s bit count becomes a multiple of 3.

Let’s take 010010 101101 001000 111111 01 as an example. It is 26 bits long and all full six-bit chunks encode to StI/. However, this is leaving out the two bits at the end.

To pad this, we will keep adding zeroes to the end of the string until it can become a full chunk. Thus, the final chunk, 01, becomes 010000, which corresponds to Q in the Base64 alphabet. Then, we add one padding character (typically an =) for every two bits we had to add for the string to only be made of full chunks. We add two padding characters because four (the number of bits we had to add) divided by two is two.

Thus, you would think that the padded string would be StI/Q==, right? Well, no, because that extra character adds another six bits, meaning that there are still two bits at the end. We can only stay in such a loop two times, so we need to add more zeroes. That means that the final letter is 000000, which is A.

So, the final result is StI/QA==.

How does Base64 turn into binary?

Base64 decoding is just like base64 encoding, but in reverse. First, we turn every single non-padding character into its respective index on the alphabet, then we turn every single number into its binary equivalent.

For example, let’s take our string StI/QA==. We first take every single non-padding character and turn it into a string of numbers that represents their indexes on the alphabet.

This turns our Base64-encoded string into a string of numbers, specifically 18 45 37 62 16 0 = =, since we don’t turn the padding into a number

The Standard Base64 Alphabet

0: A7: H14: O21: V28: c35: j42: q49: x56: 5
1: B8: I15: P22: W29: d36: k43: r50: y57: 6
2: C9: J16: Q23: X30: e37: l44: s51: z58: 7
3: D10: K17: R24: Y31: f38: m45: t52: 159: 8
4: E11: L18: S25: Z32: g39: n46: u53: 260: 9
5: F12: M19: T26: a33: h40: o47: v54: 361: +
6: G13: N20: U27: b34: i41: p48: w55: 462: /
Padding: =