Here is simple “How to do Triple-DES CBC mode encryption example in c programming with OpenSSL”

First you need to download standard cryptography library called OpenSSL to perform robust Triple-DES(Data Encryption Standard) encryption, But before that i will tell you to take a look at simple C code for Triple-DES encryption and decryption, so that you are familiar with DES cryptography APIs which is quite simple. Here i use Triple-DES CBC mode Encryption




There are two phases to the use DES encryption. The first is the generation of a DES_key_schedule from a key(8 bytes with odd parity) which is of type DES_cblock, the second phase is the actual encryption. The least significant bit in each byte is the parity bit.


The key schedule is an expanded form of the key; it is used to speed the encryption process.


DES_set_odd_parity() sets the parity of the passed key to odd.


Before a DES key can be used, it must be converted into the architecture dependent DES_key_schedule via the DES_set_key_checked() or DES_set_key_unchecked() function. DES_set_key_checked() will check that the key passed is of odd parity and is not a week or semi-weak key. If the parity is wrong, then -1 is returned. If the key is a weak key, then -2 is returned. If an error is returned, the key schedule is not generated.


DES_ede3_cbc_encrypt() implements main algorithm for triple CBC DES encryption with three keys.

Compiling and Installing OpenSSL

Before compiling this code, you need OpenSSL library which you can download from here

i am using openssl-1.0.1i which i have downloaded in form of tar file because my development OS is Linux(Ubuntu). So after downloading tar file we have to compile and install OpenSSL. To do so follow instruction below.


that’s it ! you have successfully compiled and installed OpenSSL.

Now create the file with above sample code and compile that with gcc compiler by using this gcc directives :


and run the binary you will see following output


Original     : 00 01 02 03 04 05

Encrypted : D5 40 D0 BB 16 1D

Decrypted : 00 01 02 03 04 05



More Reading

Suggested Reading

  1. How to do AES-128 bit CBC mode encryption in c programming code with OpenSSL