Complete DES Encryption and Decryption Program in C
Complete DES Encryption and Decryption Program in C
Kashif Latif
National University of Sciences & Technology, Pakistan
28 June 2010
#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned long long pln_txt, key, k[16], cd = 0, temp = 0, des_out = 0, ep_out48 = 0;
unsigned int l = 0, r = 0, c0 = 0, d0 = 0, sb_out32 = 0, p_out32 = 0, ch = 0;
unsigned char sdata = 0, row = 0, col = 0;
int j = 0, i = 0, round = 0;
static unsigned char pc1[56] = { 57, 49, 41, 33, 25, 17, 9, //PC1
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};
static unsigned char pc2[48] = { 14, 17, 11, 24, 1, 5, //PC2
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};
static unsigned char ip[64] = { 58, 50, 42, 34, 26, 18, 10, 2,//Init. Permutation
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};
static unsigned char ep[48] = { 32, 1, 2, 3, 4, 5, //Exp. Permutation
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};
/////////////////////Permutation//////////////////////////
p_out32 = 0;
for (j=31; j>=0;j--)
p_out32 = p_out32 ^ (((sb_out32 >> (32-p[(31-j)])) & 0x1) << j);
p_out32 = p_out32 ^ l; //Xor with left half
l = r; r = p_out32;
}/////////////////////// End 16 Rounds //////////////////
temp = 0; temp = ((temp ^ r) << 32) ^ l; //32bit SWAP