I wrote this little program not too long ago when I was implementing IMAP protocol. Often, email handling
software use MIME defined rules to deal with attachements. Base64 encoding is one of the encoding methods that is used to encode
email. Why do you need to encode? Well as RFC1341 states, encoding is "applied to the data in order to allow it to pass through mail
transport mechanisms which may have data or character set limitations.". The program is not too complicated and the algorithm is
rather simple. I implemented the algorithm just for fun, and I post this information just for fun.
The current version of the code is the iteration number 6. The last three were posted here as I thought they were complete. I was
very wrong as the first one was just bad in terms of performance (encoding 6 times slower than MS version), second version was
a bit buggy (encoding slower just 2 times, but decoding 2 faster <-- none matter as the results were not always correct). I think
now I have arrived at the final solution (with maybe one more mod to the decode routine) which produces correct results and runs
fast (exact measurements will be posted soon). The component
was tested with my helper random string generating utility and test harness which compared the MS and my generated base64 encodings and
decodings.
You can get
source. I am thinking to post the whole C# project.
Sample input (excerpt from RFC1341):
These values are not case sensitive. That is, Base64 and
BASE64 and bAsE64 are all equivalent. An encoding type of
7BIT requires that the body is already in a seven-bit mail-
ready representation.
Resulting output:
VGhlc2UgdmFsdWVzIGFyZSBub3QgY2FzZSBzZW5zaXRpdmUuICBUaGF0ICBpcywgIEJhc2U2
NCAgYW5kIEJBU0U2NCAgYW5kICBiQXNFNjQgYXJlIGFsbCBlcXVpdmFsZW50LiAgQW4gZW5j
b2RpbmcgdHlwZSBvZiA3QklUIHJlcXVpcmVzIHRoYXQgdGhlIGJvZHkgaXMgYWxyZWFkeSBp
biBhIHNldmVuLWJpdCAgbWFpbC1yZWFkeSByZXByZXNlbnRhdGlvbi4=
Like I said, the algorithm is pretty simple, just take 3 bytes of existing data, then make 4 6-bit pieces
and use that to obtain the encoding. Read some RFCs or look at the source code that I will put up soon.
.NET frameworks offers the same functionality with Convert class that belongs to System namespace. I still
like using my own version though :)