⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des.c

📁 一直使用的des/3des代码
💻 C
📖 第 1 页 / 共 2 页
字号:

 /* generate 28 bit blocks using 'Permuted Choice 1' */
 /* generate 'C' block */
 /* Ian you can use memset function here but I do not have the book here, so ...*/
 c_block[0] = c_block[1] = c_block[2] = c_block[3] = 0;
 for (i=0; i<28; i++) {
   k = perm_choice1_C[i];
   if (key[k>>3] & bitmapx[k])
     c_block[i>>3] |= bitmapx[i]; /* set bit if needed */
   }
// printf("\nc_block:");
// for (i=0;i<4;i++) printf("%02x",c_block[i]);
 /* generate 'D' block */
 d_block[0] = d_block[1] = d_block[2] = d_block[3] = 0;
 for (i=0; i<28; i++) {
   k = perm_choice1_D[i];
   if (key[k>>3] & bitmapx[k])
     d_block[i>>3] |= bitmapx[i]; /* set bit if needed */
   }
// printf("\nd_block:");
// for (i=0;i<4;i++) printf("%02x",d_block[i]);

 /* perform 'shifts' and generate 'Permuted Keys' */
 for (k=0; k<16; k++) {

   /* 'left shift' (rotate!) C block */
   rotate(c_block, shifts_table[k]);

   /* 'left shift' (rotate!) D block */
   rotate(d_block, shifts_table[k]);

   perm_keys[k][0] = perm_keys[k][1] =
   perm_keys[k][2] = perm_keys[k][3] =
   perm_keys[k][4] = perm_keys[k][5] = 0;

   /* run 'Permuted Choice 2' to generate 48 bit 'Permuted Key' */
   for (i=0; i<48; i++) {
     data = perm_choice2[i];
     if (data < 28) {	/* selected bit is in 'C' block */
       if (c_block[data>>3] & bitmapx[data])
	 perm_keys[k][i>>3] |= bitmapx[i]; /* set bit if needed */
     } else {	/* selected bit is in 'D' block */
       data -= 28;
       if (d_block[data>>3] & bitmapx[data])
	 perm_keys[k][i>>3] |= bitmapx[i]; /* set bit if needed */
     }
   }
 }

 } /* gen_keys() */

/**********************************************************************
function : Ciphers 64 bit block of data.
input    : Pointers to 4 word input & output data arrays.
	   mode 0 = encry
           mode 1 = decry
output	 : none.
call by  : user
**********************************************************************/
	
void cipher(byte *input, byte *output, byte mode) {
 
 byte i;
 byte last[8], temp[4];

 /* perform 'Initial Permutation' */
 do_ip(input, last, ip_table);
// printf("\nip:");
// for (i=0;i<8;i++) printf("%02x",last[i]);

 /* Generate 'Preoutput' */
 for (i=0; i<16; i++) {

   if (mode) function_rk(&last[4], temp, perm_keys[15-i]);
   else function_rk(&last[4], temp, perm_keys[i]);

   /* XOR result with lower bits of last iteration */
   temp[0] ^= last[0];
   temp[1] ^= last[1];
   temp[2] ^= last[2];
   temp[3] ^= last[3];

   /* copy high 32 bits of previous iteration to low bits */
   last[0] = last[4];
   last[1] = last[5];
   last[2] = last[6];
   last[3] = last[7];

   /* copy result of XOR to high bits */
   last[4] = temp[0];
   last[5] = temp[1];
   last[6] = temp[2];
   last[7] = temp[3];
 }

 i       = last[4];
 last[4] = last[0];
 last[0] = i;
 i       = last[5];
 last[5] = last[1];
 last[1] = i;
 i       = last[6];
 last[6] = last[2];
 last[2] = i;
 i       = last[7];
 last[7] = last[3];
 last[3] = i;

 /* perform 'Inverse Initial Permutation' */
 do_ip(last, output, inv_ip_table);
} /* cipher() */


void DES(byte *dg,byte *key_ptr)
{
    byte des_buf[8];
    memcpy(key,key_ptr,8);
    gen_keys();
    cipher(dg,des_buf,0);
    memcpy(dg,des_buf,8);
}
void _DES(byte *dg,byte *key_ptr)
{   byte des_buf[8];
    memcpy(key,key_ptr,8);
    gen_keys();
    cipher(dg,des_buf,1);
    memcpy(dg,des_buf,8);
}
void DESDEC(byte *dg,byte *key_ptr)
{   byte des_buf[8];
    memcpy(key,key_ptr,8);
    gen_keys();
    cipher(dg,des_buf,1);
    memcpy(dg,des_buf,8);
}
unsigned char hexofchar(char inpu){
	char inpu1;
	unsigned char outpu1,outpu;
	inpu1=inpu;
	switch(inpu1){
	case '0':outpu1=0x0;break;
	case '1':outpu1=0x1;break;
	case '2':outpu1=0x2;break;
	case '3':outpu1=0x3;break;
	case '4':outpu1=0x4;break;
	case '5':outpu1=0x5;break;
	case '6':outpu1=0x6;break;
	case '7':outpu1=0x7;break;
	case '8':outpu1=0x8;break;
	case '9':outpu1=0x9;break;
	case 'A':outpu1=0xa;break;
	case 'B':outpu1=0xb;break;
	case 'C':outpu1=0xc;break;
	case 'D':outpu1=0xd;break;
	case 'E':outpu1=0xe;break;
	case 'F':outpu1=0xf;break;
	case 'a':outpu1=0xa;break;
	case 'b':outpu1=0xb;break;
	case 'c':outpu1=0xc;break;
	case 'd':outpu1=0xd;break;
	case 'e':outpu1=0xe;break;
	case 'f':outpu1=0xf;break;
	default: outpu1=0x0;break;
	}
	outpu=outpu1;
return outpu;
}

//void dat_str_xor(byte *ptr1 ,byte *ptr2, WORD len)
void dat_str_xor(char *ptr1 ,char *ptr2, WORD len)
{
	while (len-->0)
    {
		*ptr1 ^= *ptr2;
		ptr1 ++; ptr2++;
	}
	return;
}

void DesData(char *out,char *key,char *input)
{
	unsigned char byteout[64];
	unsigned char bytekey[64];
	unsigned char byteinput[64];
	uchar_of_charhexstr(byteout,  out,  8);
	uchar_of_charhexstr(bytekey,  key,  8);
	uchar_of_charhexstr(byteinput,input,8);
	DES(byteinput,bytekey);
	charhexstr_of_uchar(out,byteinput,8);
}

void Des3Data(char *out,char *key,char *input)
{
	unsigned char byteout[64];
	unsigned char bytekey[64];
	unsigned char byteinput[64];
	uchar_of_charhexstr(byteout,  out,  8);
	uchar_of_charhexstr(bytekey,  key,  16);
	uchar_of_charhexstr(byteinput,input,8);
	DES(byteinput,bytekey);
	_DES(byteinput,bytekey+8);
	DES(byteinput,bytekey);
	charhexstr_of_uchar(out,byteinput,8);
}

void _DesData(char *out,char *key,char *input)
{
	unsigned char byteout[64];
	unsigned char bytekey[64];
	unsigned char byteinput[64];
	uchar_of_charhexstr(byteout,  out,  8);
	uchar_of_charhexstr(bytekey,  key,  8);
	uchar_of_charhexstr(byteinput,input,8);
	_DES(byteinput,bytekey);
	charhexstr_of_uchar(out,byteinput,8);
}

void _Des3Data(char *out,char *key,char *input)
{
	unsigned char byteout[64];
	unsigned char bytekey[64];
	unsigned char byteinput[64];
	uchar_of_charhexstr(byteout,  out,  8);
	uchar_of_charhexstr(bytekey,  key,  16);
	uchar_of_charhexstr(byteinput,input,8);
	_DES(byteinput,bytekey);
	DES(byteinput,bytekey+8);
	_DES(byteinput,bytekey);
	charhexstr_of_uchar(out,byteinput,8);
}

void TripleDES(byte *dg,byte *key_ptr)
{
	DES(dg,key_ptr);
	_DES(dg,key_ptr+8);
	DES(dg,key_ptr);
}
void _TripleDES(byte *dg,byte *key_ptr)
{
	_DES(dg,key_ptr);
	DES(dg,key_ptr+8);
	_DES(dg,key_ptr);
}
void TriDES(byte *dg,byte *key_ptr)
{
	DES(dg,key_ptr);
	_DES(dg,key_ptr+8);
	DES(dg,key_ptr);
}
void _TriDES(byte *dg,byte *key_ptr)
{
	_DES(dg,key_ptr);
	DES(dg,key_ptr+8);
	_DES(dg,key_ptr);
}
void TDES(byte *dg,byte *key_ptr)
{
	DES(dg,key_ptr);
	_DES(dg,key_ptr+8);
	DES(dg,key_ptr);
}
void _TDES(byte *dg,byte *key_ptr)
{
	_DES(dg,key_ptr);
	DES(dg,key_ptr+8);
	_DES(dg,key_ptr);
}

void DESCBCs(char *datas,char *keys,char *macs)
{
	unsigned long ulen;
	unsigned char ucdata[1024];
	//unsigned char ucmac[10];
	unsigned char ucdg[10];
	unsigned char uckey[20];
	unsigned char *dg;
	unsigned char *key;
	dg=ucdg;
	key=uckey;
	
	ulen = strlen(datas)/2;
	uchar_of_charhexstr(uckey,keys,16);
	uchar_of_charhexstr(ucdata,datas,ulen);
	if ((ulen%8) == 0){
		memcpy(&ucdata[ulen],"\x80\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0",8);
		ulen=ulen+8;
	}
	if ((ulen%8) != 0){
		memcpy(&ucdata[ulen],"\x80\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0",8-(ulen%8));
		ulen=ulen+8-(ulen%8);
	}
	int times = ulen/8;
	memset(dg,0x00,8);
	for(int i = 0 ; i< times ; i++){
		for (int j = 0 ; j < 8 ; j++){dg[j]=dg[j]^ucdata[(i<<3)+j];}
		DES(dg,key);
		_DES(dg,key+8);
		DES(dg,key);
	}
	charhexstr_of_uchar(macs,dg,8);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -