User Guide: base64.h

From Crypto++ Wiki
Jump to: navigation, search

base64.h

Base64Encoder is a filter that takes any data as input and encodes it with the base-64 algorithm, optionally appending a line break after each full line (72 characters).

Note that, in the version of Crypto++ that is available at the time of this writing, Base64Encoder does not automatically encode buffered data on destruction. Therefore, if a chain of Filter objects includes Base64Encoder and no member of that chain that appears before Base64Encoder will call MessageEnd() automatically, it is necessary to terminate input by explicitly calling MessageEnd(). (The nPropagation argument to the MessageEnd() method can be used if propagation of the message-end signal to other filter objects attached to Base64Encoder is not desired.)

Base64Decoder is a filter that takes base-64 encoded data as input, with optional line breaks, and outputs the decoded binary data.

Usage:

 // This code does not work for current versions of CryptoPP.
 // Binary data
 byte const* pbData = ...;
 unsigned int nDataLen = ...;
 
 // Encode with line breaks (default).
 // Ordinarily, we would now have to call MessageEnd() to flush
 // Base64Encoder's buffer. However, the 'true' parameter will cause
 // the StringSource::PumpAll() method to be called, and that method
 // will cause MessageEnd() to be triggered implicitly.
 StringSource(pbData, nDataLen, true,
    new Base64Encoder(
        new FileSink("base64-encoded.dat")));
 
 // Encode without line breaks.
 StringSource(pbData, nDataLen, true,
    new Base64Encoder(
        new FileSink("base64-encoded.dat"),
        false));

To find other topics in the User Guide, visit Category:User Guide.