Algorithm Implementation/Miscellaneous/Base64

The traditional (MIME) base64 encoding and decoding processes are fairly simple to implement. Here an example using JavaScript is given, including the MIME/etc required line breaks at particular line lengths. It is worth noting however, that many base64 functions (e.g. in PHP) return base64 encoded strings without the line breaks, as the line breaks can be inserted easily after encoding, and many times the base64 encoding is desired only for safely transferring data via XML or inserting into a database, etc. &mdash; times when the line breaks are known to be unnecessary and therefore undesirable. The newline inserting and removing in these functions here can easily be commented out (they are each only one line in the respective functions) if they are not needed.

An array of the base 64 characters is necessary for encoding, such as: And decoding will require the inverse list (swap the indices for the values), such as:

Note that in real implementations, it is better to explicitly list the entire array/hash for each list above &mdash; the one-liners here are given to demonstrate the concept as directly as possible, rather than being the ideal in practice.

Javascript
The base64 encoding function:

Java
The base64 encoding function:

An alternate implementation could use Java Streams. For example, one could create a Base64EncoderStream, inheriting from FilterOutputStream.

Ruby
Base64 module documentation show the shortest way to encode a string:

Ruby version, implemented as a tool that encodes the file specified on the command line:

C
C version of the base64 encoding function:

C++
Below is a C++ version of Base64Encode. This code is released into public domain.

Javascript
The base64 decoding function: The above implementation is best with a language like JavaScript that handles string concatenation of arbitrary length strings very efficiently. Other languages (e.g. C) will work much more efficiently by allocating memory for a new string/array of the appropriate size (the output string length is easily calculated from the input string at the very beginning) and then simply setting each character index, as opposed to concatenation.

Java
The base64 decoding function:

An alternate implementation could use Java Streams. For example, one could create a Base64DecoderStream, inheriting from FilterInputStream.

C
Counterpart C decoder for the encoder above. This code is also public domain. This solution has been optimized using pointer math and a look-up table. This algorithm handles multiple encoding formats: with and without line breaks, with and without whitespace, and with and without padding characters.

C++
Counterpart C++ decoder for the encoder above. This code is also public domain. While a more general solution is possible by using a table similar to that in encoding, and then finding the index of the character we want in that table, and while that solution is much simpler, this version with the if-else-if ladder is faster because it does not need to run as many character comparisons to find the right character to use. The alphabet specific parts of this code are pretty much the same across most base64 alphabets however.

Items that need changed to accommodate the base64url encoding are commented as such.