Friday, 24 August 2018

Implement expansion permutation of data encryption standards (DES).


Introduction:
The E-box expansion permutation - here the 32-bit input data from Ri−1 is expanded and permuted to give the 48 bits necessary for combination with the 48 bit key. The E-box expansion permutation delivers a larger output by splitting its input into 8, 4-bit blocks and copying every first and fourth bit in each block into the output in a defined manner. The security offered by this operation comes from one bit affecting two substitutions in the S-boxes.


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
 










                  Expansion Permutation (E)

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
                          Permutation function(P)  




C code:
//EXPANSION AND PERMUTATION
#include<stdio.h>
#include<conio.h>
void main()
{
            int i,j,k,n,x,y,t,temp[32],temp2[4][8];

    char b[32],in[8][4],exp[8][6];
            clrscr();
            for(i=0;i<32;i++)
                        b[i]=NULL;
            printf("Enter value : ");
            scanf("%d",&n);
                        i=31;
            while(i>=0)
            {   if(n==0)
                        { b[i--]=0; }
                        while(n!=0)
                        {          b[i--]=n%2;
                                    n=n/2; }
            }
            printf("\n VALUE IN BINARY.....\n");
            for(i=0;i<32;i++)
            {  printf("%d ",b[i]); }


            printf("\n");
            t=0;
            for(x=0;x<8;x++)
            {
                        for(y=0;y<4;y++)
                        {
                                    in[x][y]=b[t++];
                        }
            }
          
            for(i=1;i<8;i++)
{
exp[i][0]=in[i-1][3];
exp[i-1][5]=in[i][0];
}
exp[7][5]=in[0][0];
exp[0][0]=in[7][3];
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
exp[i][j+1]=in[i][j];
}
}
printf("\n Expansion:\n");
for(x=0;x<8;x++)
{
for(y=0;y<6;y++)
{
printf("  %d ",exp[x][y]);
}
printf("\n");
}

 //permutation

    temp[0]=b[15];
    temp[1]=b[6];
    temp[2]=b[19];
    temp[3]=b[20];
    temp[4]=b[28];
    temp[5]=b[11];
    temp[6]=b[27];
    temp[7]=b[16];
    temp[8]=b[0];
    temp[9]=b[14];
    temp[10]=b[22];
    temp[11]=b[25];
    temp[12]=b[4];
    temp[13]=b[17];
    temp[14]=b[30];
    temp[15]=b[9];
    temp[16]=b[1];
    temp[17]=b[7];
    temp[18]=b[23];
    temp[19]=b[13];
    temp[20]=b[31];
    temp[21]=b[26];
    temp[22]=b[2];
    temp[23]=b[8];
    temp[24]=b[18];
    temp[25]=b[12];
    temp[26]=b[29];
    temp[27]=b[5];
    temp[28]=b[21];
    temp[29]=b[10];
    temp[30]=b[3];
    temp[31]=b[24];
  printf("\n PERMUTATION IN SINGLE ARRAY\n");
                 for(i=0;i<32;i++)
                 {
                 printf("%d ",temp[i]);
                 }

 printf("\n \n AFTER PERMUTATION....\n");
            t=0;
            for(i=0;i<4;i++)
            {
                        for(j=0;j<8;j++)
                        {
                                    temp2[i][j]=temp[t++];
                        }
            }
             for(i=0;i<4;i++)
                 {
                  for(j=0;j<8;j++)
                  {
                        printf("%d  ",temp2[i][j]);

                        }
                        printf("\n");
                        }

            getch();
}

OUTPUT

 

THANK YOU!!!!


 

No comments:

Post a Comment