Base64

base64 is another handy little tool that converts binary data to Base64 encoding and vice versa. It is especially suited to handle cryptographic data (certificates, CRLs, keys, etc.) that is commonly transported in either binary format (DER) or Base64 (PEM) encoding.

The Base64 encoding is a three-byte to four-characters encoding based on an alphabet of 64 characters. This encoding has been introduced in PEM (RFC1421) and MIME. Other uses include HTTP Basic Authentication Headers and general binary-to-text encoding applications.

Note that the Bas64 encoding expands binary data by 33%, which is quite efficient.

Download
The program (a single C source file) is available here: base64.c. A pre-compiled WIN32 binary can be downloaded here: base64.exe.
Installation
Compile it using gcc -O2 -o base64 base64.c, then put the executable somewhere in your $PATH. Optionally, in the same directory make a symbolic link to it under the name b642bin like this: ln -s base64 b642bin. Calling the program by the name b642bin pre-selects the decoding function instead of the default encoding.
Synopsis
Base64 [options] [input file] [output file]
  options are:
  -i <filename> input file (default: stdin)
  -o <filename> output file (default: stdout)
  -e            encode binary to Base64 (default)
  -d            decode Base64 to binary
  -n <n>        encode n characters per line (0:no line breaks,default:64)
  -E <STRING>   encode and put -----BEGIN/END <STRING>----- around output
  --            indicate end of options
Call as b642bin to preselect decoding
(c) Matthias Gaertner 2002 - v1.00
Examples
Encode CRL file to Base64 on stdout
base64 foo.crl
base64 -e foo.crl
base64 -in foo.crl
base64 -e -in foo.crl
Encode certificate file to PEM-ish Base64 output file foo.pem
base64 -E "CERTIFICATE" foo.crt foo.pem
Encode file, output no line breaks in Base64 area. Not recommended with -E.
base64 -n 0 foo.crt
Encode file, maximum number of characters per line is 32.
base64 -n 32 foo.crt
Decode file to output file
base64 -d foo.pem foo.crt
base64 -d -i foo.pem foo.crt
base64 -d -i foo.pem -o foo.crt
Decode file and pipe to next program
base64 -d foo.pem | md5sum
ls / | base64 | base64 -d
Invoke as b642bin: decoding is the default.
b642bin foo.pem foo.bin
Example Output
$ base64 -E CERTIFICATE thawte-personal-freemail-2000.8.30.cer
-----BEGIN CERTIFICATE-----
MIIDKTCCApKgAwIBAgIBDDANBgkqhkiG9w0BAQQFADCB0TELMAkGA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IFICATE-----

$ ls | md5sum
8d7ddd66502958debc9b2b133e583de5
$ ls | base64 | b642bin | md5sum
8d7ddd66502958debc9b2b133e583de5
Notes

When decoding, this decoder ignores all non-Base64 caharcters. After a hyphen (-) all characters are ignored up to the next \n. The first non-ignored equals sign (=) indicates the end of b64 data.

These settings allow the decoder to read PEM-encoded objects directly without first having to strip off the leading and trailing lines. Conversely, the -E option is intended for easy creation of such objects.

Related Stuff
Copyright

This code is released under the GPL.

Back to main page. Back to software page.


 
This page was last changed on June 29th, 2002. © Matthias Gärtner 2002