/* DES加密解密函数库
*
* 本算法根据以下文章内容整理:
* DES算法的介绍和实现(上) http://www.vckbase.com/document/viewdoc/?id=352
* DES算法的介绍和实现(下) http://www.vckbase.com/document/viewdoc/?id=353
*
* Eric Wei, 2007-4-5
*/
#include "des.h"
ULONG32 g_outkey[16][2] = {0}; /*输出的key*/
ULONG32 g_bufkey[2] = {0}; /*形成起始密钥*/
/*实现时多采用查表方式,下面是定义的供查找用的表*/
static BYTE wz_lefttable[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
static ULONG32 wz_leftandtab[3] = {0x0 , 0x80000000 , 0xc0000000 } ;
static BYTE wz_keyleft[28] =
{
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
};
static BYTE wz_keyright[28] = {
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
};
static BYTE wz_keychoose[48] ={
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
};
static BYTE wz_pc4[64] = { /*最后一次调整*/
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
};
static BYTE wz_pc1[64] = { /*第一次转换时用*/
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,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7
} ;
static BYTE wz_pc3[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
} ;
static ULONG32 wz_pc2[64] = {
0x80000000L,0x40000000L,0x20000000L,0x10000000L, 0x8000000L,
0x4000000L, 0x2000000L, 0x1000000L, 0x800000L, 0x400000L,
0x200000L, 0x100000L, 0x80000L, 0x40000L, 0x20000L,0x10000L,
0x8000L, 0x4000L, 0x2000L, 0x1000L, 0x800L, 0x400L, 0x200L,
0x100L, 0x80L,0x40L,0x20L, 0x10L, 0x8L, 0x4L, 0x2L, 0x1L,
0x80000000L,0x40000000L,0x20000000L,0x10000000L, 0x8000000L,
0x4000000L, 0x2000000L, 0x1000000L, 0x800000L, 0x400000L,
0x200000L, 0x100000L, 0x80000L, 0x40000L, 0x20000L, 0x10000L,
0x8000L, 0x4000L, 0x2000L, 0x1000L, 0x800L, 0x400L, 0x200L,
0x100L, 0x80L, 0x40L,0x20L, 0x10L, 0x8L, 0x4L, 0x2L, 0x1L,
};
static BYTE exptab3[48] = {
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
};
static BYTE SP[8][64] =
{
{
0xe,0x0,0x4,0xf,0xd,0x7,0x1,0x4,0x2,0xe,0xf,0x2,0xb,
0xd,0x8,0x1,0x3,0xa,0xa,0x6,0x6,0xc,0xc,0xb,0x5,0x9,
0x9,0x5,0x0,0x3,0x7,0x8,0x4,0xf,0x1,0xc,0xe,0x8,0x8,
0x2,0xd,0x4,0x6,0x9,0x2,0x1,0xb,0x7,0xf,0x5,0xc,0xb,
0x9,0x3,0x7,0xe,0x3,0xa,0xa,0x0,0x5,0x6,0x0,0xd
},
{
0xf,0x3,0x1,0xd,0x8,0x4,0xe,0x7,0x6,0xf,0xb,0x2,0x3,
0x8,0x4,0xf,0x9,0xc,0x7,0x0,0x2,0x1,0xd,0xa,0xc,0x6,
0x0,0x9,0x5,0xb,0xa,0x5,0x0,0xd,0xe,0x8,0x7,0xa,0xb,
0x1,0xa,0x3,0x4,0xf,0xd,0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2,0x5,
0xc,0x2,0x9,0x8,0x5,0xc,0xf,0x3,0xa,0x7,0xb,0x0,0xe,
0x4,0x1,0xa,0x7,0x1,0x6,0xd,0x0,0xb,0x8,0x6,0xd
},
{
0x4,0xd,0xb,0x0,0x2,0xb,0xe,0x7,0xf,0x4,0x0,0x9,0x8,
0x1,0xd,0xa,0x3,0xe,0xc,0x3,0x9,0x5,0x7,0xc,0x5,0x2,
0xa,0xf,0x6,0x8,0x1,0x6,0x1,0x6,0x4,0xb,0xb,0xd,0xd,
0x8,0xc,0x1,0x3,0x4,0x7,0xa,0xe,0x7,0xa,0x9,0xf,0x5,
0x6,0x0,0x8,0xf,0x0,0xe,0x5,0x2,0x9,0x3,0x2,0xc
},
{
0xd,0x1,0x2,0xf,0x8,0xd,0x4,0x8,0x6,0xa,0xf,0x3,0xb,
0x7,0x1,0x4,0xa,0xc,0x9,0x5,0x3,0x6,0xe,0xb,0x5,0x0,
0x0,0xe,0xc,0x9,0x7,0x2,0x7,0x2,0xb,0x1,0x4,0xe,0x1,
0x7,0x9,0x4,0xc,0xa,0xe,0x8,0x2,0xd,0x0,0xf,0x6,0xc,
0xa,0x9,0xd,0x0,0xf,0x3,0x3,0x5,0x5,0x6,0x8,0xb
}
};
/*函数实现*/
INT32 DdesN(BYTE *data, BYTE **key, ULONG32 n_key,ULONG32 readlen)
{
INT32 i ;
for ( i = n_key ; i > 0 ; i--)
{
Ddes(data,key[i-1],readlen);
}
return SUCCESS;
}
INT32 desN(BYTE *data, BYTE **key, ULONG32 n_key,ULONG32 readlen)
{
ULONG32 i = 0;
for ( i = 0 ; i < n_key ; i++)
{
des(data,key[i],readlen);
}
return SUCCESS;
}
INT32 des3(BYTE *data, BYTE *key,ULONG32 n ,ULONG32 readlen)
{
ULONG32 i = 0 , j = 0 ;
makefirstkey((ULONG32*)key) ; /*产生密钥*/
for ( i = 0 ; i < n ; i++)
{
for ( j = 0 ; j < readlen ; j += 8)
{
handle_data( (ULONG32*)&data[j], DESENCRY );
}
}
return SUCCESS;
}
INT32 Ddes3(BYTE *data,BYTE *key,ULONG32 n ,ULONG32 readlen)
{
ULONG32 i = 0 , j = 0 ;
makefirstkey((ULONG32*)key) ; /*产生密钥*/
for ( i = 0 ; i < n ; i++)
{
for ( j = 0 ; j < readlen ; j += 8)
{
handle_data( (ULONG32*)&data[j], DESDECRY );
}
}
return SUCCESS;
}
INT32 des(BYTE *data, BYTE *key,INT32 readlen)
{
INT32 i = 0;
makefirstkey((ULONG32*)key) ; /*产生密钥*/
for ( i = 0 ; i < readlen ; i += 8)
{
handle_data( (ULONG32*)&data[i], DESENCRY );
}
return SUCCESS;
}
INT32 Ddes(BYTE *data,BYTE *key,INT32 readlen)
{
INT32 i = 0;
makefirstkey((ULONG32*)key) ;
for ( i = 0 ; i < readlen ; i += 8)
{
handle_data( (ULONG32*)&data[i] ,DESDECRY);
}
return SUCCESS;
}
INT32 handle_data(ULONG32 *left , INT 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 =