To implement S-DES sub key Generation in C. (WITH OUTPUT IMAGE)


#include<stdio.h>
#include<conio.h>

void main()
{
//sub key generation.
char key[10];
int k[10],k10[10],k8[10],p10[10]={3,5,2,7,4,10,1,9,8,6},temp,temp1;
int p8[8]={6,3,7,4,8,5,10,9},i,j,l0[5],r0[5],l1[5],r1[5];
int k1[8],k2[8];

clrscr();
abs:
printf("P10=3,5,2,7,4,10,1,9,8,6");
printf("\nP8=6,3,7,4,8,5,10,9");
printf("\n Enter 10 bit key:: ");
gets(key);
//149 for 1 and 48 for 0.
if(strlen(key)!=10)
goto abs;

for(i=0;i<10;i++)
{if(key[i]=='1')
 k[i]=1;

 else if(key[i]=='0')
 k[i]=0;

 else
 goto abs;

}

for(i=0;i<10;i++)
printf("%d",k[i]);
//p10 operation.

printf("\n Result of p10::");
for(i=0;i<10;i++)
{k10[i]=k[p10[i]-1];
printf("%d",k10[i]);
}
//devide in two parts.
for(i=0;i<5;i++)
{l0[i]=k10[i];
 r0[i]=k10[i+5];
}

printf("\n L-part::");
for(i=0;i<5;i++)
printf("%d",l0[i]);

printf("\n R-part::");
for(i=0;i<5;i++)
printf("%d",r0[i]);
//apply 1 shift on both parts.
printf("\n after 1bit right shift");
for(i=0;i<5;i++)
{
l1[i]=l0[i+1];
r1[i]=r0[i+1];

if(i==4)
{l1[i]=l0[0];
r1[i]=r1[0];
}


}
printf("\n L1::");
for(i=0;i<5;i++)
printf("%d",l1[i]);

printf("\n R1::");
for(i=0;i<5;i++)
printf("%d",r1[i]);

//combine  L1 & R1.

for(i=0;i<5;i++)
{ k8[i]=l1[i];
  k8[i+5]=r1[i];
}

printf("\n combined L1 || R1::");
for(i=0;i<10;i++)
printf("%d",k8[i]);

//do p8 on 8-bit key;
printf("\n K1::");
for(i=0;i<8;i++)
printf("%d",k1[i]=k8[p8[i]-1]);

//K1 calculated.

//for K2 use combined l0 & r0 =k8.
for(i=0;i<5;i++)
{l1[i]=k8[i];
r1[i]=k8[i+5];
}
printf("\n \n");
//now apply p8 on both parts.
printf("\n L1::");
for(i=0;i<5;i++)
printf("%d",l1[i]);

printf("\n R1::");
for(i=0;i<5;i++)
printf("%d",r1[i]);

printf("\n after 2 right shift operation;:");
for(j=0;j<2;j++)
{
 for(i=0;i<4;i++)
 {temp=l1[i];
  l1[i]=l1[i+1];
  l1[i+1]=temp;

  temp1=r1[i];
  r1[i]=r1[i+1];
  r1[i+1]=temp1;
 }
}
printf("\nL2::");
for(i=0;i<5;i++)
printf("%d",l1[i]);

printf("\nR2::");
for(i=0;i<5;i++)
printf("%d",r1[i]);

for(i=0;i<5;i++)
{
k8[i]=l1[i];
k8[i+5]=r1[i];
}

printf("\n L2 || R2::");
for(i=0;i<10;i++)
printf("%d",k8[i]);
//p8 operaion.
printf("\n After p8 operation on combined bits \n K2::");
for(i=0;i<8;i++)
printf("%d",k2[i]=k8[p8[i]-1]);

getch();
}

OUTPUT:


Comments