Portable Cryptography API for Triple DES

DES (Data Encryption Standard) is an industrial strength symmetric block cipher.

Included is an implementation of DES and triple DES (which is much stronger than DES) cryptography. It can be used to encrypt/decrypt using CBC (chain block ciphering) or ECB (Electronic Code Book). CBC is a stronger method because the results of each 64 block encryption are used for the next.

It’s implemented in ANSI C++, so it can be compiled on any platform. I’ve also included an ANSI C version for platforms without a C++ compiler). The C++ version has been written as a template class simply because it negates having to include a seperate Cpp file (and I’m lazy); it doesn’t require any template parameters.

DES requires a private 8-byte key for encryption/decryption. Triple DES requires two private 8-byte keys for encryption/decryption. To use it in its simplist form, follow this code:

#include "McbDES2.hpp"
#include &ltstdio.h>

void McbTestTripleDES()
   unsigned char * lpKey1 = (unsigned char*)"11111111";
   unsigned char * lpKey2 = (unsigned char*)"22222222";

   McbDES desEncrypt;


   if (desEncrypt.McbEncrypt("Encrypted with triple DES"))
      McbDES desDecrypt;



      printf("Decryption (%d) bytes: %s, n",

The above example uses the default options of triple DES and PKCS#5 padding to encrypt a block of text. For clarification, the cryptogram is allocated and managed in an instance of the DES class then passed to another instance that performs the decryption. Obviously, this is not a real world example because typically the cryptogram would be squirreled away somewhere or transmitted over a network. Usually, the keyz would be stored elsewhere or perhaps generated from a hashing function based on some user input.

A buffer also can be supplied to the object rather than having the object manage the cryptogram or plaintext. An example of this can be seen in McbMain.cpp, where a stl string is used as the buffer.


More by Author

Must Read