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

📄 des.c

📁 DES encryption source, for chaining and block crypt
💻 C
字号:
#include <string.h>#include "crypto.h"const char *CRYPTO_version="DES version 1.02";
unsigned char secaPC1[] = {
	42,57,29,34,  41,53,30,15,
	19,36,23,14,  43,61,12, 3,
	51,49,5,  6,  45,54,52,47,
	63,38,58,22,  60,33,10,26,
	37,35,44, 1,  20,62,28,18,
	46, 9,39, 4,  27,11,21,50,
	31,25, 2, 7,  13,55,59,17
};

unsigned char secaPC2[] = {
	18, 3,21,15,  42,35,37, 8,
	49,41,30,55,  56,29,12,23,
	43,14,7 ,27,  13, 2,11,45,
	 4,34,54,51,  22,40,16,25,
	26,48,53,28,   1,17, 5,31,
	50, 6,39,24,  33,47,38,32
};

static unsigned char _PC1[] = {   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  };static unsigned char _PC2[] = {  14,17,11,24, 1, 5,   3,28,15, 6,21,10,  23,19,12, 4,26, 8,  16, 7,27,20,13, 2,  41,52,31,37,47,55,  30,40,51,45,33,48,  44,49,39,56,34,53,  46,42,50,36,29,32  };static const unsigned char E[] = {	0x00,0x1F,0x1E,0x1D,0x1C,0x1B,0x1C,0x1B,0x1A,0x19,0x18,0x17,0x18,0x17,0x16,0x15,	0x14,0x13,0x14,0x13,0x12,0x11,0x10,0x0F,0x10,0x0F,0x0E,0x0D,0x0C,0x0B,0x0C,0x0B,	0x0A,0x09,0x08,0x07,0x08,0x07,0x06,0x05,0x04,0x03,0x04,0x03,0x02,0x01,0x00,0x1F };static const unsigned char P[] = {	0x07,0x1C,0x15,0x0A,0x1A,0x02,0x13,0x0D,0x17,0x1D,0x05,0x00,0x12,0x08,0x18,0x1E,	0x16,0x01,0x0E,0x1B,0x06,0x09,0x11,0x1F,0x0F,0x04,0x14,0x03,0x0B,0x0C,0x19,0x10};static const unsigned char IP[] = {	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};static const unsigned char FP[] = {	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};static const unsigned char S[][64] = {  { 0xe,0x0,0x4,0xf,0xd,0x7,0x1,0x4,  0x2,0xe,0xf,0x2,0xb,0xd,0x8,0x1,    0x3,0xa,0xa,0x6,0x6,0xc,0xc,0xb,  0x5,0x9,0x9,0x5,0x0,0x3,0x7,0x8,    0x4,0xf,0x1,0xc,0xe,0x8,0x8,0x2,  0xd,0x4,0x6,0x9,0x2,0x1,0xb,0x7,    0xf,0x5,0xc,0xb,0x9,0x3,0x7,0xe,  0x3,0xa,0xa,0x0,0x5,0x6,0x0,0xd  },  { 0xf,0x3,0x1,0xd,0x8,0x4,0xe,0x7,  0x6,0xf,0xb,0x2,0x3,0x8,0x4,0xe,    0x9,0xc,0x7,0x0,0x2,0x1,0xd,0xa,  0xc,0x6,0x0,0x9,0x5,0xb,0xa,0x5,    0x0,0xd,0xe,0x8,0x7,0xa,0xb,0x1,  0xa,0x3,0x4,0xf,0xd,0x4,0x1,0x2,    0x5,0xb,0x8,0x6,0xc,0x7,0x6,0xc,  0x9,0x0,0x3,0x5,0x2,0xe,0xf,0x9  },  { 0xa,0xd,0x0,0x7,0x9,0x0,0xe,0x9,  0x6,0x3,0x3,0x4,0xf,0x6,0x5,0xa,    0x1,0x2,0xd,0x8,0xc,0x5,0x7,0xe,  0xb,0xc,0x4,0xb,0x2,0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c,0x3,0x9,0x5,0x7,0xc,  0x5,0x2,0xa,0xf,0x6,0x8,0x1,0x6,    0x1,0x6,0x4,0xb,0xb,0xd,0xd,0x8,  0xc,0x1,0x3,0x4,0x7,0xa,0xe,0x7,    0xa,0x9,0xf,0x5,0x6,0x0,0x8,0xf,  0x0,0xe,0x5,0x2,0x9,0x3,0x2,0xc  },  { 0xd,0x1,0x2,0xf,0x8,0xd,0x4,0x8,  0x6,0xa,0xf,0x3,0xb,0x7,0x1,0x4,    0xa,0xc,0x9,0x5,0x3,0x6,0xe,0xb,  0x5,0x0,0x0,0xe,0xc,0x9,0x7,0x2,    0x7,0x2,0xb,0x1,0x4,0xe,0x1,0x7,  0x9,0x4,0xc,0xa,0xe,0x8,0x2,0xd,    0x0,0xf,0x6,0xc,0xa,0x9,0xd,0x0,  0xf,0x3,0x3,0x5,0x5,0x6,0x8,0xb  } };static const unsigned char LS[] = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
#define DES_ITER 16#define get_unaligned(ptr) (*(ptr))#define put_unaligned(val, ptr) ((void)( *(ptr) = (val) ))#define rol28(V,n) ((V<<(n) ^ V>>(28-(n)))&0xfffffffL)#define ror28(V,n) ((V>>(n) ^ V<<(28-(n)))&0xfffffffL)#define shiftin(V,R,n) ((V<<1)+(((R)>>(n))&1))#define hash(T) ((T&0x0000ffffL) | ((T>>8)&0x00ff0000L) | ((T<<8)&0xff000000L))#define DESHASH(T) { if(mode&DES_HASH) T = hash(T); }#define DESROUND(C,D,T) { \	unsigned int s=0; \	int j,t; \	for(j=7, k=0; j>=0; j--) \	{ \		unsigned int v=0, K=0; \		for(t=5; t>=0; t--, k++) \		{ \			v=shiftin(v,T,E[k]); \			if(PC2[k]<29) K=shiftin(K,C,28-PC2[k]); \			else          K=shiftin(K,D,56-PC2[k]); \		} \		s=(s<<4) + S[7-j][v^K]; \	} \	T=0; \	for(j=31; j>=0; j--) T=shiftin(T,s,P[j]); \}unsigned int cDes_Mod (unsigned int R, unsigned int key7){	unsigned int key5, al;  if (key7!=0)	{    key5 = (R>>24)&0xff;    al = key7*key5 + key7 + key5;    al = (al&0xff) - ((al>>8)&0xff);    if (al&0x100) al++;    R = (R&0x00ffffffL) + (al<<24);    }  return R;}void cDes_Permute (unsigned char *data, const unsigned char *P, int n){	unsigned char pin[8];	int i,j,k;	for (i=0, k=0; k<n; i++)	{		int p=0;		for (j=7; j>=0; j--,k++)		{			const int t = P[k] - 1;			p = shiftin (p, data[t>>3], 7-(t&7));			}		pin[i] = p;		}	memcpy(data,pin,8);}
#define bswap_32(x)	((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |(((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))void cDes_Des (unsigned char *data, const unsigned char *key, int mode){	int i, k;	unsigned int C,D,L,R,T;	unsigned char mkey[8];	unsigned char PC1[56], PC2[48];	if (mode&SECA_PC) {		memcpy (PC1, secaPC1, 56);		memcpy (PC2, secaPC2, 48);	} else {		memcpy (PC1, _PC1, 56);		memcpy (PC2, _PC2, 48);	}	if (mode&DES_PC1) {		memcpy (mkey, key, sizeof(mkey));		cDes_Permute (mkey, PC1, 56);		key=mkey;	}	if (mode&DES_IP) cDes_Permute (data,IP,64);	// split OpKey on 2 half	C = (unsigned long) key[0] << 20 ^ (unsigned long) key[1] << 12 ^ (unsigned long) key[2] <<  4 ^ (unsigned long) (key[3] >> 4);	D =	(unsigned long) (key[3] & 0x0f) << 24 ^ (unsigned long) key[4] << 16 ^ (unsigned long) key[5] << 8 ^ (unsigned long) key[6];	L =	(unsigned long) data[0] << 24 ^ (unsigned long) data[1] << 16 ^ (unsigned long) data[2] << 8 ^ (unsigned long) data[3];	R =	(unsigned long) data[4] << 24 ^ (unsigned long) data[5] << 16 ^ (unsigned long) data[6] << 8 ^ (unsigned long) data[7];	if (!(mode&DES_RIGHT))	{		for(i=DES_ITER-1; i>=0; i--)		{			C = rol28(C,LS[15-i]); 			D = rol28(D,LS[15-i]); T=R;			if(mode&DES_MOD) T=cDes_Mod(T,key[7]);			DESROUND(C,D,T);			DESHASH(T);			T^=L; L=R; R=T;		}	}	else {		for(i=15; i>=0; i--)		{			T=R;			if(mode&DES_MOD) T=cDes_Mod(T,key[7]);			DESROUND(C,D,T);			DESHASH(T);			T^=L; L=R; R=T; 			C = ror28(C,LS[i]); 			D = ror28(D,LS[i]);		}	}  put_unaligned(bswap_32(R),(unsigned int *)(data  ));  put_unaligned(bswap_32(L),(unsigned int *)(data+4));  if(mode&DES_FP) cDes_Permute(data,FP,64);}void cDes_3DES(unsigned char *data, unsigned char *key){	cDes_Des (data, key,   NAGRA_DES_DECR);	cDes_Des (data, key+8, NAGRA_DES_ENCR);	cDes_Des (data, key,   NAGRA_DES_DECR);	return;}

⌨️ 快捷键说明

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