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

📄 3des.cpp

📁 实现DES加密解密算法
💻 CPP
字号:
#include "stdafx.h"
#include "3DES.h"

void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
{
	register DES_LONG l,r,t,u;
#ifdef DES_PTR
	register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
#endif
#ifndef DES_UNROLL
	register int i;
#endif
	register DES_LONG *s;

	r=data[0];
	l=data[1];

	r=ROTATE(r,29)&0xffffffffL;
	l=ROTATE(l,29)&0xffffffffL;

	s=ks->ks->deslong;
	/* I don't know if it is worth the effort of loop unrolling the
	* inner loop */
	if (enc)
	{
#ifdef DES_UNROLL
		D_ENCRYPT(l,r, 0); /*  1 */
		D_ENCRYPT(r,l, 2); /*  2 */
		D_ENCRYPT(l,r, 4); /*  3 */
		D_ENCRYPT(r,l, 6); /*  4 */
		D_ENCRYPT(l,r, 8); /*  5 */
		D_ENCRYPT(r,l,10); /*  6 */
		D_ENCRYPT(l,r,12); /*  7 */
		D_ENCRYPT(r,l,14); /*  8 */
		D_ENCRYPT(l,r,16); /*  9 */
		D_ENCRYPT(r,l,18); /*  10 */
		D_ENCRYPT(l,r,20); /*  11 */
		D_ENCRYPT(r,l,22); /*  12 */
		D_ENCRYPT(l,r,24); /*  13 */
		D_ENCRYPT(r,l,26); /*  14 */
		D_ENCRYPT(l,r,28); /*  15 */
		D_ENCRYPT(r,l,30); /*  16 */
#else
		for (i=0; i<32; i+=8)
		{
			D_ENCRYPT(l,r,i+0); /*  1 */
			D_ENCRYPT(r,l,i+2); /*  2 */
			D_ENCRYPT(l,r,i+4); /*  3 */
			D_ENCRYPT(r,l,i+6); /*  4 */
		}
#endif
	}
	else
	{
#ifdef DES_UNROLL
		D_ENCRYPT(l,r,30); /* 16 */
		D_ENCRYPT(r,l,28); /* 15 */
		D_ENCRYPT(l,r,26); /* 14 */
		D_ENCRYPT(r,l,24); /* 13 */
		D_ENCRYPT(l,r,22); /* 12 */
		D_ENCRYPT(r,l,20); /* 11 */
		D_ENCRYPT(l,r,18); /* 10 */
		D_ENCRYPT(r,l,16); /*  9 */
		D_ENCRYPT(l,r,14); /*  8 */
		D_ENCRYPT(r,l,12); /*  7 */
		D_ENCRYPT(l,r,10); /*  6 */
		D_ENCRYPT(r,l, 8); /*  5 */
		D_ENCRYPT(l,r, 6); /*  4 */
		D_ENCRYPT(r,l, 4); /*  3 */
		D_ENCRYPT(l,r, 2); /*  2 */
		D_ENCRYPT(r,l, 0); /*  1 */
#else
		for (i=30; i>0; i-=8)
		{
			D_ENCRYPT(l,r,i-0); /* 16 */
			D_ENCRYPT(r,l,i-2); /* 15 */
			D_ENCRYPT(l,r,i-4); /* 14 */
			D_ENCRYPT(r,l,i-6); /* 13 */
		}
#endif
	}
	/* rotate and clear the top bits on machines with 8byte longs */
	data[0]=ROTATE(l,3)&0xffffffffL;
	data[1]=ROTATE(r,3)&0xffffffffL;
	l=r=t=u=0;
}

void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
				  DES_key_schedule *ks2, DES_key_schedule *ks3)
{
	register DES_LONG l,r;

	l=data[0];
	r=data[1];
	IP(l,r);
	data[0]=l;
	data[1]=r;
	DES_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
	DES_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
	DES_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
	l=data[0];
	r=data[1];
	FP(r,l);
	data[0]=l;
	data[1]=r;
}

void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
				  DES_key_schedule *ks2, DES_key_schedule *ks3)
{
	register DES_LONG l,r;

	l=data[0];
	r=data[1];
	IP(l,r);
	data[0]=l;
	data[1]=r;
	DES_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
	DES_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
	DES_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
	l=data[0];
	r=data[1];
	FP(r,l);
	data[0]=l;
	data[1]=r;
}


void DES_set_odd_parity(DES_cblock *key)
{
	int i;

	for (i=0; i<DES_KEY_SZ; i++)
		(*key)[i]=odd_parity[(*key)[i]];
}

int DES_check_key_parity(const_DES_cblock *key)
{
	int i;

	for (i=0; i<DES_KEY_SZ; i++)
	{
		if ((*key)[i] != odd_parity[(*key)[i]])
			return(0);
	}
	return(1);
}


int DES_is_weak_key(const_DES_cblock *key)
{
	int i;

	for (i=0; i<NUM_WEAK_KEY; i++)
		if (memcmp(weak_keys[i],key,sizeof(DES_cblock)) == 0) return(1);
	return(0);
}


void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule)
{
	static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
	register DES_LONG c,d,t,s,t2;
	register const unsigned char *in;
	register DES_LONG *k;
	register int i;

#ifdef OPENBSD_DEV_CRYPTO
	memcpy(schedule->key,key,sizeof schedule->key);
	schedule->session=NULL;
#endif
	k = &schedule->ks->deslong[0];
	in = &(*key)[0];

	c2l(in,c);
	c2l(in,d);

	PERM_OP (d,c,t,4,0x0f0f0f0fL);
	HPERM_OP(c,t,-2,0xcccc0000L);
	HPERM_OP(d,t,-2,0xcccc0000L);
	PERM_OP (d,c,t,1,0x55555555L);
	PERM_OP (c,d,t,8,0x00ff00ffL);
	PERM_OP (d,c,t,1,0x55555555L);
	d=	(((d&0x000000ffL)<<16L)| (d&0x0000ff00L)     |
		((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
	c&=0x0fffffffL;

	for (i=0; i<ITERATIONS; i++)
	{
		if (shifts2[i])
		{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
		else
		{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
		c&=0x0fffffffL;
		d&=0x0fffffffL;
		s=	des_skb[0][ (c    )&0x3f                ]|
			des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
			des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
			des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
			((c>>22L)&0x38)];
		t=	des_skb[4][ (d    )&0x3f                ]|
			des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
			des_skb[6][ (d>>15L)&0x3f                ]|
			des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];

		/* table contained 0213 4657 */
		t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
		*(k++)=ROTATE(t2,30)&0xffffffffL;

		t2=((s>>16L)|(t&0xffff0000L));
		*(k++)=ROTATE(t2,26)&0xffffffffL;
	}
}

int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule)
{
	if (!DES_check_key_parity(key))
		return(-1);
	if (DES_is_weak_key(key))
		return(-2);
	DES_set_key_unchecked(key, schedule);
	return 0;
}


int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule)
{
	if (false)
	{
		return DES_set_key_checked(key, schedule);
	}
	else
	{
		DES_set_key_unchecked(key, schedule);
		return 0;
	}
}

int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule)
{
	return(DES_set_key(key,schedule));
}

void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output,
					  DES_key_schedule *ks1, DES_key_schedule *ks2,
					  DES_key_schedule *ks3,
					  int enc)
{
	register DES_LONG l0,l1;
	DES_LONG ll[2];
	const unsigned char *in = &(*input)[0];
	unsigned char *out = &(*output)[0];

	c2l(in,l0);
	c2l(in,l1);
	ll[0]=l0;
	ll[1]=l1;
	if (enc)
		DES_encrypt3(ll,ks1,ks2,ks3);
	else
		DES_decrypt3(ll,ks1,ks2,ks3);
	l0=ll[0];
	l1=ll[1];
	l2c(l0,out);
	l2c(l1,out);
}
/////////////////////////////////////加解密函数///////////////////////////
void EncryptLastBytes(char* inBuffer, unsigned int  inLen, char* outBuffer )
{
	char index[ 7 ][ 8 ] = 
	{ { 0x04 , 0x06 , 0x01 , 0x02 , 0x00 , 0x03 , 0x07 , 0x05 },
	  { 0x01 , 0x05 , 0x04 , 0x00 , 0x07 , 0x03 , 0x06 , 0x02 },
	  { 0x06 , 0x03 , 0x05 , 0x07 , 0x01 , 0x02 , 0x00 , 0x04 },
	  { 0x07 , 0x03 , 0x00 , 0x04 , 0x06 , 0x01 , 0x05 , 0x02 },
	  { 0x03 , 0x07 , 0x01 , 0x05 , 0x04 , 0x02 , 0x06 , 0x00 },
	  { 0x04 , 0x07 , 0x06 , 0x00 , 0x02 , 0x03 , 0x05 , 0x01 },
	  { 0x03 , 0x05 , 0x01 , 0x04 , 0x02 , 0x00 , 0x07 , 0x06 }};

     if ( inLen <=0 || inLen >7 ) return;

	 char a,b,c,d,e,f,g,h;
	 a = b = c = d = e = f = g = h = 0;
	 char result = 0;

	 memcpy(outBuffer,inBuffer,inLen);

	 for (unsigned int i = 0; i < inLen; i++)
	 {
		 a = ( outBuffer[ i ] & 0x01) << index[ i ][ 0 ];
		 b = ((outBuffer[ i ] & 0x02) >> 1) << index[ i ][ 1 ];
		 c = ((outBuffer[ i ] & 0x04) >> 2) << index[ i ][ 2 ];
		 d = ((outBuffer[ i ] & 0x08) >> 3) << index[ i ][ 3 ];
		 e = ((outBuffer[ i ] & 0x10) >> 4) << index[ i ][ 4 ];
		 f = ((outBuffer[ i ] & 0x20) >> 5) << index[ i ][ 5 ];
		 g = ((outBuffer[ i ] & 0x40) >> 6) << index[ i ][ 6 ];
		 h = ((outBuffer[ i ] & 0x80) >> 7) << index[ i ][ 7 ];
         
         result = (((((((a | b) | c) | d) | e) | f) | g) | h);
         result ^= 0x96; 
         
         outBuffer[ i ] = result;
	 }

}

void DecryptLastBytes(char* inBuffer, unsigned int  inLen, char* outBuffer )
{
	char tempInBuf[ 7 ];	
	//因为原数据不可写,所以需要转存到副本里面
	memcpy(tempInBuf, inBuffer, inLen);

	char index[ 7 ][ 8 ] = 
	{ { 0x04 , 0x02 , 0x03 , 0x05 , 0x00 , 0x07 , 0x01 , 0x06 },
	  { 0x03 , 0x00 , 0x07 , 0x05 , 0x02 , 0x01 , 0x06 , 0x04 },
      { 0x06 , 0x04 , 0x05 , 0x01 , 0x07 , 0x02 , 0x00 , 0x03 },
	  { 0x02 , 0x05 , 0x07 , 0x01 , 0x03 , 0x06 , 0x04 , 0x00 },
	  { 0x07 , 0x02 , 0x05 , 0x00 , 0x04 , 0x03 , 0x06 , 0x01 },
	  { 0x03 , 0x07 , 0x04 , 0x05 , 0x00 , 0x06 , 0x02 , 0x01 },
	  { 0x05 , 0x02 , 0x04 , 0x00 , 0x03 , 0x01 , 0x07 , 0x06 }
	};

     if ( inLen <=0 || inLen >7 ) return;

	 char a,b,c,d,e,f,g,h;
	 a = b = c = d = e = f = g = h = 0;
	 char result = 0;

	 for (unsigned int i = 0; i < inLen; i++)
	 {
         tempInBuf[ i ] ^= 0x96; 

		 a = ( tempInBuf[ i ] & 0x01) << index[ i ][ 0 ];
		 b = ((tempInBuf[ i ] & 0x02) >> 1) << index[ i ][ 1 ];
		 c = ((tempInBuf[ i ] & 0x04) >> 2) << index[ i ][ 2 ];
		 d = ((tempInBuf[ i ] & 0x08) >> 3) << index[ i ][ 3 ];
		 e = ((tempInBuf[ i ] & 0x10) >> 4) << index[ i ][ 4 ];
		 f = ((tempInBuf[ i ] & 0x20) >> 5) << index[ i ][ 5 ];
		 g = ((tempInBuf[ i ] & 0x40) >> 6) << index[ i ][ 6 ];
		 h = ((tempInBuf[ i ] & 0x80) >> 7) << index[ i ][ 7 ];
         
         result = (((((((a | b) | c) | d) | e) | f) | g) | h);
         
         outBuffer[ i ] = result;
	 }
}

⌨️ 快捷键说明

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