Implementation of Base 64 in C

While not a cryptographic algorithm, this implementation is a part of my cryptography implementations project because it's a handy encoding. The full project, along with licensing information and more detail, is hosted on GitHub.

Algorithm

This is an implementation of Base64 encoding.

Base64 is an encoding scheme that allows binary data to be represented as text. It accepts a bit string of any length, splits it up into chucks of 6 bits, and replaces each chunk with its corresponding value from the list of characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ When the input is not an even multiple of 6 bits, the character '=' is appended to the output as padding as needed.

Since the input has 8 bits of significant data and the output has 6 significant bits of data (there are 26=64 characters in the array above), the input to output ratio is very close to 3:4.

Documentation

  • void base64_encode(unsigned char in[], unsigned char out[], int len)
    This encodes raw data to Base64.
    • unsigned char in[]
      This contains the data to be encoded.
    • unsigned char out[]
      This will contains the output. The array should be larger than the input by a scale of 4:3, with a two character buffer.
    • int len
      This is the length, in bytes, of the input.
    • int newline_flag
      This is a flag, which can be set to true or false, to indicate whether or not the function should insert a newline in the base64 output every 76 characters. The technical specification for Base64 calls for this value, but this was based on technical standards/difficulties of the past and isn't always desirable. By setting it to a non-zero value, a newline character will be added every 76 output characters (to wrap it in a rough rectangle shape), if it is set to 0 then the output will be one long string.
  • base64_decode(unsigned char in[], unsigned char out[], int len)
    This function decodes Base64 code into its original form.
    • unsigned char in[]
      This contains the Base64 data to be decoded.
    • unsigned char out[]
      This is the output array, it should be at least 3/4 the length of the input data, with an extra character of buffer.
    • int len
      This is the length of the input Base64 data.
    • Note: There is no need to include a newline flag for the decoding function, as it automatically parses out newline characters.

Usage

  1. First create/allocate two arrays to contain your original and encoded data.
  2. Determine the length of the data you wish to encode. Pass the input data, the array to contain the output, and the length of the data as arguments to the base64_encode() function.
  3. When you wish to decode the data, use the base64_decode() function. It works the same way, except the data gets decoded.

Code

Notes

This is the traditional base64 encoding scheme. The internet version of base64, in order to be URL friendly, replaces the last two characters of the substitution array, "+" and "/", with "*" and "-", respectively.

Update (7-26-06): Thanks to Christoph Otto for fixing a NULL termination bug.