DEV Community

Cover image for Touchbase with Base64
Debojyoti Chatterjee
Debojyoti Chatterjee

Posted on

Touchbase with Base64

Base64 is a method of representing binary data in an ASCII string format. This is achieved by converting a binary data into 6-bit character representation.

Base64 uses a specific algorithm for encoding which helps to convert the binary data into the desired output. This is a reversible algorithm, and we can retrieve the original data from a Base64 string by back-pedalling (reversing the algorithm).

Let’s have a quick run-through on how this algorithm works. Just for fun we will convert the word “Base.64” into a Base64 string.

  1. The very first thing that we would want to do is split the word into separate characters. So we have 7 characters n our selected word:

    • B
    • a
    • s
    • e
    • .
    • 6
    • 4
  2. Now let’s convert each of the above into their corresponding binary values, you can also use the Base64 Index Table if you want:

    • 01000010
    • 01100001
    • 01110011
    • 01100101
    • 00101110
    • 00110110
    • 00110100
  3. Now we need to form a single string out of the above by concatenating them one after the other. The resulting string will be as following:

    01000010011000010111001101100101001011100011011000110100
  4. Once we have done this, we have to carefully break it again so that each part has 6 characters. Something like this:

    • 010000
    • 100110
    • 000101
    • 110011
    • 011001
    • 010010
    • 111000
    • 110110
    • 001101
    • 000000

    (Note: If your last part is less than 6 characters, append it with zeros. That'll do fine. Here we added 4 zeros “0000” to make it 6-bit bytes.)

  5. Now each part is of 6-bit bytes, prepend with “00” and convert each part to 8-bit bytes:

    • 00010000
    • 00100110
    • 00000101
    • 00110011
    • 00011001
    • 00010010
    • 00111000
    • 00110110
    • 00001101
    • 00000000
  6. Now it’s time to convert them to their corresponding decimal values:

    • 16
    • 38
    • 5
    • 51
    • 25
    • 18
    • 56
    • 54
    • 13
    • 0
  7. The above list of integers you have received are called Base64 indices. You can map the Base64 indices to the Base64 Character Table and form your final Base64 string. So what are we waiting for? Let’s do that:

    • Q
    • m
    • F
    • z
    • Z
    • S
    • 4
    • 2
    • N
    • A
  8. Now, do you remember we added 4 zeros to the last part in the 4th step? So for every two zeros we will append 1 padding character “=”. Voila! Now you have your final Base64 string for the word “Base64”.

QmFzZS42NA==

Cool! Don’t you think? Now to decode a Base64 string you have to perform the same steps but backwards, if you do that correctly you will reach the actual string. I would suggest that you give it a try!

I'll just keep the Wiki Liquid tag for the Base64 Index Table, it might help:

The Base64 index table:

Index Binary Char Index Binary Char Index Binary Char Index Binary Char
0 000000 A 16 010000 Q 32 100000 g 48 110000 w
1 000001 B 17 010001 R 33 100001 h 49 110001 x
2 000010 C 18 010010 S 34 100010 i 50 110010 y
3 000011 D 19 010011 T 35 100011 j 51 110011 z
4 000100 E 20 010100 U 36 100100 k 52 110100 0
5 000101 F 21 010101 V 37 100101 l 53 110101 1
6 000110 G 22 010110 W 38 100110 m 54 110110 2
7 000111 H 23 010111 X 39 100111 n 55 110111 3
8 001000 I 24 011000 Y 40 101000 o 56 111000 4
9 001001 J 25 011001 Z 41 101001 p 57 111001 5
10 001010 K 26 011010 a 42 101010 q 58 111010 6
11

Discussion (0)