How can I implement Base 64 encoding without the Base64 method?
Hello, I was recently given the task in our IT class to create a C# WPF application that converts an input string to a Base64 result.
We were only given the layout of the application and nothing explained about the actual process behind it. I've now learned from Wikipedia that a string is divided into 8 bits each, then subdivided into 6 bits each, and if the result isn't divisible by 6, it's padded with zeros.
Our teaching is not the best and not much is explained, which is why I have the following questions:
- How can you divide a string into its 8-bit components?
- How can you divide the 8-bit pieces into 6-bit pieces?
- How can you then output the corresponding character set as a string from the Base64 table (see Wikipedia: Base64 – Wikipedia )?
I assume the binary values are stored in an array, but I haven't yet learned the workflow with these in the context of WPF applications.
Even though I've probably raised some stupid questions here, I hope someone can still answer them for me.
Thank you in advance.
With the different classes from system.Text.Encoding.
Example UTF-8:
Take the smallest common multiple (kgv) of 8 and 6.
The multiplier m for 8*m=kgv then tells you how many 8 bit blocks like many 6 bit blocks correspond to 6*M=kgv.
8*3=24
6*4=24
So: 3 blocks to 8 bits each correspond to 4 blocks to 6 bits each.
Then take your 3 blocks to 8 bits each:
and divides these into 4 blocks to 6 bits each:
One way to do this is to combine the 3 bytes with a bitwise shift to the left to form an int32 value and then divide them with bitwise shifts to the right and a bitwise AND into 6 bit blocks.
Very simple: 6 bits give exactly 64 possible combinations (from 0 to 63).
The Base64 character set has exactly 64 characters, each having a position of 0 to 63.
Therefore, for each 6 bit block, take the character that is at this index.
Don’t have to, but everything else would be Mumpitz.
This is exactly the same as in any other type of application also…
P.S.: Example code (extremely untested, but found plausible ^^^):
sorry have been a mistake here is the new code:
static void Main(string[] args)
♪
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
string unicodeString = “Hello”;
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
//1 Byte Array
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
for(int i = 0; i < asciiBytes.Length; i++)
♪
Console.Write($”{asciiBytes[i].ToString(“X”)}’);
}
Console.WriteLine();
Listbyte> sixBitBytes = new list();
byte remainingBits = 0;
for (int i = 0; i < unicodeBytes.Length; i++)
♪
int data = remainingBits == 0 ? unicodeBytes[i] : (unicodeBytes[i] < < 2) & remainingBits;
byte sixBitPart = (byte)(data & 63);
sixBitBytes.Add(sixBitPart);
remainingBits = (byte)(data & 192) >> 6);
}
foreach (byte b in sixBitBytes)
♪
Console.Write($”{b} “);
}
}
Thanks, that gave me a good basis, for my own code
This lookup tablet would have for the encoder 16777216 entries for 4 bytes each and for the decoder again as many as 3 bytes each (everything together so 112MiB)…
There would be a few more entries for all possible pudding scenarios.
Functional? Yeah.
Sensual? No…
I just looked at the link, in this case I would think a lookup table more meaningful
if A then 0
if B then 1
etc…
This is a condition for Base64 encrypting
Why you need to disassemble the data in 6 bits packages