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

📄 des.c

📁 这是从目前银行里
💻 C
字号:
/*=///////////////////////////////////////////////////////////////////////=*/
/*= DES.c   DES算法声明及实现,用于DSP下的标准C语言版本,Made By sck007   =*/
/*=///////////////////////////////////////////////////////////////////////=*/

#include "DES.h"

#pragma DATA_SECTION(C_des, ".vglobal")
static unsigned char C_des[17][28];

#pragma DATA_SECTION(D_des, ".vglobal")
static unsigned char D_des[17][28];

#pragma DATA_SECTION(K_des, ".vglobal")
static unsigned char K_des[17][48];

/*=///////////////////////////////////////////////////////////////////////=*/

static void expand0(const unsigned char *in, unsigned char *out)
{
	int divide;
	int i, j;
	
	for(i=0; i<8; i++)
	{
		divide = 0x80;
		for(j=0; j<8; j++)
		{
			*out++ = ((in[i] / divide) & 1);
			divide /= 2;
		}
	}
}

static void LS(const unsigned char *bits, unsigned char *buffer, int count)
{
	int i;
	for(i=0; i<28; i++)
	{
		buffer[i] = bits[(i + count) % 28];
	}
}

static void son(const unsigned char *cc, const unsigned char *dd,
				unsigned char *kk)
{
	int i;
	unsigned char buffer[56];
	
	for(i=0; i<28; i++) buffer[i] = *cc++;
	for(i=28; i<56; i++) buffer[i] = *dd++;
	for(i=0; i<48; i++) *kk++ = buffer[pc_2_des[i] - 1];
}

static void setkeystar(const unsigned char *bits)
{
	int i, j;
	
	for(i=0; i<28; i++) C_des[0][i] = bits[pc_1_c_des[i] - 1];
	for(i=0; i<28; i++) D_des[0][i] = bits[pc_1_d_des[i] - 1];
	
	for(j=0; j<16; j++)
	{
		LS(C_des[j], C_des[j+1], ls_count_des[j]);
		LS(D_des[j], D_des[j+1], ls_count_des[j]);
		son(C_des[j+1], D_des[j+1], K_des[j+1]);
	}
}

static initkeyext(const unsigned char *key)
{
	unsigned char tmp[64];
	expand0(key, tmp);
	setkeystar(tmp);
}

/*=///////////////////////////////////////////////////////////////////////=*/

static void compress0(const unsigned char *out, unsigned char *in)
{
	int times;
	int i, j;
	
	for(i=0; i<8; i++)
	{
		times = 0x80;
		in[i] = 0;
		for(j=0; j<8; j++)
		{
			in[i] += (unsigned char)((*out++) * times);
			times /= 2;
		}
	}
}

static void ip(const unsigned char *text, unsigned char *ll,
			   unsigned char *rr)
{
	int i;
	unsigned char buffer[64];
	
	expand0(text, buffer);
	
	for(i=0; i<32; i++) ll[i] = buffer[ip_tab_des[i] - 1];
	
	for(i=0; i<32; i++) rr[i] = buffer[ip_tab_des[i + 32] - 1];
}

static void _ip(unsigned char *text, const unsigned char *ll,
				const unsigned char *rr)
{
	int i;
	unsigned char tmp[64];
	
	for(i=0; i<32; i++) tmp[i] = ll[i];
	for(i=32; i<64; i++) tmp[i] = rr[i - 32];
	for(i=0; i<64; i++) text[i] = tmp[_ip_tab_des[i] - 1];
}

static void s_box(const unsigned char *aa, unsigned char *bb)
{
	int i, j, k, m;
	int y, z;
	unsigned char ss[8];
	m = 0;
	
	for(i=0; i<8; i++)
	{
		j = 6 * i;
		y = aa[j] * 2 + aa[j + 5];
		z = aa[j + 1] * 8 + aa[j + 2] * 4 + aa[j + 3] * 2 + aa[j + 4];
		
		ss[i] = SSS_des[i][y][z];
		y = 0x08;
		
		for(k=0; k<4; k++)
		{
			bb[m++] = (unsigned char)((ss[i] / y) & 1);
			y/=2;
		}
	}
}

static void F(int n, const unsigned char *ll, const unsigned char *rr,
			  unsigned char *LL, unsigned char *RR)
{
	int i;
	unsigned char buffer[64], tmp[64];
	
	for(i=0; i<48; i++) buffer[i] = rr[e_r_des[i] - 1];
	for(i=0; i<48; i++) buffer[i] = ((buffer[i] + K_des[n][i]) & 1);
	s_box(buffer, tmp);
	
	for(i=0; i<32; i++) buffer[i] = tmp[P_des[i] - 1];
	for(i=0; i<32; i++) RR[i] = ((buffer[i] + ll[i]) & 1);
	for(i=0;i<32;i++) LL[i] = rr[i];
}

static void encrypt0(const unsigned char *text, unsigned char *mtext)
{
	unsigned char ll[64], rr[64], LL[64], RR[64];
	int i, j;
	
	ip(text, ll, rr);
	for(i=1; i<17; i++)
	{
		F(i, ll, rr, LL, RR);
		for(j=0; j<32; j++)
		{
			ll[j] = LL[j];
			rr[j] = RR[j];
		}
	}
	
	_ip(RR, rr, ll);
	
	compress0(RR, mtext);
}

static void discrypt0(const unsigned char *mtext, unsigned char *text)
{
	unsigned char ll[64], rr[64], LL[64], RR[64];
	int i, j;
	
	ip(mtext, ll, rr);
	for(i=16; i>0; i--)
	{
		F(i, ll, rr, LL, RR);
		for(j=0; j<32; j++)
		{
			ll[j] = LL[j];
			rr[j] = RR[j];
		}
	}
	
	_ip(RR, rr, ll);
	
	compress0(RR, text);
}

/*=///////////////////////////////////////////////////////////////////////=*/

void des(const unsigned char *key, unsigned char *text, int len)
{
	initkeyext(key);
	
	while(len >= 8)
	{
		encrypt0(text, text);
		text += 8; len -= 8;
	}
}

void undes(const unsigned char *key, unsigned char *text, int len)
{
	initkeyext(key);
	
	while(len >= 8)
	{
		discrypt0(text, text);
		text += 8; len -= 8;
	}
}

/*=///////////////////////////////////////////////////////////////////////=*/
/* The end of this file. */

⌨️ 快捷键说明

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