Ash's Blog

CN progs –>> DES Encryption.

Posted on: March 19, 2011

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,s[5],f[8],l[8],a,b,r[8],xor[8],xor1[4],s3[2],s4[2],swp[8],si[8];

int ip[]={0,2,6,3,1,4,8,5,7},ip1[]={0,4,1,3,5,7,2,8,6},ep[]={0,4,1,2,3,2,3,4,1},p4[]={0,2,4,3,1};

int key1[]={0,1,0,1,0,0,1,0,0},key2[]={0,0,1,0,0,0,0,1,1};

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

int pt[8],temp[8],temp1[8];

clrscr();

printf(“enter the plain text”);

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

scanf(“%d”,&pt[i]);

//*initial permutation*//

printf(“the initial permuted value is:”);

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

{

temp[i]=pt[ip[i]];

printf(“%d”,temp[i]);

}

for(i=1;i<=4;i++)

l[i]=temp[i];

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

r[i-4]=temp[i];

//*expanded permutation*//

printf(“\n\n\nthe expanded permutation is:”);

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

{

temp1[i]=r[ep[i]];

printf(“%d”,temp1[i]);

}

//*exclusive or*//

printf(“\n\n\nafter ex-or operation:”);

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

{

xor[i]=temp1[i]^key1[i];

printf(“%d”,xor[i]);

}

//*calculating s0,s1*//

s3[0]=xor[4]+xor[1]*2;

s3[1]=xor[3]+xor[2]*2;

s4[0]=xor[8]+xor[5]*2;

s4[1]=xor[7]+xor[6]*2;

a=s0[s3[0]][s3[1]];

b=s1[s4[0]][s4[1]];

for(i=1;i>=0;i–)

{

s3[i]=a%2;

a=a/2;

}

for(i=1;i>=0;i–)

{

s4[i]=b%2;

b=b/2;

}
s[1]=s3[0];

s[2]=s3[1];

s[3]=s4[0];

s[4]=s4[1];

//*permutation4 & ex-or operation*//

for(i=1;i<=4;i++)

s[i]=s[p4[i]];

for(i=1;i<=4;i++)

xor1[i]=s[i]^l[i];

printf(“\n\n\nafter first iteration:”);

for(i=1;i<=4;i++)

printf(“%d”,xor1[i]);

for(i=1;i<=4;i++)

printf(“%d”,r[i]);

for(i=1;i<=4;i++)

swp[i]=r[i];

i=5;

j=1;

while(i<=8)

{

swp[i]=xor1[j];

i++;

j++;

}

printf(“\n\n\nafter swapping”);

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

printf(“%d”,swp[i]);

//*second iteration*//

printf(“\n\n\n\n\n\t\t\t\tSecond iteration!!!!”);

for(i=1;i<=4;i++)

l[i]=swp[i];

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

r[i-4]=swp[i];

//*expanded permutation*//

printf(“\n\n\nthe expanded permutation is:”);

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

{

temp1[i]=r[ep[i]];

printf(“%d”,temp1[i]);

}

//*exclusive or*//

printf(“\n\n\nafter ex-or operation:”);

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

{

xor[i]=temp1[i]^key2[i];

printf(“%d”,xor[i]);

}

//*calculating s0,s1*//

s3[0]=xor[4]+xor[1]*2;

s3[1]=xor[3]+xor[2]*2;

s4[0]=xor[8]+xor[5]*2;

s4[1]=xor[7]+xor[6]*2;

a=s0[s3[0]][s3[1]];b=s1[s4[0]][s4[1]];

for(i=1;i>=0;i–)

{

s3[i]=a%2;

a=a/2;

}

for(i=1;i>=0;i–)

{

s4[i]=b%2;b=b/2;

}

s[1]=s3[0];

s[2]=s3[1];

s[3]=s4[0];

s[4]=s4[1];

//*permutation4 & ex-or operation*//

for(i=1;i<=4;i++)

s[i]=s[p4[i]];

for(i=1;i<=4;i++)

xor1[i]=s[i]^l[i];

printf(“\n\n\nafter second iteration:\n\n”);

for(i=1;i<=4;i++)

si[i]=xor1[i];

i=5;j=1;

while(i<=8)

{

si[i]=r[j];

i++;

j++;

}

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

printf(“%d”,si[i]);

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

f[i]=si[ip1[i]];

printf(“\n\n\nthe CIPHER TEXT is:”);

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

printf(“%d”,f[i]);

getch();

}

Advertisements

1 Response to "CN progs –>> DES Encryption."

[…] CN progs – >> DES Encryption. (ashrules24.wordpress.com) […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow up !

Enter your email address to follow this blog and receive notifications of new posts by email.

iTweet :

Error: Please make sure the Twitter account is public.

Where are you?

%d bloggers like this: