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

📄 des.cpp

📁 des加密解密代码
💻 CPP
字号:
#include "des.h"
void deskey(unsigned char * key,short edf,unsigned long *keystr)
{
	register int i,j,l,m,n;
	unsigned char pclm[56],pcr[56];
    unsigned long kn[32]={0L};
	unsigned long *raw0,*cook,*raw1;
	cook=keystr;
	raw1=kn;
	for(j=0;j<56;j++)
	{
		l=pc1[j];
		m=l&07;
		pclm[j]=(key[l>>3]&bytebit[m])?1:0;
	}
	for(i=0;i<16;i++)
	{
		if(edf==DE1) m=(15-i)<<1;
		else m=i<<1;
		n=m+1;
		kn[m]=kn[n]=0L;
		for(j=0;j<28;j++) 
		{
			l=j+totrot[i];
			if(l<28) pcr[j]=pclm[l];
			else pcr[j]=pclm[l-28];
		}
	for(j=28;j<56;j++){
			l=j+totrot[i];
			if(l<56) pcr[j]=pclm[l];
			else pcr[j]=pclm[l-28];
		}
	for(j=0;j<24;j++) {
		if( pcr[pc2[j]]) kn[m]|=bigbyte[j];
		if( pcr[pc2[j+24]]) kn[n]|=bigbyte[j];
	}
	}
		for(i=0;i<16;i++,raw1++) {
		raw0=raw1++;
		*cook   =(*raw0 & 0x00fc0000L)<<6;
		*cook  |=(*raw0 & 0x00000fc0L)<<10;
		*cook  |=(*raw1 & 0x00fc0000L)>>10;
        *cook++|=(*raw1 & 0x00000fc0L)>>6;
		*cook   =(*raw0 & 0x0003f000L)<<12;
        *cook  |=(*raw0 & 0x0000003fL)<<16;
		*cook  |=(*raw1 & 0x0003f000L)>>4;
		*cook++|=(*raw1 & 0x0000003fL);
	}
	return;
}

////////////////////////////////////////////////////////
//////////////////////////

static void scrunch(register unsigned char *outof,
					register unsigned long *into){
	*into   =(*outof++ & 0xffL)<<24;
	*into  |=(*outof++ & 0xffL)<<16;
	*into  |=(*outof++ & 0xffL)<<8;
	*into++|=(*outof++ & 0xffL);
	*into   =(*outof++ & 0xffL)<<24;
	*into  |=(*outof++ & 0xffL)<<16;
	*into  |=(*outof++ & 0xffL)<<8;
	*into  |=(*outof   & 0xffL);
	
}
static void unscrun(register unsigned long *outof,
                    register unsigned char *into)
{
	*into++=(unsigned char)((*outof>>24) & 0xffL);
	*into++=(unsigned char)((*outof>>16) & 0xffL);
	*into++=(unsigned char)((*outof>> 8) & 0xffL);
	*into++=(unsigned char)(*outof++     & 0xffL);
	*into++=(unsigned char)((*outof>>24) & 0xffL);
	*into++=(unsigned char)((*outof>>16) & 0xffL);
	*into++=(unsigned char)((*outof>> 8) & 0xffL);
	*into++=(unsigned char)(*outof       & 0xffL);
	return;
}
static void desfunc(register unsigned long *block,
					register unsigned long *keys,
					register unsigned long *mcipher)
{
	register unsigned long fval,work,right,leftt;
	register int round;
	leftt=block[0];
	right=block[1];
	work=((leftt>>4)^right) & 0x0f0f0f0fL;
	right^=work;
	leftt^=(work<<4);
	work=((leftt>>16)^right) & 0x0000ffffL;
	right^=work;
	leftt^=(work<<16);
	work=((right>>2)^leftt) & 0x33333333L;
	leftt^=work;
	right^=(work<<2);
	work=((right>>8)^leftt) & 0x00ff00ffL;
	leftt^=work;
	right^=(work<<8);
	right=((right<<1)|((right>>31)&1L)) & 0xffffffffL;
	work=(leftt^right) & 0xaaaaaaaaL;
	leftt^=work;
	right^=work;
	leftt=((leftt<<1)|((leftt>>31)&1L)) & 0xffffffffL;
	for(round=0;round<8;round++){
		work =(right<<28)|(right>>4);
		work^=*keys++;
		fval =sp7[work     & 0x3fL];
		fval|=sp5[(work>>8)&0x3fL];
		fval|=sp3[(work>>16)&0x3fL];
		fval|=sp1[(work>>24)&0x3fL];
		work =right^*keys++;
		fval|=sp8[work     & 0x3fL];
		fval|=sp6[(work>>8)&0x3fL];
		fval|=sp4[(work>>16)&0x3fL];
		fval|=sp2[(work>>24)&0x3fL];
		leftt^=fval;
		work =(leftt<<28)|(leftt>>4);
		work^=*keys++;
		fval =sp7[work     & 0x3fL];
		fval|=sp5[(work>>8)&0x3fL];
		fval|=sp3[(work>>16)&0x3fL];
		fval|=sp1[(work>>24)&0x3fL];
		work =leftt^*keys++;
		fval|=sp8[work     & 0x3fL];
		fval|=sp6[(work>>8)&0x3fL];
		fval|=sp4[(work>>16)&0x3fL];
		fval|=sp2[(work>>24)&0x3fL];
		right^=fval;
	}
    right=(right<<31)|(right>>1);
	work=(leftt^right) & 0xaaaaaaaaL;
	leftt^=work;
	right^=work;
	leftt=(leftt<<31)|(leftt>>1);
	work=((leftt>>8)^right) & 0x00ff00ffL;
	right^=work;
	leftt^=(work<<8);
	work=((leftt>>2)^right) & 0x33333333L;
	right^=work;
	leftt^=(work<<2);
	work=((right>>16)^leftt) & 0x0000ffffL;
	leftt^=work;
	right^=(work<<16);
	work=((right>>4)^leftt) & 0x0f0f0f0fL;
	leftt^=work;
	right^=(work<<4);
	*mcipher++=right;
	*mcipher=leftt;
	return;
}

void des_enc(unsigned char *fkey,unsigned char *data,unsigned char *cipher)
{
	unsigned long work[2];
	unsigned long mkey[32]={0L},mcipher[2]={0L};
	deskey(fkey,EN0,mkey);
	scrunch(data,work);
	desfunc(work,mkey,mcipher);
	unscrun(mcipher,cipher);
}

void des_dec(unsigned char *fkey,unsigned char *cipher,unsigned char *plaint){
		unsigned long work[2];
		unsigned long mkey[32]={0L},mplaint[2]={0L};
		deskey(fkey,DE1,mkey);
	
		scrunch(cipher,work);
		desfunc(work,mkey,mplaint);
	
		unscrun(mplaint,plaint);
	
	}
void main(void)
{
	int i;
	unsigned char data[81]={0};//{0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68};//{0x12,0x34,0x56,0x78,0x12,0x34,0x56,0x78};
	gets((char *)data);
	unsigned char key[81]="12345678";//{0x12,0x34,0x56,0x78,0x12,0x34,0x56,0x78};
	unsigned char cipher[8]={0L},plaint[8]={0L};
	des_enc(key,data,cipher);
	printf("加密明文消息");
	for(i=0;i<8;i++)
		printf("%02x",cipher[i]);
	printf("\n");
	des_dec(key,cipher,plaint);                     
	printf("解密密文");
	for(i=0;i<8;i++)
		printf("%02x",plaint[i]);
	printf("\n");        
}

⌨️ 快捷键说明

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