# Cryptography | 3-DES implementation in C

In cryptography, Triple DES (3DES) is a symmetric-key block cipher, which applies the Data Encryption Standard (DES) cipher algorithm three times to each data block.

We have already discussed DES algorithm in the previous post. DES is now considered to be insecure for many applications. This is mainly due to the 56-bit effective key size being too small. Triple DES provides a relatively simple method of increasing the key size of DES to protect against such attacks, without the need to design a completely new block cipher algorithm.

Algorithm:

Triple DES uses a “key bundle” that comprises three DES keys, K1, K2 and K3, each of 56 bits (excluding parity bits). The encryption algorithm is:

ciphertext = EK3(DK2(EK1(plaintext)))
i.e., DES encrypt with K1, DES decrypt with K2, then DES encrypt with K3.

Decryption is the reverse:

plaintext = DK1(EK2(DK3(ciphertext)))
i.e., decrypt with K3, encrypt with K2, then decrypt with K1.

INPUT FILES TO THE PROGRAM –

input.txt – THIS FILE SHOULD CONTAIN THE PLAIN TEXT (Max. Limit of plain text is 64kb).

key.txt – IT SHOULD CONTAIN THREE 64-BIT KEY (One after another without spaces).
consider
K1 = 0001001100110100010101110111100110011011101111001101111111110001
K2 = 0000000100100011010001010110011110001001101010111100110111101111
K3 = 0001001100110100010101110111100110011011101111001101111111110001

3-DES implementation –

OUTPUT FILE –
result.txt – IT WILL CONTAIN OUR DECRYPTED TEXT.

TEMP FILES –
1.txt & 2.txt

Above program is tested in Windows environment using Code::Blocks 16.01.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Get great deals at Amazon