assigned to SAMHURIS

profileMQSR22
sdes.docx

4. Programming Problem: Please read the programming guidelines (in the course outline) before starting to work on the programming problem

You need to turn in:

• hard copy: a self-critique, a printout of the program listing, a printout of the output

• soft copy: a soft copy (source code + all executables etc) needs to be turned in on blackboard.

You have to implement encryption and decryption with Modified Simplified DES, as discussed in the class and the textbook. The permutations IP, P10, P8, and SW, and the functions fk, F, and the S-box S1 are all as described in the textbook, and can be hardwired into your program. For parts (a) and (b), you use the original S0 box as described in Stallings 3rd edition. However, for part (c), you have to use a modified S-box S0’. In the modified S0’, the rows 0 and 2 are the same as described for the original S0, but the rows 1 and 3 have been switched. So row 1 is 3, 1, 3, 2, and row 3 is 3, 2, 1, 0.

Your program should:

• take as input a 8-bit block of plaintext and a 10-bit key.

• Show the following output (please only print what is being asked for, and nothing else):

(a) the intermediate result after the SW operation while encypting.

(b) the ciphertext. (c) the intermediate result after the SW operation while decrypting.

(d) the result of the decryption process.

• You have to run your programs on the following inputs:

(a) with the original S0: the example from the textbook i.e. the plaintext is 10111101 and the key is 1010000010. In this case we know the ciphertext should come out to be 01110101, so this is a good way to check that your program is performing correctly on this input.

(b) with the original S0: the plaintext is 11001110 and the key is 1001100101.

(c) with the modified S0’: the plaintext is 00100101 and the key is 1001011001.

• Please note that you do not have to actually implement these operations as bit operations. For example, you can store the plaintext as an array of integers.

The Code:

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#define PLAINTEXT 8

#define SIZE 4

#define KEY 10

void oSBox(int[],int[],int,int);

void compf(int val);

void shiftf(int[],int);

void keyGen();

void EncryptDecrypt(int ptext[],int val);

void modSBox(int array[],int p[],int boxNum, int i);

int left[SIZE],right[SIZE],keys[2][PLAINTEXT],cipher[PLAINTEXT];

int main(void) {

int ptext[PLAINTEXT]={0};

int i,comp=1;

int leftS[4],rightS[4],p[4];

printf("\n ENTER PLAINTEXT TEXT: \n");

for(i=0; i<PLAINTEXT ; i++)

scanf("%d",&ptext[i]);

keyGen();

EncryptDecrypt(ptext,0);

printf("\n\n CIPHER TEXT: ");

for(i=0; i<PLAINTEXT; i++)

printf("%d",cipher[i]);

EncryptDecrypt(cipher,1);

printf("\n\n DECIPHER TEXT: ");

for(i=0; i<PLAINTEXT; i++)

printf("%d",cipher[i]);

printf("\n\n ~End of Program~");

printf("\n\n ~Start from Beginning~\n");

return 0;

}

void oSBox(int array[],int p[],int bn, int i)

{

int b[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};

int x;

int o;

int op;

x = array[3]+array[0]*2;

o = array[2]+array[1]*2;

op = b[bn][x][o];

for(;op!=0 ; op /= 2)

p[i--] = op%2;

}

void compf(int n)

{

int SP[]={4,1,2,3,2,3,4,1};

int p4[]={2,4,3,1};

int i;

int r[PLAINTEXT];

int leftS[SIZE],rightS[SIZE];

int p[SIZE]={0};

int x[SIZE];

int comp =0;

printf("\n\n OPTION (0 = modified SBox, \n\t 1 = Original SBox): ");

scanf("%d",&comp);

if(comp == 0) {

for(i=0; i<PLAINTEXT; i++)

r[i]= right[SP[i]-1];

for(i=0; i<PLAINTEXT; i++)

if(i < SIZE) {

leftS[i] = r[i] ^ keys[n][i];

}

else {

rightS[i-4] = r[i] ^ keys[n][i];

}

modSBox(leftS,p,0,1);

modSBox(rightS,p,1,3);

for(i=0; i<SIZE ;i++)

x[i]= p[p4[i]-1];

for(i=0; i<SIZE; i++)

left[i] = left[i]^x[i];

}

else

{

for(i=0; i<PLAINTEXT; i++)

r[i]= right[SP[i]-1];

for(i=0; i<PLAINTEXT; i++)

if(i < SIZE)

{

leftS[i] = r[i] ^ keys[n][i];

}

else

{

rightS[i-4] = r[i] ^ keys[n][i];

}

oSBox(leftS,p,0,1);

oSBox(rightS,p,1,3);

for(i=0; i<SIZE ;i++)

x[i]= p[p4[i]-1];

for(i=0; i<SIZE; i++)

left[i] = left[i]^x[i];

}

}

void shiftf(int IPkey[],int n)

{ int ps1,ps2,i;

while( n > 0 )

{ ps1 = IPkey[0];

ps2 = IPkey[5];

for(i=0; i < 9; i++)

if(i < SIZE)

IPkey[i] = IPkey[i+1];

else if( i > SIZE)

IPkey[i] = IPkey[i+1];

IPkey[4] = ps1;

IPkey[9] = ps2;

n--;

}

}

void keyGen()

{

int compare =1;

int key[KEY];

int i,ikey[KEY];

int p10[]={3,5,2,7,4,10,1,9,8,6};

int p8[] ={6,3,7,4,8,5,10,9};

printf("\n ENTER THE KEY: \n");

for(i=0; i<KEY; i++)

scanf("%d", &key[i]);

for(i=0; i<KEY; i++)

ikey[i] = key[p10[i]-1];

shiftf(ikey,1);

printf("\n\n KEY GENRATED-1: ");

for(i=0; i<PLAINTEXT; i++)

{

keys[0][i]=ikey[p8[i]-1];

printf("%d",keys[0][i]);

}

shiftf(ikey,2);

printf("\n\n KEY GENRATED-2: ");

for(i=0;i<8;i++)

{

keys[1][i] = ikey[p8[i]-1];

printf("%d",keys[1][i]);

}

}

void modSBox(int array[],int p[],int bxn,int i)

{

int bx[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,1,0,3,3,0,1,0,2,0,1,3};

int x;

int ob;

int op;

x = array[3]+array[0]*2;

ob = array[2]+array[1]*2;

op = bx[bxn][x][ob];

for(;op!=0 ; op /= 2)

p[i--] = op%2;

}

void EncryptDecrypt(int ptext[],int val)

{

int AP[] = {2,6,3,1,4,8,5,7}, IP1[] = {4,1,3,5,7,2,8,6};

int rs[PLAINTEXT];

int i;

for(i=0; i<PLAINTEXT; i++)

if( i<SIZE )

{

left[i] = ptext[AP[i]-1];

}

else

{

right[i-SIZE] = ptext[AP[i]-1];

}

compf(val);

for(i=0;i<SIZE;i++)

right[i] = left[i] + right[i],

left[i] = right[i] - left[i],

right[i] = right[i] - left[i];

printf("\n\n RESULT AFTER SWAPPING: ");

for(i=0;i<SIZE; ++i)

{

printf("%d",left[i]);

}

for(i=0;i<SIZE; ++i)

{

printf("%d",right[i]);

}

compf(!val);

for(i=0; i<PLAINTEXT ;i++)

if( i < SIZE)

rs[i]= left[i];

else

rs[i]= right[i-SIZE];

for(i=0; i<PLAINTEXT ;i++)

cipher[i] = rs[IP1[i]-1];

}