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

📄 des1.cpp

📁 VOD点歌系统
💻 CPP
字号:
// DES1.cpp: implementation of the CDES class.
//
//////////////////////////////////////////////////////////////////////

//#include "stdafx.h"
#include "DES1.h"
#include <stdlib.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#define IP_TABLE	0
#define IP_1_TABLE	1
#define E_TABLE		2
#define P_TABLE		3
#define PC1_TABLE	4
#define PC2_TABLE	5

////////////////////////////////////////////////////////
// g_nOut: number of output bits from each table
int g_nOut[6] = {64,64,48,32,56,48};

////////////////////////////////////////////////////////
// TABLE_ip: INITIAL PERMUTATION
int TABLE_ip[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
		} ;
////////////////////////////////////////////////////////
// TABLE_ip_1: INVERSE INITIAL PERMUTATION
int TABLE_ip_1[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
		} ;
////////////////////////////////////////////////////////
// TABLE_e: Expansion Function
int TABLE_ePro[48] = {   
		32,	5,	1,	2,	3,	4,
		4,	9,	5,	6,	7,	8,
		8,	13,	9,	10,	11,	12,
		12,	17,	13,	14,	15,	16,
		16,	21,	17,	18,	19,	20,
		20,	25,	21,	22,	23,	24,
		24,	29,	25,	26,	27,	28,
		28,	1,	29,	30,	31,	32
		} ;
////////////////////////////////////////////////////////
// p: Permutation Function P
int TABLE_p[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
		} ;
////////////////////////////////////////////////////////
// pc_1: Permuted Choice1
int TABLE_pc_1[56] = {
		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,
		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} ;
////////////////////////////////////////////////////////
// pc_2: Permuted Choice2
int TABLE_pc_2Pro[48] = {
		14,	5,	17,	11,	24,	1,	
		3,	10,	28,	15,	6,	21,	
		23,	8,	19,	12,	4,	26,	
		16,	2,	7,	27,	20,	13,	
		41,	55,	52,	31,	37,	47,	
		30,	48,	40,	51,	45,	33,	
		44,	53,	49,	39,	56,	34,	
		46,	32,	42,	50,	36,	29
} ;
////////////////////////////////////////////////////////
// s: Selection Function (s1...s8)
BYTE TABLE_s[8][64] = 
{
	// S1
	{
		14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
		0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
		4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
		15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
	} ,
	// S2
	{	
		15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
		3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
		0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
		13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
	},
	// S3
	{	
		10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
		13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
		13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
		1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
	},
	// S4
	{
		7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
		13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
		10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
		3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
	},
	// S5
	{	
		2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
		14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
		4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
		11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,
	},
	// S6
	{
		12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
		10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
		9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
		4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
	},
	// S7
	{
		4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
		13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
		1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
		6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
	},
	// S8
	{
		13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
		1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
		7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
		2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
	}
};
/*
 TABLE_shift: Left SHifts of C and D blocks
*/
int TABLE_shift[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1} ;

void Encrypt(unsigned char *src, unsigned char *key, unsigned char *dst)
{
	DesAlgo(src, key, dst, 1);
}

void Decrypt(unsigned char *src, unsigned char *key, unsigned char *dst)
{
	DesAlgo(src, key, dst, 0);
}

void DesAlgo(unsigned char *src, unsigned char *key, unsigned char *dst, int bEncrypt)
{
	unsigned char input[64];			/* store input data*/
	unsigned char output[64];		/* store output data*/
	unsigned char KEY[64];			/* store key*/
        int i,i1,j;
	unsigned char temp2[8];
	unsigned char temp7[7];
	unsigned char temp32[32];
	unsigned char temp64[64];
	unsigned char K[48];		/* store subkey --- K[n], n=1,2,...16*/
	unsigned char fResult[32];	/* store result of fFunction(),*/

	LRStruct LR;			/* store L[i],R[i] --- temp data after every circle*/
	CDStruct CD;			/* store C[i],D[i] --- temp Result of Shift()*/

	BitToByte(src, input, 8); /* src (64 bits) ==>input (64 unsigned chars)*/
	SelectExchangeFromTable(IP_TABLE,input,(unsigned char*)&LR);  /* INPUT ==> IP ==> L0,R0*/

	BitToByte(key, KEY, 8); /* key (64 bits) ==>KEY (64 unsigned chars)*/
	SelectExchangeFromTable(PC1_TABLE,KEY,(unsigned char*)&CD);  /* KEY ==> PC_1 ==> C0,D0*/

	for (i=0; i<16; i++)
	{
		/*unsigned char K[48];*/		/* store subkey --- K[n], n=1,2,...16*/
		/*unsigned char fResult[32];*/	/* store result of fFunction(),*/
							/* then store result of L[i].EOR.f[i].*/

		/* get K(i+1) */
		Shift(&CD, i, bEncrypt);	/* C[i],D[i] ==> shifting ==> C[i+1],D[i+1]*/
                {
                        /*unsigned char temp[7];*/
                        ByteToBit((unsigned char*)&CD,temp7,7);
                }
		SelectExchangeFromTable(PC2_TABLE,(unsigned char*)&CD, K);  /*  C[i+1],D[i+1] ==> PC_2 ==> K[i+1]*/
                {
                        /*unsigned char temp[64];*/
                        for(i1=0; i1<8; i1++)
                        {
                                temp64[i1*8+0]=0;
                                temp64[i1*8+1]=0;
                                for (j=0; j<6; j++)
                                {
                                        temp64[i1*8+(j+2)]=K[i1*6+j];
                                }
                        }
                        ByteToBit(temp64,temp2,8);
                }

		/* fResult[i] <== f( R[i], K[i+1] )*/
		fFunction(LR.R, K, fResult);

		/* fResult[i] <== L[i] .EOR. f[i]*/
		EOR(LR.L, fResult, 32);

		/* L(i+1) <== R[i]*/
		memcpy(LR.L, LR.R, sizeof(unsigned char)*32);

		/* R(i+1) <== fResult[i]*/
		memcpy(LR.R, fResult, sizeof(unsigned char)*32);
	}
	/* LR <== RL*/
	memcpy(temp32, LR.L,sizeof(unsigned char)*32);
	memcpy(LR.L, LR.R,sizeof(unsigned char)*32);
	memcpy(LR.R, temp32,sizeof(unsigned char)*32);

	SelectExchangeFromTable(IP_1_TABLE, (unsigned char*)&LR, output);  /* L16R16 ==> IP_1 ==> output*/
	ByteToBit(output, dst, 8);/* dst (64 bits) <==  output (64 unsigned chars)*/
}

void BitToByte(unsigned char *src, unsigned char *dst, int n)
{
        int i;
        unsigned char bs;
	for (i=0; i<n; i++)
	{
		/*
                BYTEStruct *BS = (BYTEStruct* )src[i];
                */
                bs = 0;
                bs = 0x01 & src[i];
                if(bs > 0)
		dst[i*8+7] = 1;
                else
                dst[i*8+7] = 0;

                bs = 0;
                bs = 0x02 & src[i];
                if(bs > 0)
		dst[i*8+6] = 1;
                else
                dst[i*8+6] = 0;

                bs = 0;
                bs = 0x04 & src[i];
                if(bs > 0)
		dst[i*8+5] = 1;
                else
                dst[i*8+5] = 0;

                bs = 0;
                bs = 0x08 & src[i];
                if(bs > 0)
		dst[i*8+4] = 1;
                else
                dst[i*8+4] = 0;

                bs = 0;
                bs = 0x10 & src[i];
                if(bs > 0)
		dst[i*8+3] = 1;
                else
                dst[i*8+3] = 0;

                bs = 0;
                bs = 0x20 & src[i];
                if(bs > 0)
		dst[i*8+2] = 1;
                else
                dst[i*8+2] = 0;

                bs = 0;
                bs = 0x40 & src[i];
                if(bs > 0)
		dst[i*8+1] = 1;
                else
                dst[i*8+1] = 0;

                bs = 0;
                bs = 0x80 & src[i];
                if(bs > 0)
		dst[i*8+0] = 1;
                else
                dst[i*8+0] = 0;
	}
}

void ByteToBit(unsigned char *src, unsigned char *dst, int n)
{
        int i;
        unsigned char bs;
	for (i=0; i<n; i++)
	{
                /*
		BYTEStruct *BS = (BYTEStruct* )dst[i];
                */

                bs = 0;
                if(src[i*8+7] > 0)
                bs |= 0x01;

                if(src[i*8+6] > 0)
                bs |= 0x02;

                if(src[i*8+5] > 0)
                bs |= 0x04;

                if(src[i*8+4] > 0)
                bs |= 0x08;

                if(src[i*8+3] > 0)
                bs |= 0x10;

                if(src[i*8+2] > 0)
                bs |= 0x20;

                if(src[i*8+1] > 0)
                bs |= 0x40;

                if(src[i*8+0] > 0)
                bs |= 0x80;

                dst[i] = bs;
                /*
		BS->b0 = src[i*8+7];
		BS->b1 = src[i*8+6];
		BS->b2 = src[i*8+5];
		BS->b3 = src[i*8+4];
		BS->b4 = src[i*8+3];
		BS->b5 = src[i*8+2];
		BS->b6 = src[i*8+1];
		BS->b7 = src[i*8+0];
                */
	}
}

/**/
void SelectExchangeFromTable(int nTable, unsigned char *src, unsigned char *dst)
{
	static int * table[6] = {TABLE_ip, TABLE_ip_1, TABLE_ePro, TABLE_p, TABLE_pc_1, TABLE_pc_2Pro};

	int nOut,i;
	int *pTable;

	nOut = g_nOut[nTable];
	pTable = table[nTable];

	for (i=0; i< nOut; i++)
	{
		dst[i] = src[pTable[i]-1];
	}

}

void Shift(CDStruct * pCD, int iCircle, int bEncrypt)
{
	if ( 0 == bEncrypt)
	{
		/* When decprypting, the shift direction is opposite to*/
		/* encrypting , but the shift value is the same as*/
		/* encprypting except the first time.*/
		/* The first time Decprypting needn't shift.*/
		if ( iCircle != 0)
		{
			RShift(pCD->C, TABLE_shift[iCircle]);
			RShift(pCD->D, TABLE_shift[iCircle]);
		}
	}
	else
	{
		LShift(pCD->C, TABLE_shift[iCircle]);
		LShift(pCD->D, TABLE_shift[iCircle]);
	}
}

/* 28 unsigned chars Right shift nShift time(s)*/
void RShift(unsigned char *buf28, int nShift)
{
	int i;
	unsigned char temp[2];

	for (i=0; i<nShift; i++)
	{
		temp[nShift-1-i] = buf28[28-1-i];
	}

	for (i=28-1; i>=nShift; i--)
	{
		buf28[i] = buf28[i-nShift];
	}

	for (i=0; i<nShift; i++)
	{
		buf28[i] = temp[i];
	}
}

/* 28 unsigned chars Left shift nShift time(s)*/
void LShift(unsigned char *buf28, int nShift)
{
	int i;
	unsigned char temp[2];

	for (i=0; i<nShift; i++)
	{
		temp[i] = buf28[i];
	}

	for (i=nShift; i<28; i++)
	{
		buf28[i-nShift] = buf28[i];
	}

	for (i=0; i<nShift; i++)
	{
		buf28[28-nShift+i] = temp[i] ;
	}
}

/* result <== f( R, K )*/
void fFunction(unsigned char* R, unsigned char* K, unsigned char* result)
{
	unsigned char temp[48];	/* store temp data*/
	unsigned char SResult[32];	/* store result of S_Change*/

	/* R ==> E_TABLE ==> temp*/
	SelectExchangeFromTable(E_TABLE, R, temp);

	/* temp .EOR. K ==> temp*/
	EOR(K, temp, 48);

	/* temp ==> S_Change ==> SResult*/
	S_Change(temp, SResult);

	/* SResult ==> P_TABLE ==> result*/
	SelectExchangeFromTable(P_TABLE, SResult, result);
}

/* dst <== src .EOR. dst*/
void EOR(unsigned char * src, unsigned char* dst, int n)
{
        int i;
	for (i=0; i<n; i++)
	{
		dst[i] = (src[i] + dst[i])%2 ;
	}
}

/* src ==> S_Change ==> src*/
void S_Change(unsigned char* src, unsigned char* result)
{
	unsigned char sTemp;
        int i;
        unsigned char nPos;
	for (i=0; i<8; i++)
	{
                /*
		BYTEStruct* rBSPos = (BYTEStruct*)nPos ;
                */
		nPos = 0;

                if(src[i*6+5] > 0)
                nPos |= 0x01;
                if(src[i*6+4] > 0)
                nPos |= 0x02;
                if(src[i*6+3] > 0)
                nPos |= 0x04;
                if(src[i*6+2] > 0)
                nPos |= 0x08;
                if(src[i*6+1] > 0)
                nPos |= 0x10;
                if(src[i*6+0] > 0)
                nPos |= 0x20;
                /*
		rBSPos->b0 = src[i*6+5];
		rBSPos->b1 = src[i*6+4];
		rBSPos->b2 = src[i*6+3];
		rBSPos->b3 = src[i*6+2];
		rBSPos->b4 = src[i*6+1];
		rBSPos->b5 = src[i*6+0];
                */
		if (i%2==0)
		{
			/* S Table Result ==> High 4 bits of sTemp*/
			sTemp= TABLE_s[i][nPos]*16;
		}
		else
		{
			/* S Table Result ==> Low 4 bits of sTemp*/
			sTemp += TABLE_s[i][nPos];

			/* sTemp Expand to 8 unsigned chars*/
			BitToByte(&sTemp, &result[i/2*8], 1);
		}
	}

}

⌨️ 快捷键说明

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