追风者

DES算法的介绍和实现(下)

0
阅读(1596)

INT32 handle_data(ULONG32 *left , ULONG8 choice)
{
INT32 number = 0 ,j = 0;
ULONG32 *right = &left[1] ;
ULONG32 tmp = 0;
ULONG32 tmpbuf[2] = { 0 };

/*第一次调整wz_pc1[64]*/
for ( j = 0 ; j < 64 ; j++)
{
if (j < 32 )
{
if ( wz_pc1[j] > 32)/*属于right*/
{
if ( *right&wz_pc2[wz_pc1[j]-1] )
{
tmpbuf[0] |= wz_pc2[j] ;
}
}
else
{
if ( *left&wz_pc2[wz_pc1[j]-1] )
{
tmpbuf[0] |= wz_pc2[j] ;
}
}
}
else
{
if ( wz_pc1[j] > 32)/*属于right*/
{
if ( *right&wz_pc2[wz_pc1[j]-1] )
{
tmpbuf[1] |= wz_pc2[j] ;
}
}
else
{
if ( *left&wz_pc2[wz_pc1[j]-1] )
{
tmpbuf[1] |= wz_pc2[j] ;
}
}
}
}
*left = tmpbuf[0] ;
*right = tmpbuf[1];
tmpbuf[0] = 0 ;
tmpbuf[1] = 0 ;
switch( choice )
{
case DESENCRY:
for ( number = 0 ; number < 16 ; number++)
{
makedata( left , right , (ULONG32)number) ;
}
break;
case DESDECRY:
for ( number = 15 ; number >= 0 ; number--)
{
makedata( left , right ,(ULONG32)number) ;
}
break;
default:
break;
}

/*最后一轮操作不交换左右值*/

tmp = *left ;
*left = *right ;
*right = tmp ;

/*最后一次调整wz_pc4[64]*/

for ( j = 0 ; j < 64 ; j++)
{
if (j < 32 )
{
if ( wz_pc4[j] > 32)/*属于right*/
{
if ( *right&wz_pc2[wz_pc4[j]-1] )
{
tmpbuf[0] |= wz_pc2[j] ;
}
}
else
{
if ( *left&wz_pc2[wz_pc4[j]-1] )
{
tmpbuf[0] |= wz_pc2[j] ;
}
}
}
else
{
if ( wz_pc4[j] > 32)/*属于right*/
{
if ( *right&wz_pc2[wz_pc4[j]-1] )
{
tmpbuf[1] |= wz_pc2[j] ;
}
}
else
{
if ( *left&wz_pc2[wz_pc4[j]-1] )
{
tmpbuf[1] |= wz_pc2[j] ;
}
}
}
}

*left = tmpbuf[0] ;
*right = tmpbuf[1];

return SUCCESS;
}


INT32 makedata(ULONG32 *left ,ULONG32 *right ,ULONG32 number)
{
INT32 j;
ULONG32 oldright = *right;
ULONG8 rexpbuf[8] = { 0} ;
ULONG32 datatmp = 0;
ULONG32 exp[2] = { 0} ;
//由32扩充至48位

for ( j = 0 ; j < 48 ; j++)
{
/*两个32位,每个存放24位*/
if ( j < 24 )
{
if ( *right&wz_pc2[exptab3[j]-1] )
{
exp[0] |= wz_pc2[j] ;
}
}
else
{
if ( *right&wz_pc2[exptab3[j]-1] )
{
exp[1] |= wz_pc2[j-24] ;
}
}
}

for ( j = 0 ; j < 2 ; j++)
{
exp[j] ^= g_outkey[number][j] ;
}

/*由48->32*/

exp[1] >>= 8 ;
rexpbuf[7] = (ULONG8) (exp[1]&0x0000003fL) ;
exp[1] >>= 6 ;
rexpbuf[6] = (ULONG8) (exp[1]&0x0000003fL) ;
exp[1] >>= 6 ;
rexpbuf[5] = (ULONG8) (exp[1]&0x0000003fL) ;
exp[1] >>= 6 ;
rexpbuf[4] = (ULONG8) (exp[1]&0x0000003fL) ;
exp[0] >>= 8 ;
rexpbuf[3] = (ULONG8) (exp[0]&0x0000003fL) ;
exp[0] >>= 6 ;
rexpbuf[2] = (ULONG8) (exp[0]&0x0000003fL) ;
exp[0] >>= 6 ;
rexpbuf[1] = (ULONG8) (exp[0]&0x0000003fL) ;
exp[0] >>= 6 ;
rexpbuf[0] = (ULONG8) (exp[0]&0x0000003fL) ;
exp[0] = 0 ;
exp[1] = 0 ;

/*由48-> 32*/

*right = 0 ;
for ( j = 0 ; j < 7 ; j++)
{
*right |= SP[j][rexpbuf[j]] ;
*right <<= 4 ;
}
*right |= SP[j][rexpbuf[j]] ;

/*又要换位了*/

datatmp = 0;
for ( j = 0 ; j < 32 ; j++)
{
if ( *right&wz_pc2[wz_pc3[j]-1] )
{
datatmp |= wz_pc2[j] ;
}
}
*right = datatmp ;

/*一轮结束收尾操作*/

*right ^= *left;
*left = oldright;

return SUCCESS;
}

INT32 makefirstkey( ULONG32 *keyP )
{
ULONG32 key[2] = {0};
ULONG32 *Pkey ;
ULONG32 *Pbufkey ;
INT32 j;
Pbufkey = (ULONG32*)g_bufkey ;
Pkey = (ULONG32*)key;

memset((ULONG8*)g_bufkey,0,sizeof(g_bufkey));
memcpy((ULONG8*)&key,(ULONG8*)keyP ,8) ;
memset((ULONG8*)g_outkey,0,sizeof(g_outkey));
for( j = 0 ; j < 28 ; j++)
{
if ( wz_keyleft[j] > 32 )
{
if ( Pkey[1]&wz_pc2[wz_keyleft][j]-1] )
{
Pbufkey[0] |= wz_pc2[j] ;
}
}
else
{
if ( Pkey[0]&wz_pc2[wz_keyleft][j]-1] )
{
Pbufkey[0] |= wz_pc2[j] ;
}
}

if ( wz_keyright[j] > 32 )
{
if ( Pkey[1]&wz_pc2[wz_keyright][j]-1] )
{
Pbufkey[1] |= wz_pc2[j];
}
}
else
{
if ( Pkey[0]&wz_pc2[wz_keyright][j]-1] )
{
Pbufkey[1] |= wz_pc2[j];
}
}
}
for (j = 0 ; j < 16 ; j++)
{
makekey(&Pbufkey[0],&Pbufkey[1] , j ) ;
}
return SUCCESS;
}


INT32 makekey( ULONG32 *keyleft,ULONG32 *keyright ,ULONG32 number)/*输入密钥的地址,一个32位的*/
{
ULONG32 tmpkey[2] ={0};
ULONG32 *Ptmpkey = (ULONG32*)tmpkey;
ULONG32 *Poutkey = (ULONG32*)&g_outkey[number];
INT32 j;
memset((ULONG8*)tmpkey,0,sizeof(tmpkey));
/*要最高的一位或两位*/
*Ptmpkey = *keyleft&wz_leftandtab[wz_lefttable][number]] ;
Ptmpkey[1] = *keyright&wz_leftandtab[wz_lefttable][number]] ;
if ( wz_lefttable[number] == 1)
{
*Ptmpkey >>= 27;
Ptmpkey[1] >>= 27;
}
else
{
*Ptmpkey >>= 26;
Ptmpkey[1] >>= 26;
}
Ptmpkey[0] &= 0xfffffff0;
Ptmpkey[1] &= 0xfffffff0;
/*得到高位的值*/
*keyleft <<= wz_lefttable[number] ;
*keyright <<= wz_lefttable[number] ;
*keyleft |= Ptmpkey[0] ;
*keyright |= Ptmpkey[1] ;
Ptmpkey[0] = 0;
Ptmpkey[1] = 0;

/*从56位中选出48位,3个16位*/
for ( j = 0 ; j < 48 ; j++)
{
if ( j < 24 )
{

if ( *keyleft&wz_pc2[wz_keychoose][j]-1])
{
Poutkey[0] |= wz_pc2[j] ;
}
}

else /*j>=24*/
{
if ( *keyright&wz_pc2[(wz_keychoose][j]-28)])
{
Poutkey[1] | = wz_pc2[j-24] ;
}
}
}
return SUCCESS;
}

Baidu
map