Implementation of DES Algorithm C Program

By | June 10, 2016

Simple C Program For DES Algorithm in Cryptography

Learn how to implement DES algorithm in C programming language. The DES encryption algorithm is an implementation of Fiestel Cipher. There are two different methods enlisted here for DES algorithm implementation in C programming.

What is DES Encryption Algorithm?

The DES algorithm is also sometimes referred to as Data Encryption Algorithm (DEA). The DES encryption algorithm is a symmetric key algorithm for the encryption of data. The block size is of 64 bits.

The DES is an archetypal block cipher which takes a fixed length string of plain-text bits. There’s another improvised version of this algorithm which is Triple DES Algorithm.

The simplified DES (S-DES) is a modified version of the data encryption standard DES algorithm. Another modified version of the DES algorithm is famously known as Triple DES. The key generator method creates 16 48-bit keys.

C Program For DES Algorithm

Note: This implementation of simplified data encryption standard in C programming language is compiled with GNU GCC compiler on Linux Ubuntu 14.04 operating system.

Method 1: C Program To Implement Data Encryption Standard Algorithm

#include "custom_header.h"
#include <stdio.h>
#include <stdlib.h>

int key_shift_sizes[] = {-1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};

int key_permutation[] = {
                         57, 49, 41, 33, 25, 17, 9,
                         1, 58, 50, 42, 34, 26, 18,
                         10, 2, 59, 51, 43, 35, 27,
                         19, 11, 3, 60, 52, 44, 36,
                         63, 55, 47, 39, 31, 23, 15,
                         7, 62, 54, 46, 38, 30, 22,
                         14, 6, 61, 53, 45, 37, 29,
                         21, 13, 5, 28, 20, 12, 4 };

int message_permutation[] = {	
                             58, 50, 42, 34, 26, 18, 10, 2,
                             60, 52, 44, 36, 28, 20, 12, 4,
                             62, 54, 46, 38, 30, 22, 14, 6,
                             64, 56, 48, 40, 32, 24, 16, 8,
                             57, 49, 41, 33, 25, 17,  9, 1,
                             59, 51, 43, 35, 27, 19, 11, 3,
                             61, 53, 45, 37, 29, 21, 13, 5,
                             63, 55, 47, 39, 31, 23, 15, 7 };

int sub_key_permutation[] = {
                             14, 17, 11, 24, 1, 5,
                             3, 28, 15, 6, 21, 10,
                             23, 19, 12,  4, 26, 8,
                             16, 7, 27, 20, 13, 2,
                             41, 52, 31, 37, 47, 55,
                             30, 40, 51, 45, 33, 48,
                             44, 49, 39, 56, 34, 53,
                             46, 42, 50, 36, 29, 32 };

int message[] =  {
                  32, 1, 2, 3, 4, 5,
                  4, 5, 6, 7, 8, 9,
                  8, 9, 10, 11, 12, 13,
                  12, 13, 14, 15, 16, 17,
                  16, 17, 18, 19, 20, 21,
                  20, 21, 22, 23, 24, 25,
                  24, 25, 26, 27, 28, 29,
                  28, 29, 30, 31, 32, 1 };

int array_a1[] = {
                  14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
                  0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
                  4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
                  15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 };

int array_a2[] = {
                  15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
                  3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
                  0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
                  13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 };

int array_a3[] = {
                  10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
                  13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
                  13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
                  1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 };

int array_a4[] = { 
                  7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
                  13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
                  10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
                  3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14 };

int array_a5[] = { 
                  2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
                  14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
                  4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
                  11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3 };

int array_a6[] = {
                  12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
                  10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
                  9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
                  4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13 };

int array_a7[] = { 
                  4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
                  13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
                  1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
                  6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12 };

int array_a8[] = {
                  13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
                  1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
                  7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
                  2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11 };

int intermediate_permutation[] = {
                                  16,  7, 20, 21,
                                  29, 12, 28, 17,
                                  1, 15, 23, 26,
                                  5, 18, 31, 10,
                                  2,  8, 24, 14,
                                  32, 27,  3,  9,
                                  19, 13, 30,  6,
                                  22, 11,  4, 25 };

int end_permutation[] = {
                         40,  8, 48, 16, 56, 24, 64, 32,
                         39,  7, 47, 15, 55, 23, 63, 31,
                         38,  6, 46, 14, 54, 22, 62, 30,
                         37,  5, 45, 13, 53, 21, 61, 29,
                         36,  4, 44, 12, 52, 20, 60, 28,
                         35,  3, 43, 11, 51, 19, 59, 27,
                         34,  2, 42, 10, 50, 18, 58, 26,
                         33,  1, 41,  9, 49, 17, 57, 25 };

void make_keys(unsigned char* key) 
{
      int count;
      for(count = 0; count < 8; count++) 
      {
            key[count] = rand()%255;
      }
}

void binary_digits(char input) 
{
      int count;
      for(count = 0; count < 8; count++) 
      {
            char shift_byte = 0x01 << (7 - count);
            if(shift_byte & input) 
            {
                  printf("1");
            } 
            else 
            {
                  printf("0");
            }
      }
}

void display_keys(key_set key_set)
{
      int count;
      printf("Y: \n");
      for(count = 0; count < 8; count++) 
      {
            printf("%02X : ", key_set.temp[count]);
            binary_digits(key_set.temp[count]);
            printf("\n");
      }
      printf("\nX: \n");
      for(count = 0; count < 4; count++) 
      {
            printf("%02X : ", key_set.y[count]);
            binary_digits(key_set.y[count]);
            printf("\n");
      }
      printf("\nZ: \n");
      for(count = 0; count < 4; count++) 
      {
            printf("%02X : ", key_set.z[count]);
            binary_digits(key_set.z[count]);
             printf("\n");
      }
      printf("\n");
}

void key_maker(unsigned char* main_key, key_set* key_sets) 
{
      int m, n;
      int shift_size;
      unsigned char shift_byte, first_bit, second_bit, third_bit, fourth_bit;
      for(m = 0; m < 8; m++) 
      {
            key_sets[0].temp[m] = 0;
      }
      for(m = 0; m < 56; m++) 
      {
            shift_size = key_permutation[m];
            shift_byte = 0x80 >> ((shift_size - 1)%8);
            shift_byte &= main_key[(shift_size - 1)/8];
            shift_byte <<= ((shift_size - 1)%8);
            key_sets[0].temp[m/8] |= (shift_byte >> m%8);
      }
      for(m = 0; m < 3; m++) 
      {
            key_sets[0].y[m] = key_sets[0].temp[m];
      }
      key_sets[0].y[3] = key_sets[0].temp[3] & 0xF0;
      for(m = 0; m < 3; m++) 
      {
            key_sets[0].z[m] = (key_sets[0].temp[m + 3] & 0x0F) << 4;
            key_sets[0].z[m] |= (key_sets[0].temp[m + 4] & 0xF0) >> 4;
      }
      key_sets[0].z[3] = (key_sets[0].temp[6] & 0x0F) << 4;
      for(m = 1; m < 17; m++) 
      {
            for(n = 0; n < 4; n++) 
            {
                  key_sets[m].y[n] = key_sets[m - 1].y[n];
                  key_sets[m].z[n] = key_sets[m - 1].z[n];
            }
            shift_size = key_shift_sizes[i];
            if(shift_size == 1)
            {
                  shift_byte = 0x80;
            } 
            else 
            {
                  shift_byte = 0xC0;
            }
            first_bit = shift_byte & key_sets[m].y[0];
            second_bit = shift_byte & key_sets[m].y[1];
            third_bit = shift_byte & key_sets[m].y[2];
            fourth_bit = shift_byte & key_sets[m].y[3];
            key_sets[m].y[0] <<= shift_size;
            key_sets[m].y[0] |= (second_bit >> (8 - shift_size));
            key_sets[m].y[1] <<= shift_size;
            key_sets[m].y[1] |= (third_bit >> (8 - shift_size));
            key_sets[m].y[2] <<= shift_size;
            key_sets[m].y[2] |= (fourth_bit >> (8 - shift_size));
            key_sets[m].y[3] <<= shift_size;
            key_sets[m].y[3] |= (first_bit >> (4 - shift_size));
            first_bit = shift_byte & key_sets[m].z[0];
            second_bit = shift_byte & key_sets[m].z[1];
            third_bit = shift_byte & key_sets[m].z[2];
            fourth_bit = shift_byte & key_sets[m].z[3];
            key_sets[m].z[0] <<= shift_size;
            key_sets[m].z[0] |= (second_bit >> (8 - shift_size));
            key_sets[m].z[1] <<= shift_size;
            key_sets[m].z[1] |= (third_bit >> (8 - shift_size));
            key_sets[m].z[2] <<= shift_size;
            key_sets[m].z[2] |= (fourth_bit >> (8 - shift_size));
            key_sets[m].z[3] <<= shift_size;
            key_sets[m].z[3] |= (first_bit >> (4 - shift_size));
            for(n = 0; n < 48; n++) 
            {
                  shift_size = sub_key_permutation[n];
                  if(shift_size <= 28) 
                  {
                        shift_byte = 0x80 >> ((shift_size - 1)%8);
                        shift_byte &= key_sets[m].y[(shift_size - 1)/8];
                        shift_byte <<= ((shift_size - 1)%8);
                  } 
                  else 
                  {
                        shift_byte = 0x80 >> ((shift_size - 29)%8);
                        shift_byte &= key_sets[m].z[(shift_size - 29)/8];
                        shift_byte <<= ((shift_size - 29)%8);
                  }
                  key_sets[m].temp[n/8] |= (shift_byte >> n%8);
            }
      }
}

void evaluate_message(unsigned char* message_piece, unsigned char* processed_piece, key_set* key_sets, int mode) 
{
      unsigned char initial_permutation[8], final_permutation[8];
      unsigned char row, column, shift_byte;
      unsigned char ln[4], rn[4], er[6], ser[4], l[4], r[4];
      int count, temp, key_position, shift_size;
      memset(initial_permutation, 0, 8);
      memset(processed_piece, 0, 8);
      for(count = 0; count < 64; count++) 
      {
            shift_size = message_permutation[count];
            shift_byte = 0x80 >> ((shift_size - 1)%8);
            shift_byte &= message_piece[(shift_size - 1)/8];
            shift_byte <<= ((shift_size - 1)%8);
            initial_permutation[count/8] |= (shift_byte >> count%8);
      }
      for(count = 0; count < 4; count++) 
      {
            l[count] = initial_permutation[count];
            r[count] = initial_permutation[count + 4];
      }
      for(temp = 1; temp <= 16; temp++) 
      {
            memcpy(ln, r, 4);
            memset(er, 0, 6);
            for(count = 0; count < 48; count++) 
            {
                  shift_size = message[count];
                  shift_byte = 0x80 >> ((shift_size - 1)%8);
                  shift_byte &= r[(shift_size - 1)/8];
                  shift_byte <<= ((shift_size - 1)%8);
                  er[count/8] |= (shift_byte >> count%8);
            }
            if(mode == DECRYPTION_MODE) 
            {
                  key_position = 17 - temp;
            } 
            else 
            {
                  key_position = temp;
            }
            for(count = 0; count < 6; count++) 
            {
                  er[count] ^= key_sets[key_position].temp[count];
            }
            for(count = 0; count < 4; count++) 
            {
                  ser[count] = 0;
            }
            row = 0;
            row |= ((er[0] & 0x80) >> 6);
            row |= ((er[0] & 0x04) >> 2);
            column = 0;
            column |= ((er[0] & 0x78) >> 3);
            ser[0] |= ((unsigned char)array_a1[row*16 + column] << 4);
            row = 0;
            row |= (er[0] & 0x02);
            row |= ((er[1] & 0x10) >> 4);
            column = 0;
            column |= ((er[0] & 0x01) << 3);
            column |= ((er[1] & 0xE0) >> 5);
            ser[0] |= (unsigned char)array_a2[row*16 + column];
            row = 0;
            row |= ((er[1] & 0x08) >> 2);
            row |= ((er[2] & 0x40) >> 6);
            column = 0;
            column |= ((er[1] & 0x07) << 1);
            column |= ((er[2] & 0x80) >> 7);
            ser[1] |= ((unsigned char)array_a3[row*16 + column] << 4);
            row = 0;
            row |= ((er[2] & 0x20) >> 4);
            row |= (er[2] & 0x01);
            column = 0;
            column |= ((er[2] & 0x1E) >> 1);
            ser[1] |= (unsigned char)array_a4[row*16 + column];
            row = 0;
            row |= ((er[3] & 0x80) >> 6);
            row |= ((er[3] & 0x04) >> 2);
            column = 0;
            column |= ((er[3] & 0x78) >> 3);
            ser[2] |= ((unsigned char)array_a5[row*16 + column] << 4);
            row = 0;
            row |= (er[3] & 0x02);
            row |= ((er[4] & 0x10) >> 4);
            column = 0;
            column |= ((er[3] & 0x01) << 3);
            column |= ((er[4] & 0xE0) >> 5);
            ser[2] |= (unsigned char)array_a6[row*16 + column];
            row = 0;
            row |= ((er[4] & 0x08) >> 2);
            row |= ((er[5] & 0x40) >> 6);
            column = 0;
            column |= ((er[4] & 0x07) << 1);
            column |= ((er[5] & 0x80) >> 7);
            ser[3] |= ((unsigned char)array_a7[row*16 + column] << 4);
            row = 0;
            row |= ((er[5] & 0x20) >> 4);
            row |= (er[5] & 0x01);
            column = 0;
            column |= ((er[5] & 0x1E) >> 1);
            ser[3] |= (unsigned char)array_a8[row*16 + column];
            for(count = 0; count < 4; count++) 
            {
                  rn[count] = 0;
            }
            for(count = 0; count < 32; count++) 
            {
                  shift_size = intermediate_permutation[count];
                  shift_byte = 0x80 >> ((shift_size - 1)%8);
                  shift_byte &= ser[(shift_size - 1)/8];
                  shift_byte <<= ((shift_size - 1)%8);
                  rn[count/8] |= (shift_byte >> count%8);
            }
            for(count = 0; count < 4; count++) 
            {
                  rn[count] ^= l[count];
            }
            for(count = 0; count < 4; count++) 
            {
                  l[count] = ln[count];
                  r[count] = rn[count];
            }
      }
      for(count = 0; count < 4; count++) 
      {
            final_permutation[count] = r[count];
            final_permutation[4 + count] = l[count];
      }
      for(count = 0; count < 64; count++) 
      {
            shift_size = end_permutation[count];
            shift_byte = 0x80 >> ((shift_size - 1)%8);
            shift_byte &= final_permutation[(shift_size - 1)/8];
            shift_byte <<= ((shift_size - 1)%8);
            processed_piece[count/8] |= (shift_byte >> count%8);
      }
}

Custom Header File

#ifndef _DES_H_
#define _DES_H_

#define ENCRYPTION_MODE 1
#define DECRYPTION_MODE 0

typedef struct 
{
      unsigned char k[8];
      unsigned char c[4];
      unsigned char d[4];
} key_set;

void generate_key(unsigned char* key);
void generate_sub_keys(unsigned char* main_key, key_set* key_sets);
void process_message(unsigned char* message_piece, unsigned char* processed_piece, key_set* key_sets, int mode);

#endif

Method 2: C Program To Implement DES Encryption Algorithm

#include<stdio.h>

int main()
{
      int array_a1[30], array_a2[30], array_a3[30], array_a4[30], array_a5[30], array_a6[30], array_a7[30], array_a8[30];
      int div, count, j, key, m, plaintext, temp, dec = 0;
      printf("\nEnter a Plain-Text value:\t");
      scanf("%d", &plaintext);
      printf("\nEnter the Key:\t");
      scanf("%d", &key);
      printf("\nEnter the Bit-Stream\n");
      for(count = 0; count < plaintext; count++)
      {
            scanf("%d", &array_a1[count]);
      }
      div = plaintext / 2;
      temp = div - key;
      for(count = 0; count <= temp; count++)
      {
            array_a3[count] = 0;
            dec++;
      }
      dec = dec - 1;
      printf("Enter the Key Bit Stream\n");
      for(count = 0; count < key; count++)
      {
            scanf("%d", &array_a3[dec++]);
      }
      for(count = 0; count < 2; count++)
      {
            printf("%d", array_a8[count]);
      }
      printf("Left Hand\n");
      for(count = 0; count < div; count++)
      {
            array_a5[count] = array_a1[count];
            printf("%d", array_a1[count]);
      }
      printf("Right Hand\n");
      for(count = div; count < plaintext; count++)
      {
            array_a2[count] = array_a1[count];
            printf("%d", array_a1[count]);
      }
      for(j = 0, m = div; j < dec, m < plaintext; j++, m++)
      {
            if(array_a2[m] == 1 && array_a3[j] == 1)
            {
                  array_a6[j] = 0;
            }
            else if(array_a2[m] == 1 && array_a3[j] == 0)
            {
                  array_a6[j] = m;
            }
            else
            {
                  array_a6[j] = 0;
            }
      }
      printf("\nFirst XOR\n");
      for(count = 0; count < div; count++)
      {
            printf("%d", array_a6[count]);
      }
      for(j = 0, m = 0; j < div, j++; j++, m++)
      {
            if(array_a5[m] = 1 && array_a6[j] == 1)
            {
                  array_a4[j] = 0;
            }
            else if(array_a5[m] = 1 && array_a6[j] == 0)
            {
                  array_a4[j] = m;
            }
            else if(array_a5[m] == 0 && array_a6[j] == 1)
            {
                  array_a4[j] = 0;
            }
      }
      printf("\nSecond XOR\n");
      for(count = 0; count < div; count++)
      {
            printf("%d", array_a4[j]);
      }
      return 0;
}

If you have any doubts or compilation errors in this C program to implement DES encryption algorithm, let us know about it in the comment section below. Find more about DES algorithm on Wikipedia.

Recommended Programs
C Program For Caesar Cipher Algorithm Implementation
C Program To Implement Banker’s Algorithm
C Program To Encrypt and Decrypt Text Files
C Program To Solve Lucas Tower Problem Algorithm
C Program To Find Execution Time of a Program
C Program To Find First and Follow of Grammar
C Program For Tower of Hanoi Algorithm
C Program To Find Permutations of a String
C Program For N-Queens Problem
C Program To Evaluate a Prefix Expression

9 thoughts on “Implementation of DES Algorithm C Program

  1. Rishi Kadam

    What are the different types of algorithms which can be an alternative to DES Encryption algorithm?

    Reply
    1. Sachin Bhoi

      There are many alternatives to the DES algorithm such as:
      – BlowFish Algorithm
      64 bits
      – TwoFish Algorithm
      256 bits
      – RSA Algorithm
      – AES (Advanced Encryption Standard) –
      192, 256 and 128 bits
      – Triple DES
      68 bits, 112 bits

      Reply
  2. Sudhakar Shetty

    This is such a simple DES algorithm. Thanks for this one. Amazing it is.

    Reply
  3. Santosh Kumar

    There is another very good algorithm which has been developed to improve the DES algorithm and it is IDEA which stands for International Date Encryption System.

    Reply
  4. Ashwini

    HI,
    can you suggest how to execute above code.
    I see you wrote 3 program.

    Reply

Let's Discuss