Categories > Coding > C# >

[TUT] How Base64 works and how to implement it from scratch

Wabz

Winning.js

Posts: 2410

Threads: 194

Joined: Apr, 2020

Reputation: 27

Posted

Hi gamers, this will be a small tutorial of me explaining what base64 is, how base64 works, and how you can implement it.

My code is based off what i learned from this article: https://medium.com/swlh/powering-the-internet-with-base64-d823ec5df747

To anyone who doesn't understand what I mean well the article didn't provide any code, but from what I learned there I was able to make this, and this tutorial will try to simplify this and show you how i implemented it.

 

What is Base64 (and why is it useful)?

 

Okay so basically with Base64 you can encode any kind of binary to some ASCII string (learn more about ASCII by searching it, i won't explain what it is). This is very good because when you send data you never know if the receiver can deal well with unicode characters etc (once again, i won't explain what that is, search it up). Base64 only uses 64 characters (which are A-Z,  a-z, 0-9, +/, and =).

 

How does Base64 work?

 

Basically, for 3 bytes (if you don't know what a byte is you really shouldn't be reading this), you can convert them into 4 base64 characters? How? Well I will explain that. A byte is 8 bits, so 3 bytes is 24 bits. How can you divide 24 bits into 4 characters? By using 6 bits to represent a base64 character. Here is a table that represents which base64 character is for each 6 bits of data that is possible (2^6 is 64 that's why there's only 64 possible, learn more about binary if you don't get that and come back). 

 

https://i.imgur.com/6zBxNmw.png

 

How to implement Base64 for noobs

Basically, here's what I do:

  1. Convert each byte of data (in my implementation its a string that you encode, so each characters bc that's a byte) into some binary, add some padding with 0s to make it fit into 8 bits if it doesn't already btw
  2. Put it all into a long string
  3. Divide it all into 6 bits and put it into an array (01010110, 00111110, 10011001 becomes 010101, 100011, 111010, 011011)
  4. Loop through all of them, convert them into the equivalent b64 character
  5. Join it all together (very easy step)

This strategy works with data that can be perfectly be divided into 6 bits, but not everything can, this is where padding comes in place, there are 2 more things to do.

 

At step 4, don't convert what you have into a base64 character directly, since the last 6 bits won't be 6 bits, and that will fail. add 0's at the end of it as a padding, make sure to only add it to the point it becomes 6 bits long. 

 

At step 5, you have to make sure to add = as padding so that the length of the string remains divisible by 4 (the ='s are not meant to do anything besides that)

 

Here is my code for it if you want to see a final result, just know that my C# is a bit rusty and there's probably better, more efficiant ways to do this, I only followed the way i found reading the article. I believe i explained everything there is to know about encoding base64 but I'd highly suggest you read the article too, I just wanted to simplify it for people who aren't used to it. 

 

Code link: https://pastebin.com/gY0WnMGp

(updated, i've talked to iMaximus and he mentionned me a few things I needed to change, some of which I've added, creds to him too)

 

If anyone wants me to make a thread explaining the basis of how binary works, let me know, I'm not too good at it and I'm still learning but I do know a couple basics since a very long time so i'm kinda used to working with binary numbers.

  • 0

My new discord is Wabz#1337 with ID 777154062789509130

Posts: 2099

Threads: 10

Joined: Sep, 2020

Reputation: 62

Replied

Very nice / charsssssss

  • 0

Discord : Doctor Doom#0550

Moon

Moon

vip

Posts: 7441

Threads: 314

Joined: Aug, 2020

Reputation: 80

Replied

I should probably learn the basics of C# before reading this

  • 0

Posts: 870

Threads: 34

Joined: Aug, 2020

Reputation: 6

Replied

the code is not the greatest but will work. but here's a fact there's a bult-in class for base64 encode and decode.

  • 0

Wabz

Winning.js

Posts: 2410

Threads: 194

Joined: Apr, 2020

Reputation: 27

Replied

@MaximusExploit It was meant to teach people how base64 works, so that's why I'm doing it from scratch.

Other than that what can i ameliorate? Ik i've been using foreach in a couple of places and that's not very good but i honestly would rather have readable code for this since it's for learning purposes, and i've done retarded stuff for a few return types but that's rly all i can see.

  • 0

Added

@_realnickk (just so ppl can learn, if they use this in production code theyre dumb)

  • 0

Added

bump 10 chars

  • 0

My new discord is Wabz#1337 with ID 777154062789509130

MINISHXP

[REDACTED]

Posts: 976

Threads: 3

Joined: Jan, 2021

Reputation: 9

Replied

isnt there a base64 class with functions for encoding in c#

 

  • 0

Wabz

Winning.js

Posts: 2410

Threads: 194

Joined: Apr, 2020

Reputation: 27

Replied

@MINISHXP omg, im tired of explaining it, so i'll tell you this, read the title please

  • 0

My new discord is Wabz#1337 with ID 777154062789509130

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )