[img]http://i.minus.com/ieq6viOQ6szdE.gif[/img]
I thought I might make a thread on how to base64 encode and decode to give everyone a bit more of an insight into basic cryptography. If you don't understand any part, please post here and I'll fix it up. This tutorial will require you to read every single step to understand it. You will also need two images which you can print or just show on your screen for this. For someone who is advanced in cryptography, I may use really basic terminology, but it's so that people can understand it. For this tutorial, we will base64 encode the word "Hello" into "SGVsbG8=".
Step 1
Get out a piece of paper and a pen and print or display the following 2 image on your screen
Spoiler (Click to View)
Spoiler (Click to View)
Step 2
Draw the following boxes up on your piece of paper
Spoiler (Click to View)
Step 3
Now here is where it all starts. Firstly, you will need to understand that
- All characters on the ASCII table (all special, numeric, and alphabetical) characters have a specific number value on the ACSII table.
- Binary only consists of 1s and 0s. To make our decimal value on the ACSII table actually become binary, we must use a binary table (first table you drew).
- Each alphabetical character represents 8 binary numbers (8 zeros and ones).
- All alphabetic characters are case sensitive which means that you need to assign the right number to the right case too. E.g. a on the ASCII table is 97 decimal value and A on the ASCII table is 65 decimal value.
So what do we start off with in this step? We firstly need to turn our characters into decimal values. These decimal values are on the ACSII chart as 'dec'. So we need to find the decimal value for each of our letters in Hello.
Code
H = 72
e = 101
l = 108
l = 108
o = 111![[Image: DcdES2B.png]](https://i.imgur.com/DcdES2B.png)
So what's all those random numbers for the rest of our columns? As I said before, each character will represent a 8 bit / character binary sequence. For example, 'H' has the decimal value of 72 and the binary value of 01001000. How is this possible though? For any character binary blocks, each number represents how much of another number is inside that number.
Binary goes up in 2s, so you will see a binary table of 128 64 32 16 8 4 2 1. The more characters in a binary block, the more x2 we go up to. For example, having a 10 character binary block, you would have 512 256 128 64 32 16 8 4 2 1.
In our case, the ACSII table only has a max value of 255 characters, which a 8 bit block of 128 64 32 16 8 4 2 1 can cover. Solving the binary value for H is pretty straight forward. You have the decimal of 72, and you go through the following process:
Code
H = 72
How many 128's in 72? 0
We still have the value of 72
How many 64's in 72? 1
Now our remainder value is 8
How many 32's in 8? 0
How many 16's in 8? 0
How many 8's in 8? 1
Now we have nothing left. So..
How many 4's in 0? 0
How many 2's in 0? 0
How many 1's in 0? 0
What do we end up with? 01001000 which is our binary value for H or 72.So, lets fill in our binary values for H, e, l, l, o.
![[Image: RXrQuEz.png]](https://i.imgur.com/RXrQuEz.png)
If you still don't understand by this point, make sure you post and I'll try help you out.
Step 4
This is where we go from our binary representations of our characters to the base64 representations of our characters. So we currently have
Code
H = 01001000
e = 01100101
l = 01101100
l = 01101100
o = 01101111Code
0100100001100101011011000110110001101111Code
010010
000110
010101
101100
011011
000110
1111BUT WAIT A SECOND. We have one block of 6 that only has 4 characters. This is because we have a total of 40 binary numbers BUT 40/6 isn't actually a full number. So what we are going to do is add two added zeros on the end. We need to remember they are two added 0's because we will have to add a equals sign in the padding step per 2 0's we have. In this case, we only have one pair of two zeros, but we will get that sorted later. So lets add the added zeros on the end to make our blocks all consist of 6 characters.
Code
010010
000110
010101
101100
011011
000110
1111(00) <---- added 0's.Now, we are onto our second table. Our bin number is just each block. In this, you can see that we only have 32 16 8 4 2 1 instead of 128 64 32 16 8 4 2 1. This is because we went from 8 bit / character binary blocks to 6 bit / character binary blocks. So, we pretty much just do the reverse of what we did for the first step and insert each binary number into its respective column. For example, the first block of 010010
Code
0 of 32's
1 of 16's
0 of 8's
0 of 4's
1 of 2's
0 of 1's![[Image: pVzMdye.png]](https://i.imgur.com/pVzMdye.png)
So we have our base64 numerical values (18, 6, 21, 44, 27, 6, and 60) and we just need to use our base64 chart to find the character to the number.
Code
18 = S
6 = G
21 = V
44 = s
27 = b
6 = G
60 = 8So our final value would be SGVsbG8 but we are not actually finished. Remember the added 0's? Go to step 5 if you end up with added 0's.
Step 5
This is the last step and you only need padding if you end up having those added zeros on the end. Per pair of added zeros, we add one equals sign onto the end of it. It's only possible to either have zero added 0's, two added 0's, or four added 0's. So in saying that, it's only possible to have either no equals, one equals or two equals. In our case, we had 2 added 0's, so we only add one equals sign to the end of it.
Our final value is SGVsbG8= . Want to check if this is correct? Visit http://www.base64decode.org/ and enter in that value and see if this is correct :). Decoding base64 is pretty much the opposite process of what we just did. For example
- Find the numerical values for the base64 characters
- Put them into binary (6 character blocks).
- Put them into a long string
- If you had one or two equals signs take off 2, or 4 0's respectively.
- Put them into 8 character blocks
- 8 character blocks to numerical number
- Lastly, numerical number to acsii character.
If you didn't understand the above explanation, tell me and I'll make an example of reversing what we just did. This thread seems a little silly but it was more about learning the basics of encoding than learning how to write down a base64 encoded message with a pen and paper.

![[Image: eaqOUqT.png]](https://i.imgur.com/eaqOUqT.png)
![[Image: KDm6pZo.png]](https://i.imgur.com/KDm6pZo.png)


![[Image: XHWqIBc.png]](https://i.imgur.com/XHWqIBc.png)