In my everyday work with Kubernetes, I often encounter Kubernetes secrets containing strings of seemingly random characters ending with equal signs. These are Base64 encoded texts. This made me curious: What exactly is Base64 encoding, and how does it work its magic?

So, one day, I decided to learn how Base64 actually works. It turns out to be a pretty simple process where the data (normal text and numbers) are converted into a special format that machines can easily read and use. Even though it’s not super secure like encryption, it’s really handy for keeping things organized and safe in places like Kubernetes.

What exactly is Base64 Encoding?

Base64 is a method used to change any type of data, like text or pictures, into a format that’s safe and easy for machines to handle. It’s called “Base64” because it uses 64 different characters to represent data. These characters include letters (both uppercase and lowercase), numbers, and symbols like + and /. Base64 encoding ensures that the data is not altered during transmission and helps maintain data integrity as it passes through various systems and networks.

How does Base64 Encoding Work?

Let’s quickly break down into how Base64 encoding works step by step:

1. Convert to ASCII

Convert each character in the string to its ASCII value.

2. Convert ASCII to Binary

Convert each ASCII value to its 8-bit binary equivalent.

3. Combine Binary Values

Combine all the 8-bit binary values into a single continuous binary string.

4. Split into 6-bit Segments

Split the continuous binary string into segments of 6 bits each.

5. Add Padding (if necessary)

If the final segment is less than 6 bits, add padding bits 0 to make it a full 6-bit segment.

6. Convert 6-bit Segments to Decimal

Convert each 6-bit binary segment to its decimal equivalent.

7. Map Decimal to Base64 Characters

Map each decimal value to its corresponding Base64 character using the Base64 index table, which consists of uppercase letters, lowercase letters, digits, plus +, & slash /

8. Combine Base64 Characters

Combine all the Base64 characters obtained from the previous step into a single string.

9. Add Padding Characters

If necessary, add = characters to the end of the Base64 string to make the total length a multiple of 4 characters.

Example: Steps to Encode “Hello” into Base64

Here’s a breakdown of how the word “Hello” is encoded into Base64 string:

Source ASCII textCharacterHello
ASCII Code72101108108111
Binary Equivalent(8 bits)0100100001100101011011000110110001101111
Base64 encoded textGrouped 6-bit Units010010000110010101101100011011000110111100
Decimal186214427660
CharacterSGVsbG8

1. Convert “Hello” to ASCII First, convert each character in the string “Hello” to its ASCII value:

  • H -> 72
  • e -> 101
  • l -> 108
  • l -> 108
  • o -> 111

2. Convert ASCII to Binary Next, convert each ASCII value to its 8-bit binary equivalent:

  • 72 -> 01001000
  • 101 -> 01100101
  • 108 -> 01101100
  • 108 -> 01101100
  • 111 -> 01101111

3. Combine Binary Values Combine these binary values into a single binary string:

0100100001100101011011000110110001101111

4. Split into 6-bit Segments Split the combined binary string into 6-bit segments:

010010 000110 010101 101100 011011 000110 1111

5. Add Padding Since the last segment is less than 6 bits, add padding to make it a full 6-bit segment. Each padding bit is 0:

010010 000110 010101 101100 011011 000110 1111[00]

6. Convert 6-bit Segments to Decimal Convert each 6-bit segment to its decimal equivalent:

  • 010010 -> 18
  • 000110 -> 6
  • 010101 -> 21
  • 101100 -> 44
  • 011011 -> 27
  • 000110 -> 6
  • 111100 -> 60

7. Map Decimal to Base64 Characters Map each decimal value to the corresponding Base64 character using the Base64 index table:

Reference: Base64 index table

IndexCharacterIndexCharacterIndexCharacterIndexCharacter
0A16Q32g48w
1B17R33h49x
2C18S34i50y
3D19T35j51z
4E20U36k520
5F21V37l531
6G22W38m542
7H23X39n553
8I24Y40o564
9J25Z41p575
10K26a42q586
11L27b43r597
12M28c44s608
13N29d45t619
14O30e46u62+
15P31f47v63/
  • 18 -> S
  • 6 -> G
  • 21 -> V
  • 44 -> s
  • 27 -> b
  • 6 -> G
  • 60 -> 8

8. Combine Base64 Characters Combine the Base64 characters: SGVsbG8

9. Add Padding Characters Base64 encoding requires the output to be a multiple of 4 characters. As required here, let’s add = to the end to make it so.

So, the Base64 encoding of Hello is SGVsbG8=

Base64URL

Base64URL is a variant of the Base64 standard tailored for use in filenames and URLs. Standard Base64 includes the characters +, /,& =, which can interfere with their reserved meanings in filesystems and URLs. Base64URL replaces + with -, / with _,& removes the trailing = padding when not necessary, ensuring the encoded data can be used in URLs without any issues.

Conclusion

Base64 encoding is a simple yet powerful tool to ensure data integrity and compatibility across different systems and platforms. By converting binary data into a text-friendly format, it allows for smooth data transfer and storage without any risk of data corruption, making it very useful for applications like emails and web development. Additionally, Base64 encoding is easily reversible, allowing for straightforward decoding back to the original data.