Portable Cryptography API for Triple DES | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 22, 2004
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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;

   desEncrypt.McbSetKey1(lpKey1);
   desEncrypt.McbSetKey2(lpKey2);

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

      desDecrypt.McbSetKey1(lpKey1);
      desDecrypt.McbSetKey2(lpKey2);

      desDecrypt.McbDecrypt(desEncrypt.McbGetCryptogram(),
         desEncrypt.McbGetCryptogramSize());

      printf("Decryption (%d) bytes: %s, n",
             desDecrypt.McbGetPlainTextSize(),
             desDecrypt.McbGetPlainText());
   }
}

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.

Enjoy…

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.