If you are looking for a cryptography solutions in your project, whether it is Blowfish, DES, RC2 or RC4, perhaps, you can consider to utilize the crypto library which is provided by openssl. In this tutorial, i am going to show you how it can be easily implemented into the C++ project solution. If you are new to OpenSSL or you haven’t compile the OpenSSL source right now, i’d strongly recommend you to read my previous post at the following link: Compiling and Installing OpenSSL for 32 bit Windows Let’s start the tutorial with the following steps:
  1. Create a Win32 console application. I called my demo application “OpenSSLCryptoSample”. Btw, I am using VS C++ 2010 express in this tutorial.
  2. Make sure the following settings are setup in the project property pages:
    [C/C++ -> General -> Additional Include Directories]
    value: OpenSSL’s include directory in your machine (e.g C:\openssl\include\)
    [Linker -> General -> Additional Library Directories]
    value: OpenSSL’s lib directory in your machine (e.g C:\openssl\lib\)
    [Linker -> Input -> Additional Dependencies]
    value: libeay32.lib
  3. Download and add the following header file into the project. This header file will consists of the core function to encrypt and decrypt the given string. Following is the code snippet of the header file.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    /* osslcrypto.cpp
     *
     * Copyright (c) 2012 askyb.com
     *
     * All rights reserved
     */


    #include <openssl/evp.h>

    /* Please choose an encryption mode in the following defines value.
       Only 1 encryption mode is allow at a time. Blowfish CBC mode is
       the default encryption in this file.  
     */


    // BlowFish
    #define MY_CIPHER_MODE EVP_bf_cbc()   // Blowfish CBC mode
    //#define MY_CIPHER_MODE EVP_bf_ecb()   // Blowfish ECB mode

    // DES
    //#define MY_CIPHER_MODE EVP_des_cbc()    // DES CBC mode
    //#define MY_CIPHER_MODE EVP_des_ecb()    // DES ECB mode
    //#define MY_CIPHER_MODE EVP_des_ede()    // DES EDE mode
    //#define MY_CIPHER_MODE EVP_des_ede3()   // DES EDE3 mode

    // RC2
    //#define MY_CIPHER_MODE EVP_rc2_cbc()    // RC2 CBC mode
    //#define MY_CIPHER_MODE EVP_rc2_ecb()    // RC2 ECB mode

    // RC4
    //#define MY_CIPHER_MODE EVP_rc4()      // RC4 mode
    //#define MY_CIPHER_MODE EVP_rc4_40()   // RC4 40 mode


    bool do_encrypt(const char *in, unsigned char *out, int *outlen, unsigned char *key, unsigned char *iv)
    {
      int buflen, tmplen;

      EVP_CIPHER_CTX ctx;
        EVP_CIPHER_CTX_init(&ctx);
        EVP_EncryptInit_ex(&ctx, MY_CIPHER_MODE, NULL, key, iv);

      if(!EVP_EncryptUpdate(&ctx, out, &buflen, (unsigned char*)in, strlen(in)))
        {
            return false;
      }

      if(!EVP_EncryptFinal_ex(&ctx, out + buflen, &tmplen))
        {
            return false;
      }
       
      buflen += tmplen;
      *outlen = buflen;
        EVP_CIPHER_CTX_cleanup(&ctx);

      return true;
    }

    bool do_decrypt(const unsigned char *in, unsigned char *out, int inlen, unsigned char *key, unsigned char *iv)
    {
      int buflen, tmplen;

      EVP_CIPHER_CTX ctx;
      EVP_CIPHER_CTX_init(&ctx);
      EVP_DecryptInit_ex(&ctx, MY_CIPHER_MODE, NULL, key, iv);
       
      if(!EVP_DecryptUpdate(&ctx, out, &buflen, in, inlen))
      {
        return false;
      }
     
      if(!EVP_DecryptFinal_ex(&ctx, out + buflen, &tmplen))
        {
            return false;
      }
     
      int decryptedLength = buflen + tmplen;
        out[decryptedLength] = '\0';
     
      EVP_CIPHER_CTX_cleanup(&ctx);

      return true;
    }
  4. Now enter the following code into your project main function:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    /* OpenSSLCryptoSample.cpp
     *
     * Copyright (c) 2012 askyb.com
     *
     * All rights reserved
     */


    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include "osslcrypto.h"

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
      unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
      unsigned char iv[] = {1,2,3,4,5,6,7,8};

      cout << "askYB.com Encryption Example" << endl;
      cout << "+++++++++++++++++++++++++++\n" << endl;

      string mystr;
      cout << "Enter string to encrypt: ";
      getline (cin, mystr);

      int outlen;
      unsigned char pEncryptedStr[1024];
      unsigned char pDecryptedStr[1024];
      memset(pEncryptedStr, 0, sizeof(pEncryptedStr));
      memset(pDecryptedStr, 0, sizeof(pDecryptedStr));

      do_encrypt(mystr.c_str(), pEncryptedStr, &outlen, key, iv);
      cout << "After encrypted: " << pEncryptedStr << endl;

      cout << "\n\n--- Press ENTER to decrypt ---" << endl;
      getchar();

      do_decrypt(pEncryptedStr, pDecryptedStr, outlen, key, iv);
      cout << "After decrypted: " << pDecryptedStr << endl;

      getchar();
      return 0;
    }
  5. The project may compile sucessfull now. BUT, if you try to run it, it will complaint that the application can’t start because LIBEAY32.DLL is missing. To address this issue, you may copy LIBEAY.DLL, which you can found it in the OpenSSL bin folder, to the project’s debug/release folder
  6. Compile and run the project now and you should see the following screenshot:   MFC May 4, 2012 askyb No Comments Using OpenSSL Cryptography
Download sample project: OpenSSLCryptoSample.zip