Vous êtes sur la page 1sur 14

***** DATA ENCRYPTION STANDARD PROGRAM *****

#include
#include

void keygen();
int len,x,y = 0,l[40],r[40],r1[48],str1[20], r2[48],i3, tmp3 [64],tmp4[64];

int ip [] = {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,5
1,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7};

int e [] = {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 c1[100];
int s[64];
int round=1;

int k [] = {0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1,
1,1,1,1,1,0,0,0,0,0,0, 0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,0,1,1};

int pc1 [] = {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 pc2[]={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 tmp5[8],flag=0,t,t1,i,j=0,c2[100],c[100],d[100],
key[100],k2[20][100],count=1;

int s1[4][16]={{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 s2[4][16]={{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 s3[4][16]={{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 s4[4][16]={{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 s5[4][16]={{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 s6[4][16]={{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 s7[4][16]={{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 s8[4][16]={{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 p[32]={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 g3,w[8],i4,d1=0,g,g1=0,g2,size,ch1[8],a1[4],b1[8][4],
b[8][6],i1,j1,k1=0,tz[4],tmp,tmp1,t11,temp,z=0,m,count1=0,c11[32],res[32];

int iip[]={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};

char ch[20];
int str[64];

void main()
{
clrscr();
printf("enter the string\n");
gets(ch);
size=strlen(ch);

for(g=0;g<SIZE;G++)
{
ch1[g]=ch[g];
}

for(g=0;g<SIZE;G++)
{
g1=0;
while(ch1[g]>0)
{
w[g1++]=ch1[g]%2;
ch1[g]=ch1[g]/2;
}
for(g3=g1;g3<8;g3++)
w[g3]=0;

for(g2=7;g2>=0;g2--)
{
str[d1++]=w[g2];
}
}

for(g3=0;g3<64;g3++)
s[g3]=str[g3];

for(g2=0;g2<64;g2++)
printf("%d",str[g2]);
keygen(); /*16 key generation*/
/*initial permutation*/
for(x=0;x<64;x++)
{
c1[x]=str[ip[x]-1];
}

for(x=0;x<64;x++)
{
printf("%d",c1[x]);
if(((x%7)==0)&&(x!=0))
printf("\n");
}

/*dividing the whole string into l & r*/

for(x=0;x<32;x++)
l[x]=c1[x];
y=0;
for(x=32;x<64;x++)
r[y++]=c1[x];

/*round starts here*/


while(round<=16)
{

/*expansion permutation e table*/


for(x=0;x<48;x++)
{
r1[x]=r[e[x]-1];
}
printf("r1: ");
for(x=0;x<48;x++)
printf("%d",r1[x]);
printf("\n");

/*xor r1 with key*/


for(x=0;x<48;x++)
{
if(r1[x]==k2[round][x])
r2[x]=0;
else
r2[x]=1;
}
printf("\nkey");
for(x=0;x<48;x++)
printf("%d",k2[round][x]);
printf("\n");
for(x=0;x<48;x++)
printf("%d",r2[x]);

/*substitution s-box*/
k1=0;
for(i1=0;i1<8;i1++)
{
for(j1=0;j1<6;j1++)
{
b[i1][j1]=r2[k1++];
}
}
count1=0;
for(i1=0;i1<8;i1++)
{
count1++;
tz[0]=b[i1][0];
tz[1]=b[i1][5];
tmp=(tz[0]*2)+tz[1];
k1=0;
printf("\n");
for(j1=1;j1<5;j1++)
{
tz[k1]=b[i1][j1];
printf("%d",tz[k1]);
k1++;
}
tmp1=(tz[0]*8)+(tz[1]*4)+(tz[2]*2)+tz[3];

switch(count1)
{
case 1 : temp=s1[tmp][tmp1];
break;
case 2 : temp=s2[tmp][tmp1];
break;
case 3 : temp=s3[tmp][tmp1];
break;
case 4 : temp=s4[tmp][tmp1];
break;
case 5 : temp=s5[tmp][tmp1];
break;
case 6 : temp=s6[tmp][tmp1];
break;
case 7 : temp=s7[tmp][tmp1];
break;
case 8 : temp=s8[tmp][tmp1];
break;
}

printf("temp is %d",temp);
t11=0;

if(temp==0)
{
for(m=0;m<4;m++)
b1[i1][m]=0;
}

else
{
while(temp>0)
{
a1[t11]=temp%2;
temp=temp/2;
t11++;
}

for(m=t11;m<4;m++)
a1[m]=0;
z=0;
for(m=3;m>=0;m--)
{
b1[i1][z++]=a1[m];
}
}
}

k1=0;
printf("b1 is\n");
for(i1=0;i1<8;i1++)
{
for(j1=0;j1<4;j1++)
printf("%d",b1[i1][j1]);
printf("\n");
}

for(i1=0;i1<8;i1++)
{
for(j1=0;j1<4;j1++)
c11[k1++]=b1[i1][j1];
}

/*permutation after substitution*/

for(i1=0;i1<32;i1++)
res[i1]=c11[p[i1]-1];
for(i1=0;i1<32;i1++)
printf("%d",c11[i1]);
printf("after substn\n");
for(i1=0;i1<32;i1++)
printf("%d",res[i1]);
printf("xor l[i] with res[i]\n");

for(i1=0;i1<32;i1++)
{
if(l[i1]==res[i1])
res[i1]=0;
else
res[i1]=1;
}
for(i1=0;i1<32;i1++)
l[i1]=r[i1];
for(i1=0;i1<32;i1++)
r[i1]=res[i1];
printf("left and right for next round\n");

for(i1=0;i1<32;i1++)
printf("%d",l[i1]);
printf("\n");
for(i1=0;i1<32;i1++)
printf("%d",r[i1]);
round++;
}

for(i3=0;i3<32;i3++)
{
tmp3[i3]=r[i3];
}
i4=0;
for(i3=32;i3<64;i3++)
{
tmp3[i3]=l[i4++];
}
printf("\nafter swapping\n");

for(i3=0;i3<64;i3++)
printf("%d",tmp3[i3]);

for(i3=0;i3<64;i3++)
{
tmp4[i3]=tmp3[iip[i3]-1];
}
printf("output is\n");

for(i3=0;i3<64;i3++)
printf("%d",tmp4[i3]);
j=0;

for(i=0;i<64;i++)
{
tmp5[j]=(tmp4[i]*128)+(tmp4[i+1]*64)+(tmp4[i+2]*32)+(tmp4[i+3]*16)+
(tmp4[i+4]*8)+(tmp4[i+5]*4)+(tmp4[i+6]*2)+tmp4[i+7];

i=i+7;
j++;
}

printf("\n");
for(i=0;i<8;i++)
printf("%c",tmp5[i]);
getch();
}

void keygen()
{
for(i=0;i<56;i++)
{
key[i]=k[pc1[i]-1];
}
for(i=0;i<28;i++)
{
c[i]=key[i];
}
for(i=28;i<56;i++)
{
d[j++]=key[i];
}

while(count<17)
{
t=c[0];
t1=d[0];
for(i=0;i<27;i++)
{
c[i]=c[i+1];
d[i]=d[i+1];
}

if(i==27)
{
c[i]=t;
d[i]=t1;
}
if((count==1)||(count==2)||(count==9)||(count==16))
count++;
else if(flag==1)
{
count++;
flag=0;
}
else
flag=1;
if(flag==0)
{
for(i=0;i<28;i++)
c2[i]=c[i];
j=0;
for(i=28;i<56;i++)
c2[i]=d[j++];
for(i=0;i<48;i++)
k2[count-1][i]=c2[pc2[i]-1];
}
}
}

Enter the string of 8 chars

SREERAMA

010100110101001001000101010001010101001001000001010011010100000111111111
0001001
1010011
0011101
1010000
0000000
0000001
0000000
0010011
r1: 100000000000000000000000001000000000000010100110

key 010000011101011110001101101111100110001110101010
110000011101011110001101100111100110001100001100
1000temp is 15
1110temp is 11
1111temp is 8
0110temp is 0
0011temp is 7
0011temp is 5
0110temp is 8
0110temp is 11
b1 is
1111
1011
1000
0000
0111
0101
1000
1011
11111011100000000111010110001011after substn
01101000100011101110101110001011xor l[i] with res[i]
left and right for next round
00000000000000000100000000010011
10010111100111011010011101100110r1:
010010101111110011111011110100001110101100001101

key111111100010010011101000000101110110110100110111
101101001101100000010011110001111000011000111010
0110temp is 1
0110temp is 8
0000temp is 13
1001temp is 7
1000temp is 6
1100temp is 1
1100temp is 5
1101temp is 3
b1 is
0001
1000
1101
0111
0110
0001
0101
0011
00011000110101110110000101010011after substn
10000110010111110011100110000010xor l[i] with res[i]
left and right for next round
10010111100111011010011101100110
10000110010111110111100110010001r1:
110000001100001011111110101111110011110010100011

key110110101100111000111000011011110000100111010100
000110100000110011000110110100000011010101110111
0011temp is 1
0000temp is 0
1001temp is 15
0011temp is 3
1010temp is 12
0001temp is 15
1010temp is 5
1011temp is 0
b1 is
0001
0000
1111
0011
1100
1111
0101
0000
00010000111100111100111101010000after substn
10010111011101010010000100001110xor l[i] with res[i]
left and right for next round
10000110010111110111100110010001
00000000111010001000011001101000r1:
000000000001011101010001010000001100001101010000

key110011001011101100011110110000011110000111011111
110011001010110001001111100000010010001010001111
1001temp is 11
0101temp is 11
1000temp is 4
0111temp is 3
0000temp is 4
1001temp is 13
0101temp is 0
0111temp is 4
b1 is
1011
1011
0100
0011
0100
1101
0000
0100
10111011010000110100110100000100after substn
11010000110011010110001000101010xor l[i] with res[i]
left and right for next round
00000000111010001000011001101000
01010110100100100001101110111011r1:
101010101101010010100100000011110111110111110110

key001001101011111001001111011001111001011010001101
100011000110101011101011011010001110101101111011
0001temp is 12
0011temp is 14
0101temp is 9
0101temp is 1
1101temp is 0
0111temp is 8
0110temp is 10
1101temp is 5
b1 is
1100
1110
1001
0001
0000
1000
1010
0101
11001110100100010000100010100101after substn
11010100100010001000110100110001xor l[i] with res[i]
left and right for next round
01010110100100100001101110111011
11010100011000000000101101011001r1:
111010101000001100000000000001010110101011110011

key011010110111110001100010110110100001010111101111
100000011111111101100010110111110111111100011100
0000temp is 4
1111temp is 5
1110temp is 2
0001temp is 6
1011temp is 9
1011temp is 7
1110temp is 9
1110temp is 12
b1 is
0100
0101
0010
0110
1001
0111
1001
1100
01000101001001101001011110011100after substn
00101011011000001111000000111101xor l[i] with res[i]
left and right for next round
11010100011000000000101101011001
01111101111100101110101110000110r1:
001111111011111110100101011101010111110000001100

key111010001110110111111000000011101101101110101101
110101110101001001011101011110111010011110100001
1010temp is 3
1010temp is 7
0100temp is 3
1110temp is 14
1111temp is 9
1101temp is 13
1111temp is 1
0000temp is 2
b1 is
0011
0111
0011
1110
1001
1101
0001
0010
00110111001111101001110100010010after substn
01110111010000100111001001011110xor l[i] with res[i]
left and right for next round
01111101111100101110101110000110
10100011001000100111100100000111r1:
110100000110100100000100001111110010100000001111

key110101001110011100011011010100100111110111110001
000001001000111000011111011011010101010111111110
0000temp is 0
0100temp is 6
1100temp is 5
1111temp is 9
1101temp is 9
1010temp is 13
1011temp is 12
1111temp is 8
b1 is
0000
0110
0101
1001
1001
1101
1100
1000
00000110010110011001110111001000after substn
11111101000100010010000001011001xor l[i] with res[i]
left and right for next round
10100011001000100111100100000111
10000000111000111100101111011111r1:
110000000001011100000111111001010111111011111111

key101111100100101111100110111011001111100001010111
011111100101110011100001000010011000011010101000
1111temp is 8
0010temp is 10
1001temp is 15
0000temp is 3
0001temp is 12
1100temp is 14
1101temp is 10
0100temp is 9
b1 is
1000
1010
1111
0011
1100
1110
1010
1001
10001010111100111100111010101001after substn
11011101111011010000110100001101xor l[i] with res[i]
left and right for next round
10000000111000111100101111011111
01111110110011110111010000001010r1:
001111111101011001011110101110101000000001010100

key101110100111001100101001101001111100011011111010
100001011010010101110111000111010100011010101110
0000temp is 15
1101temp is 0
1010temp is 5
1011temp is 11
0011temp is 12
1010temp is 3
1101temp is 10
0111temp is 2
b1 is
1111
0000
0101
1011
1100
0011
1010
0010
11110000010110111100001110100010after substn
10000101111001111010011001000011xor l[i] with res[i]
left and right for next round
01111110110011110111010000001010
00000101000001000110110110011100r1:
000000001010100000001000001101011011110011111000

key100010010001011101111101100111011001111101000011
100010011011111101110101101010000010001110111011
0001temp is 1
1101temp is 9
1110temp is 2
1010temp is 5
0101temp is 13
0001temp is 1
0111temp is 13
1101temp is 5
b1 is
0001
1001
0010
0101
1101
0001
1101
0101
00011001001001011101000111010101after substn
10100011000111000111100000100111xor l[i] with res[i]
left and right for next round
00000101000001000110110110011100
11011101110100110000110000101101r1:
111011111011111010100110100001011000000101011011

key110001010101101011011101100111101100011001110100
001010101110010001111011000110110100011100101111
0101temp is 15
0111temp is 1
1000temp is 2
1101temp is 7
0011temp is 1
1010temp is 4
1110temp is 6
0111temp is 13
b1 is
1111
0001
0010
0111
0001
0100
0110
1101
11110001001001110001010001101101after substn
10101000110100001101111000101110xor l[i] with res[i]
left and right for next round
11011101110100110000110000101101
10101101110101001011001110110010r1:
010101011011111010101001010110100111110110100101

key000101111111101111100000010110011110111111000100
010000100100010101001001000000111001001001100001
1000temp is 3
0010temp is 7
1010temp is 5
0100temp is 6
0000temp is 2
1100temp is 6
0100temp is 4
0000temp is 2
b1 is
0011
0111
0101
0110
0010
0110
0100
0010
00110111010101100010011001000010after substn
01000100011100110101001010011010xor l[i] with res[i]
left and right for next round
10101101110101001011001110110010
10011001101000000101111010110111r1:
110011110011110100000000001011111101010110101111

key100110100111110111100011101110001110010010011001
010101010100000011100011100101110011000100110110
1010temp is 12
1010temp is 2
0001temp is 7
0001temp is 15
0010temp is 12
1001temp is 14
0010temp is 2
1011temp is 13
b1 is
1100
0010
0111
1111
1100
1110
0010
1101
11000010011111111100111000101101after substn
11011101111001011001110001101100xor l[i] with res[i]
left and right for next round
10011001101000000101111010110111
01110000001100010010111111011110r1:
001110100000000110100010100101011111111011111100

key111110010110011101001101111010110111011000000111
110000110110011011101111011111101000100011111011
1000temp is 15
1011temp is 6
1101temp is 11
0111temp is 8
1111temp is 6
0100temp is 2
0001temp is 11
1101temp is 5
b1 is
1111
0110
1011
1000
0110
0010
1011
0101
11110110101110000110001010110101after substn
01000110101001001000111111110111xor l[i] with res[i]
left and right for next round
01110000001100010010111111011110
11011111000001001101000101000000r1:
011011111110100000001001011010100010101000000001

key001011110011110010111110001010111001101110110011
010000001101010010110111010000011011000110110010
1000temp is 3
0110temp is 8
1001temp is 13
1011temp is 11
1000temp is 8
1101temp is 11
0011temp is 14
1001temp is 6
b1 is
0011
1000
1101
1011
1000
1011
1110
0110
00111000110110111000101111100110after substn
10010101011110110010011101100011xor l[i] with res[i]
left and right for next round
11011111000001001101000101000000
11100101010010100000100010111101
after swapping
1110010101001010000010001011110111011111000001001101000101000000output is
1100100110010000111000011001010110001001010000011101101011001001

Vous aimerez peut-être aussi