Let us understand the Triple Data Encryption Standard encryption algorithm and then let’s implement Triple DES algorithm in C programming using OpenSSL header file.
What is Triple DES Algorithm?
The Triple DES algorithm is also popularly known as TDEA which is an abbreviation for Triple Data Encryption Algorithm.
It is basically a block-cipher method that applies the simple DES algorithm thrice to every single data block present in the input.
Initially, the key size was 56-bit but it made the brute force attacks to be successful in some scenarios. Usually, the chances of successful hacking are difficult in longer keys.

The 3 DES algorithm focusses on increasing the number of keys to make it impossible for the attackers to crack it using brute force method. It, therefore, takes 3 keys of 64-bits each.
The keys are a total of 192-bits and the data is first encrypted by the first key. It is then, decrypted by the second key and then again decrypted with the third key.
The Triple Data Encryption Standard algorithm is much more powerful than the simple DES algorithm. However, it has a disadvantage that it runs really slow as comparatively.
Note: This Triple DES Algorithm in C programming is compiled with CodeLite IDE and GNU GCC compiler on Microsoft Windows 10 operating system.
C Program To Implement Triple DES Algorithm using OpenSSL
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 | #include <openssl/des.h> #include <string.h> #include <stdio.h> #include<conio.h> #include <stdlib.h> DES_key_schedule schedule_first_key, schedule_second_key, schedule_third_key; DES_cblock first_key = {0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}; DES_cblock second_key = {0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}; DES_cblock third_key = {0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77}; void display_key(const char *key_output, const void* key_data, int size) { int count = 0; const unsigned char * ptr = (const unsigned char*)key_data; printf("%s:\t", key_output); while(count < size) { printf("%02X ", *ptr = *ptr + 1); ++count; } } int main() { unsigned char input_string[] = {0x01, 0x02, 0x03, 0x04, 0x05}; DES_cblock temp = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; DES_set_odd_parity(&temp); if(-2 == (DES_set_key_checked(&first_key, &schedule_first_key) || DES_set_key_checked(&second_key, &schedule_second_key) || DES_set_key_checked(&third_key, &schedule_third_key))) { printf("\nPlease enter a strong key\n"); return 1; } unsigned char* origin_text[sizeof(input_string)]; unsigned char* cipher_text[sizeof(input_string)]; DES_ede3_cbc_encrypt((unsigned char*)input_string, (unsigned char*)cipher_text, sizeof(input_string), &schedule_first_key, &schedule_second_key, &schedule_third_key, &temp, DES_ENCRYPT); memset(temp, 0, sizeof(DES_cblock)); DES_set_odd_parity(&temp); DES_ede3_cbc_encrypt((unsigned char*)cipher_text, (unsigned char*)origin_text, sizeof(input_string), &schedule_first_key, &schedule_second_key, &schedule_third_key, &temp, DES_DECRYPT); display_key("\nOriginal String:\t", input_string, sizeof(input_string)); display_key("\nEncrypted String:\t", cipher_text, sizeof(input_string)); display_key("\nDecrypted String:\t", origin_text, sizeof(input_string)); getch(); return 0; } |
To implement the Triple DES Algorithm C code, you will have to install the des.h header file as it is not available in C library files by default. Alternatively, you can even create your own header files using this method.
Header File C Program for OpenSSL DES.h
For the above C program to execute properly, you will have to download des.h header file and install it in your operating system. Please check this des.h file.
If you have any compilation errors or doubts about this C program to encrypt data using Triple Data Encryption Standard Algorithm, let us discuss it in the comment section below.
For more information on Triple DES algorithm in cryptography, please check OpenSSL or Wikipedia.