dcsimg
Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Code for DES :

  1. #11
    Senior Member
    Join Date
    Oct 2001
    Posts
    786
    XOR basically returns 0 if the two bits it is comparing are the same, and a 1 if they are different. It's quite useful in encryption because if you XOR two bits and get a result, you can XOR one of the original bits with a bit of the result to get the other original bit. So, if you XOR some data with a key to get some XOR encrypted data, you can send the encrypted data and as long as the person has the key you XOR'ed it with, they can XOR those two together to get the unencrypted data...

    -----------------------------------


    Back to the code...I don't know DES, but I think the goto statements can be replaced with some while looping code that ends appropriately. I can't really follow how the tables are split and reorganized and then left to take up space unused, but one thing that can be done to help keep down on the code is to define & initilize the array at the same time, or really close to each other.


    You might be able to do something like this (I don't feel like breaking out a c-compiler to check the code):
    Code:
    int s1[3][15] =
    {
      {14, 4, 13, 1, 2, ...etc}, {0, 15, 7, 4, ...etc}, {4, 1, 14, ...etc}, {15, 12, 8, ...etc}
    };

    Good luck. The book seems...dated...

  2. #12
    Junior Member
    Join Date
    Jan 2003
    Posts
    18
    Code:
    /*
                   Hey guys, I actually rewritten the code originally posted by AbhishekDwivedi 
    to make it more readable. Plus, I have also rewritten the array initialization 
    for arrays s1, s2, etc... for, well, readability purposes. 
    
                   I have also removed the function declaration for sbox, sl, rh, row, bindec and 
    p1 inside the main() block (otherwise, it will generate compile-time error). 
    instead i put it outside, which is how we would normally declare our function prototypes.
    
                   NOTE: I have found out also that in the original code especially in the 
    array initialization for s2,  the author has somehow committed an error. 
    Look at the code snippet
    
    s2[0][0]=5;
    s2[0][1]=1;
    s2[0][2]=8;
    s2[0][3]=14;
    s2[0][4]=6;
    s1[0][5]=11;      /* THIS LINE IS ERRONEOUS!!! IT SHOULD BE s2[0][5]=11; */
    s2[0][6]=3;
    
    I haven't started analyzing the code yet. Maybe we can start from here. 
                   
    */
    
    
    #include<stdio.h>
    #include<conio.h>
    
    int s1[4][16],s2[4][16],s3[4][16],s4[4][16],s5[4][16],s6[4][16],s7[4][16],s8[4][16];
    int k[56],k1[64],k2[56],k3[48];
    int bl[32], br[32];
    int b1[64], b2[64], b3[48];
    int s[48], t[48];
    int br1[32], br2[32];
    int i;
    int count=0;
    int n;
    int r=1;
    
    int sbox(int,int,int,int);
    void sl();
    void rh();
    int row(int,int);
    void bindec(int);
    void p1();
    
    void main()
    {
    	int snum[8],btemp[32], i;
    	int sr1,sr2,sr3,sr4,sr5,sr6,sr7,sr8,sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8;
    	
    	clrscr();
    
    	
    	static int s1[][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
    	};
    	
    	static int s2[][16]={
    		5, 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
    	};
    	
    	
    	static int s3[][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		
    	};
    	
    	
    	static int s4[][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
    				
    	};
    	
    	static int s5[][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
    	};
    	
    	static int s6[][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
    	};
    	
    	static int s7[][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, 12,
    		6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
    	};
    	
    	static int s8[][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
    	};
    	
    
    	printf("Enter 64-bit binary data ");
    	for( i=0;i<64;i++){
    		g:	
    		scanf("%d",&b1[i]);
    		if(b1[i]>1||b1[i]<0){
    			printf("Enter binary data only : ");
    			goto g;
    		}	
    	}
    	
    	printf("Enter the 56-bit key\n[Key length is 64 bits,but 8 are\nreserved for parity checking ");
    	
    	for(i=0;i<56;i++)
    	{
    		g1:	
    		scanf("%d",&k[i]);
    		if(k[i]>1||k[i]<0){
    		printf("Enter binary data only : ");
    		goto g1;
    		}	
    	}
    	
    	for(i=0;i<64;i++)
    		b2[i]=b1[i]; / * Initial permutation of data. */
    
    	b1[0]=b2[57];
    	b1[1]=b2[49];
    	b1[2]=b2[41];
    	b1[3]=b2[33];
    	b1[4]=b2[25];
    	b1[5]=b2[17];
    	b1[6]=b2[9];
    	b1[7]=b2[1];
    	
    	b1[8]=b2[59];
    	b1[9]=b2[51];
    	b1[10]=b2[43];	
    	b1[11]=b2[35];
    	b1[12]=b2[27];
    	b1[13]=b2[19];
    	b1[14]=b2[11];
    	b1[15]=b2[3];
    	
    	b1[16]=b2[61];
    	b1[17]=b2[53];
    	b1[18]=b2[45];
    	b1[19]=b2[37];
    	b1[20]=b2[29];	
    	b1[21]=b2[21];
    	b1[22]=b2[13];
    	b1[23]=b2[5];
    	
    	b1[24]=b2[63];
    	b1[25]=b2[55];
    	b1[26]=b2[47];
    	b1[27]=b2[39];
    	b1[28]=b2[31];
    	b1[29]=b2[23];
    	b1[30]=b2[15];	
    	b1[31]=b2[7];
    	
    	b1[32]=b2[56];
    	b1[33]=b2[48];
    	b1[34]=b2[40];
    	b1[35]=b2[32];
    	b1[36]=b2[24];
    	b1[37]=b2[16];
    	b1[38]=b2[8];
    	b1[39]=b2[0];
    	
    	b1[40]=b2[58];	
    	b1[41]=b2[50];	
    	b1[42]=b2[42];
    	b1[43]=b2[34];
    	b1[44]=b2[26];
    	b1[45]=b2[18];
    	b1[46]=b2[10];
    	b1[47]=b2[2];
    	
    	b1[48]=b2[60];
    	b1[49]=b2[52];	
    	b1[50]=b2[44];	
    	b1[51]=b2[36];
    	b1[52]=b2[28];
    	b1[53]=b2[20];
    	b1[54]=b2[12];
    	b1[55]=b2[4];
    	
    	b1[56]=b2[62];	
    	b1[57]=b2[54];	
    	b1[58]=b2[46];
    	b1[59]=b2[38];
    	b1[60]=b2[30];	
    	b1[61]=b2[22];
    	b1[62]=b2[14];
    	b1[63]=b2[6];
    
    	for(i=0;i<56;i++)
    		k1[i]=k[i]; /* Converting 64-bit key to 56-bit key. */
    		
    	k2[0]=k1[57];
    	k2[1]=k1[49];
    	k2[2]=k1[41];
    	k2[3]=k1[33];
    	k2[4]=k1[25];
    	k2[5]=k1[17];
    	k2[6]=k1[9];
    	k2[7]=k1[1];
    	
    	k2[8]=k1[58];
    	k2[9]=k1[50];
    	k2[10]=k1[42];
    	k2[11]=k1[34];
    	k2[12]=k1[26];
    	k2[13]=k1[18];
    	k2[14]=k1[10];
    	k2[15]=k1[2];
    	
    	k2[16]=k1[59];
    	k2[17]=k1[51];
    	k2[18]=k1[43];
    	k2[19]=k1[35];
    	k2[20]=k1[27];
    	k2[21]=k1[19];
    	k2[22]=k1[11];
    	k2[23]=k1[3];
    	
    	k2[24]=k1[60];
    	k2[25]=k1[52];
    	k2[26]=k1[44];
    	k2[27]=k1[36];
    	k2[28]=k1[63];
    	k2[29]=k1[55];
    	k2[30]=k1[47];
    	k2[31]=k1[39];
    	
    	k2[32]=k1[31];
    	k2[33]=k1[23];
    	k2[34]=k1[15];
    	k2[35]=k1[7];
    	k2[36]=k1[62];
    	k2[37]=k1[54];
    	k2[38]=k1[46];
    	k2[39]=k1[38];
    	
    	k2[40]=k1[30];
    	k2[41]=k1[22];
    	k2[42]=k1[14];
    	k2[43]=k1[6];
    	k2[44]=k1[61];
    	k2[45]=k1[53];
    	k2[46]=k1[45];
    	k2[47]=k1[37];
    	
    	k2[48]=k1[29];
    	k2[49]=k1[21];
    	k2[50]=k1[13];
    	k2[51]=k1[5];
    	k2[52]=k1[28];
    	k2[53]=k1[20];
    	k2[54]=k1[12];
    	k2[55]=k1[4];
    
    	for(i=0;i<32;i++) 	/* Breaking the initially permuted 	*/
    		bl[i]=b1[i]; 	/* data bits in right-half			*/
    		
    	for(i=32;i<64;i++) 	/* and left-half respectively		*/
    		br[i-32]=b1[i];
    
    	for(r=1;r<=16;r++)
    	{
    		rh();
    		sl();
    		printf("Round %d :\n",r);
    		
    		for(i=0;i<56;i++)
    			printf("%d ",k3[i]);
    			
    		for(i=0;i<48;i++){
    			s[i]=b3[i]+k3[i]; 	/* X-OR of Expanded Right-side of data and compressed */
    			if(s[i]==2) 		/* key. */
    				s[i]=0;
    		}
    		
    		for(i=0;i<48;i++)
    			t[i]=s[i];
    			
    		printf("\n\n");
    		getch();
    		
    		sr1=row(t[0],t[5]);
    		sr2=row(t[6],t[11]);
    		sr3=row(t[12],t[17]);
    		sr4=row(t[18],t[23]);
    		sr5=row(t[24],t[29]);
    		sr6=row(t[30],t[35]);
    		sr7=row(t[36],t[41]);
    		sr8=row(t[42],t[47]);
    		sc1=sbox(1,2,3,4);
    		sc2=sbox(7,8,9,10);
    		sc3=sbox(13,14,15,16);
    		sc4=sbox(19,20,21,22);
    		sc5=sbox(25,26,27,28);
    		sc6=sbox(31,32,33,34);
    		sc7=sbox(37,38,39,40);
    		sc8=sbox(43,44,45,46);
    		
    		snum[0]=s1[sr1][sc1];
    		snum[1]=s2[sr2][sc2];
    		snum[2]=s3[sr3][sc3];
    		snum[3]=s4[sr4][sc4];
    		snum[4]=s5[sr5][sc5];
    		snum[5]=s6[sr6][sc6];
    		snum[6]=s7[sr7][sc7];
    		snum[7]=s8[sr8][sc8];
    		
    		bindec(snum[0]);
    		bindec(snum[1]);
    		bindec(snum[2]);
    		bindec(snum[3]);
    		bindec(snum[4]);
    		bindec(snum[5]);
    		bindec(snum[6]);
    		bindec(snum[7]);
    		
    		p1();
    		
    		for(i=0;i<32;i++)
    		{
    		btemp[i]=bl[i]+br1[i];
    		if(btemp[i]==2)
    		btemp[i]=0;
    		}
    		for(i=0;i<32;i++)
    			bl[i]=br[i];
    			
    		for(i=0;i<32;i++) 
    			br[i]=btemp[i];
    			
    		count=0;		
    	}/* end for block */
    
    	for(i=0;i<32;i++)
    		b1[i]=bl[i];
    	for(i=0;i<32;i++)
    		b1[i+32]=br[i];
    	for(i=0;i<64;i++)
    		b2[i]=b1[i];
    		
    		
    	/* Final permutation to obtain ciphertext */
    	b1[0]=b2[40];
    	b1[1]=b2[8];
    	b1[2]=b2[48];
    	b1[3]=b2[16];
    	b1[4]=b2[56];
    	b1[5]=b2[24];
    	b1[6]=b2[64];
    	b1[7]=b2[32];
    	
    	b1[8]=b2[39];
    	b1[9]=b2[7];
    	b1[10]=b2[47];
    	b1[11]=b2[15];
    	b1[12]=b2[55];
    	b1[13]=b2[23];
    	b1[14]=b2[63];
    	b1[15]=b2[31];
    	
    	b1[16]=b2[38];
    	b1[17]=b2[6];
    	b1[18]=b2[46];
    	b1[19]=b2[14];
    	b1[20]=b2[54];
    	b1[21]=b2[22];
    	b1[22]=b2[62];
    	b1[23]=b2[30];
    	
    	b1[24]=b2[37];
    	b1[25]=b2[5];
    	b1[26]=b2[45];
    	b1[27]=b2[13];
    	b1[28]=b2[53];
    	b1[29]=b2[21];
    	b1[30]=b2[61];
    	b1[31]=b2[29];
    	
    	b1[32]=b2[36];
    	b1[33]=b2[4];
    	b1[34]=b2[44];
    	b1[35]=b2[12];
    	b1[36]=b2[52];
    	b1[37]=b2[20];
    	b1[38]=b2[60];
    	b1[39]=b2[28];
    	
    	b1[40]=b2[35];
    	b1[41]=b2[3];
    	b1[42]=b2[43];
    	b1[43]=b2[11];
    	b1[44]=b2[51];
    	b1[45]=b2[19];
    	b1[46]=b2[59];
    	b1[47]=b2[27];
    	
    	b1[48]=b2[34];
    	b1[49]=b2[2];
    	b1[50]=b2[42];
    	b1[51]=b2[10];
    	b1[52]=b2[50];
    	b1[53]=b2[18];
    	b1[54]=b2[58];
    	b1[55]=b2[26];
    	
    	b1[56]=b2[33];
    	b1[57]=b2[1];
    	b1[58]=b2[41];
    	b1[59]=b2[9];
    	b1[60]=b2[49];
    	b1[61]=b2[17];
    	b1[62]=b2[57];
    	b1[63]=b2[25];
    
    	printf("\n\n");
    	for(i=0;i<32;i++)
    		printf("%d ",bl[i]);
    		
    	printf("\n");
    	
    	for(i=0;i<32;i++)
    		printf("%d ",br[i]);
    		
    	printf("\n\n***********\n");
    	printf("The encrypted 64-bit data is as follows :\n");
    	
    	for(i=0;i<64;i++)
    		printf("%d ",b1[i]);
    		
    	getch();
    
    }	/* end of main block */
    
    
    void rh()
    { 	
    	/* Expansion permutation of Right-half of data. */
    	
    	b3[0]=br[31];
    	b3[1]=br[0];
    	b3[2]=br[1];
    	b3[3]=br[2];
    	b3[4]=br[3];
    	b3[5]=br[4];
    	b3[6]=br[3];
    	b3[7]=br[4];
    	
    	b3[8]=br[5];
    	b3[9]=br[6];
    	b3[10]=br[7];
    	b3[11]=br[8];
    	b3[12]=br[7];
    	b3[13]=br[8];
    	b3[14]=br[9];
    	b3[15]=br[10];
    	
    	b3[16]=br[11];	
    	b3[17]=br[12];
    	b3[18]=br[11];
    	b3[19]=br[12];
    	b3[20]=br[13];
    	b3[21]=br[14];
    	b3[22]=br[15];
    	b3[23]=br[16];
    	
    	b3[24]=br[15];	
    	b3[25]=br[16];
    	b3[26]=br[17];
    	b3[27]=br[18];
    	b3[28]=br[19];
    	b3[29]=br[20];
    	b3[30]=br[19];
    	b3[31]=br[20];
    	
    	b3[32]=br[21];
    	b3[33]=br[22];
    	b3[34]=br[23];
    	b3[35]=br[24];
    	b3[36]=br[23];
    	b3[37]=br[24];
    	b3[38]=br[25];
    	b3[39]=br[26];
    	
    	b3[40]=br[27];
    	b3[41]=br[28];
    	b3[42]=br[27];
    	b3[43]=br[28];
    	b3[44]=br[29];
    	b3[45]=br[30];
    	b3[46]=br[31];
    	b3[47]=br[0];
    	
    } /* end of rh block */
    
    
    void sl()
    {
    	int temp;
    	temp=k2[0];
    	
    	for(i=0;i<27;i++)
    		k2[i]=k2[i+1];
    		
    	k2[27]=temp;
    	
    	if(r==3||r==4||r==5||r==6||r==7||r==8||r==10||r==11||r==12||r==13||r==14||r==15)
    	{
    		temp=k2[0];
    		
    		for(i=0;i<27;i++)
    			k2[i]=k2[i+1];
    			
    		k2[27]=temp;	
    	}
    	
    	temp=k2[28];
    	
    	for(i=28;i<55;i++)
    		k2[i]=k2[i+1];
    		
    	k2[55]=temp;
    	
    	if(r==3||r==4||r==5||r==6||r==7||r==8||r==10||r==11||r==12||r==13||r==14||r==15)
    	{
    		temp=k2[28];
    				
    		for(i=28;i<55;i++)
    			k2[i]=k2[i+1];
    			
    		k2[55]=temp;	
    	} 
    	
    	/* Compression permutation of the key. */
    	k3[0]=k2[13];
    	k3[1]=k2[16];
    	k3[2]=k2[10];
    	k3[3]=k2[23];
    	k3[4]=k2[0];
    	k3[5]=k2[4];
    	k3[6]=k2[2];
    	k3[7]=k2[27];
    	
    	k3[8]=k2[14];
    	k3[9]=k2[5];
    	k3[10]=k2[20];
    	k3[11]=k2[9];
    	k3[12]=k2[22];
    	k3[13]=k2[18];
    	k3[14]=k2[11];
    	k3[15]=k2[3];
    	
    	k3[16]=k2[25];
    	k3[17]=k2[7];
    	k3[18]=k2[15];
    	k3[19]=k2[6];
    	k3[20]=k2[26];
    	k3[21]=k2[19];
    	k3[22]=k2[12];
    	k3[23]=k2[1];
    	
    	k3[24]=k2[40];
    	k3[25]=k2[51];
    	k3[26]=k2[30];
    	k3[27]=k2[36];
    	k3[28]=k2[46];
    	k3[29]=k2[54];
    	k3[30]=k2[29];
    	k3[31]=k2[39];
    	
    	k3[32]=k2[50];
    	k3[33]=k2[44];
    	k3[34]=k2[32];
    	k3[35]=k2[47];
    	k3[36]=k2[43];
    	k3[37]=k2[48];
    	k3[38]=k2[38];
    	k3[39]=k2[55];
    	
    	k3[40]=k2[33];
    	k3[41]=k2[52];
    	k3[42]=k2[45];
    	k3[43]=k2[41];
    	k3[44]=k2[49];
    	k3[45]=k2[35];
    	k3[46]=k2[28];
    	k3[47]=k2[31];
    
    } /* end of s1 block */
    
    
    
    
    int row(int bitl,int bitr)
    {
    	int rown;
    	/**/
    	t[bitr]=t[bitr]*1;
    	t[bitl]=t[bitr]*2;
    	rown=t[bitr]+t[bitl];
    	return(rown);
    }/* end of row block */
    
    int sbox(int p,int q,int r,int s)
    {
    	int col;
    	t[s]=t[s]*1;
    	t[r]=t[r]*2;
    	t[q]=t[q]*4;
    	t[p]=t[p]*8;
    	col=t[s]+t[r]+t[q]+t[p];
    	/**/
    	return(col);
    }/* end of sbox block */
    
    void bindec(int dec)
    {
    	int vrble=3,dec1;
    	while(vrble!=0)
    	{
    		dec1=dec%2;
    		if(dec1==0) {
    			br1[count+vrble]=0;
    		}
    		else {
    			br1[count+vrble]=1;
    		}
    			
    		vrble=vrble-1;
    		dec=dec/2;
    	}	
    	count=count+4;
    } /* end of bindec block */
    
    void p1()
    {
    	for(i=0;i<32;i++) 		/* Permutation of 32-bit output of s-boxes */
    		br2[i]=br1[i];
    		
    	br1[0]=br2[15];
    	br1[1]=br2[6];
    	br1[2]=br2[19];
    	br1[3]=br2[20];
    	br1[4]=br2[28];
    	br1[5]=br2[11];
    	br1[6]=br2[27];
    	br1[7]=br2[16];
    	
    	br1[8]=br2[0];
    	br1[9]=br2[14];
    	br1[10]=br2[22];
    	br1[11]=br2[25];
    	br1[12]=br2[4];
    	br1[13]=br2[17];
    	br1[14]=br2[30];
    	br1[15]=br2[9];
    	
    	br1[16]=br2[1];
    	br1[17]=br2[7];
    	br1[18]=br2[23];
    	br1[19]=br2[13];
    	br1[20]=br2[31];
    	br1[21]=br2[26];
    	br1[22]=br2[2];
    	br1[23]=br2[8];
    	
    	br1[24]=br2[18];
    	br1[25]=br2[12];
    	br1[26]=br2[29];
    	br1[27]=br2[5];
    	br1[28]=br2[21];
    	br1[29]=br2[10];
    	br1[30]=br2[3];
    	br1[31]=br2[24];
    } /* end of p1 block */
    Turn on. Tune in. Drop out.

  3. #13
    Junior Member
    Join Date
    Jul 2003
    Posts
    9
    Hello all,
    I thank all the members who have shown interest in my code.The queries of AxessTerminated were aptly answered by SirDice.
    I thank frz a lot for pointing out the error in my code [yes he is right it should be s2[0][5]=11; ] , and putting it in a better form . I would hence advise wyred to consider putting the updated code into his attachment file[which has been downloaded by 10 people] . But I would like to tell TimAxe that I did not use goto statements in my code. I think that he is reffering to the modified codes of wyred or frz .
    I wrote this code about 1 and a half years ago,as part of a mini-project in our college.
    I myself have forgotten what many of the functions do.I will soon put a posting giving a brief description of the functions which are easy, provided that DES is understood by the reader.
    I know that this is in anti-thesis to one of my earlier postings,but seeing the response to it,I will put the brief explanations to the functions.
    Thank you.
    From Abhishek Dwivedi.

  4. #14
    Junior Member
    Join Date
    Jan 2003
    Posts
    18
    i really find the code block with goto statements very "awkward". this is the code snippet i have seen which was originally posted anyway:

    Code:
    printf("Enter 64-bit binary data ");
    	for( i=0;i&lt;64;i++){
    		g:	
    		scanf("%d",&b1[i]);
    		if(b1[i]&gt;1||b1[i]&lt;0){
    			printf("Enter binary data only : ");
    			goto g;
    		}	
    	}
    	
    	printf("Enter the 56-bit key\n[Key length is 64 bits,but 8 are\nreserved for parity checking ");
    	
    	for(i=0;i&lt;56;i++)
    	{
    		g1:	
    		scanf("%d",&k[i]);
    		if(k[i]&gt;1||k[i]&lt;0){
    		printf("Enter binary data only : ");
    		goto g1;
    		}	
    	}
    Turn on. Tune in. Drop out.

  5. #15
    Junior Member
    Join Date
    Jul 2003
    Posts
    9
    Hello again,
    I must confess, I discovered a very serious flaw in my code yesterday. Please do make the following changes : Declare k[56] as k[64], and instead of accepting a 56-bit key, run the for loop to accept a 64-bit key. Refer to the code pasted by frz and go to the line where I have commented "Converting the 64-bit key to 56-bit key".The for loop should be :
    for(i=0;i&lt;64;i++)
    k1[i]=k[i];
    now that k[] and k1[] are both 64-bit arrays.
    It was like this originally,but I made this change some months after the main program,thinking that Iwould have to enter only 56 bits instead of the 64 bits earlier,and conveniently forgetting about the compression permutation that converts the 64-bit key to the 56-bit key !!! But even after this flaw having gone unnoticed, I never got any garbage values. Hence it was undetected until yesterday when I sat down to understand my own code to post the working of the functions .
    I am profusely sorry to those who have downloaded this code,and those who have used it. I allow myself a famous frustration vent "DOH!!" (courtesy--Homer Simpson) .
    I am again sorry,considering the response it has got,and especially to those who ran it.
    Thank you.
    P.S. I "DOH!!" once again....
    From Abhishek Dwivedi.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •