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

📄 tripledes.c

📁 一个用于智能手机的多媒体库适合S60 WinCE的跨平台开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
	right ^= f(key, 1, left, knp);	knp -= 8;	left ^= f(key, 1, right, knp);	knp -= 8;	right ^= f(key, 1, left, knp);	knp -= 8;	left ^= f(key, 1, right, knp);	knp -= 8;	right ^= f(key, 1, left, knp);	knp -= 8;	left ^= f(key, 1, right, knp);	knp -= 8;	right ^= f(key, 1, left, knp);	knp -= 8;	left ^= f(key, 1, right, knp);	/* Do the 16 rounds.	 * The rounds are numbered from 0 to 15. On even rounds	 * the right half is fed to f() and the result exclusive-ORs	 * the left half; on odd rounds the reverse is done.	 */	knp = &key->kn[2][0][0];	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	knp += 8;	left ^= f(key, 2, right, knp);	knp += 8;	right ^= f(key, 2, left, knp);	/* Left/right half swap, plus byte swap if little-endian */#ifndef	WORDS_BIGENDIAN	work[1] = byteswap32(left);	work[0] = byteswap32(right);#else	work[0] = right;	work[1] = left;#endif	permute((char *) work, key->fperm, block);	/* Inverse initial permutation */}/* In-place decryption of 64-bit block. This function is the mirror * image of encryption; exactly the same steps are taken, but in * reverse order */static void _mcrypt_decrypt(TRIPLEDES_KEY * key, char *block){	register u32 left, right;	register char *knp;	u32 work[2];		/* Working data storage */	permute(block, key->iperm, (char *) work);	/* Initial permutation */	/* Left/right half swap, plus byte swap if little-endian */#ifndef	WORDS_BIGENDIAN	right = byteswap32(work[0]);	left = byteswap32(work[1]);#else	right = work[0];	left = work[1];#endif/* DES 3 */	/* Do the 16 rounds in reverse order.	 * The rounds are numbered from 15 to 0. On even rounds	 * the right half is fed to f() and the result exclusive-ORs	 * the left half; on odd rounds the reverse is done.	 */	knp = &key->kn[2][15][0];	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);	knp -= 8;	right ^= f(key, 2, left, knp);	knp -= 8;	left ^= f(key, 2, right, knp);/* DES 2*/	/* Do the 16 rounds.	 * The rounds are numbered from 0 to 15. On even rounds	 * the right half is fed to f() and the result exclusive-ORs	 * the left half; on odd rounds the reverse is done.	 */	knp = &key->kn[1][0][0];	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);	knp += 8;	left ^= f(key, 1, right, knp);	knp += 8;	right ^= f(key, 1, left, knp);/* DES 1 */	/* Do the 16 rounds in reverse order.	 * The rounds are numbered from 15 to 0. On even rounds	 * the right half is fed to f() and the result exclusive-ORs	 * the left half; on odd rounds the reverse is done.	 */	knp = &key->kn[0][15][0];	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);	knp -= 8;	right ^= f(key, 0, left, knp);	knp -= 8;	left ^= f(key, 0, right, knp);#ifndef	WORDS_BIGENDIAN	work[0] = byteswap32(left);	work[1] = byteswap32(right);#else	work[0] = left;	work[1] = right;#endif	permute((char *) work, key->fperm, block);	/* Inverse initial permutation */}/* Permute inblock with perm */static void permute(char *inblock, char perm[16][16][8], char *outblock){	register char *ib, *ob;	/* ptr to input or output block */	register char *p, *q;	register int j;	if (perm == NULL) {		/* No permutation, just copy */		memmove(outblock, inblock, 8);		return;	}	/* Clear output block */	Bzero(outblock, 8);	ib = inblock;	for (j = 0; j < 16; j += 2, ib++) {	/* for each input nibble */		ob = outblock;		p = perm[j][(*ib >> 4) & 0xf];		q = perm[j + 1][*ib & 0xf];		/* and each output byte, OR the masks together */		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;		*ob++ |= *p++ | *q++;	}}/* The nonlinear function f(r,k), the heart of DES */static u32 f(TRIPLEDES_KEY * key, int pos, register u32 r,		register char *subkey){	register u32 *spp;	register u32 rval, rt;	register int er;#ifdef	TRACE	printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",	       r,	       subkey[0], subkey[1], subkey[2],	       subkey[3], subkey[4], subkey[5], subkey[6], subkey[7]);#endif	/* Run E(R) ^ K through the combined S & P boxes.	 * This code takes advantage of a convenient regularity in	 * E, namely that each group of 6 bits in E(R) feeding	 * a single S-box is a contiguous segment of R.	 */	subkey += 7;	/* Compute E(R) for each block of 6 bits, and run thru boxes */	er = ((int) r << 1) | ((r & 0x80000000) ? 1 : 0);	spp = &key->sp[pos][7][0];	rval = spp[(er ^ *subkey--) & 0x3f];	spp -= 64;	rt = (u32) r >> 3;	rval |= spp[((int) rt ^ *subkey--) & 0x3f];	spp -= 64;	rt >>= 4;	rval |= spp[((int) rt ^ *subkey--) & 0x3f];	spp -= 64;	rt >>= 4;	rval |= spp[((int) rt ^ *subkey--) & 0x3f];	spp -= 64;	rt >>= 4;	rval |= spp[((int) rt ^ *subkey--) & 0x3f];	spp -= 64;	rt >>= 4;	rval |= spp[((int) rt ^ *subkey--) & 0x3f];	spp -= 64;	rt >>= 4;	rval |= spp[((int) rt ^ *subkey--) & 0x3f];	spp -= 64;	rt >>= 4;	rt |= (r & 1) << 5;	rval |= spp[((int) rt ^ *subkey) & 0x3f];#ifdef	TRACE	printf(" %08lx\n", rval);#endif	return rval;}/* initialize a perm array */static void perminit(char perm[16][16][8], char p[64]){	register int l, j, k;	int i, m;	/* Clear the permutation array */	Bzero((char *) perm, 16 * 16 * 8);	for (i = 0; i < 16; i++)	/* each input nibble position */		for (j = 0; j < 16; j++)	/* each possible input nibble */			for (k = 0; k < 64; k++) {	/* each output bit position */				l = p[k] - 1;	/* where does this bit come from */				if ((l >> 2) != i)	/* does it come from input posn? */					continue;	/* if not, bit k is 0    */				if (!(j & nibblebit[l & 3]))					continue;	/* any such bit in input? */				m = k & 07;	/* which bit is this in the byte */				perm[i][j][k >> 3] |= bytebit[m];			}}/* Initialize the lookup table for the combined S and P boxes */static void spinit(TRIPLEDES_KEY * key, int pos){	char pbox[32];	int p, i, s, j, rowcol;	u32 val;	/* Compute pbox, the inverse of p32i.	 * This is easier to work with	 */	for (p = 0; p < 32; p++) {		for (i = 0; i < 32; i++) {			if (p32i[i] - 1 == p) {				pbox[p] = i;				break;			}		}	}	for (s = 0; s < 8; s++) {	/* For each S-box */		for (i = 0; i < 64; i++) {	/* For each possible input */			val = 0;			/* The row number is formed from the first and last			 * bits; the column number is from the middle 4			 */			rowcol =			    (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) &							     0xf);			for (j = 0; j < 4; j++) {	/* For each output bit */				if (si[s][rowcol] & (8 >> j)) {					val |=					    1L << (31 - pbox[4 * s + j]);				}			}			key->sp[pos][s][i] = val;		}	}}void gf_crypt_register_3des(GF_Crypt *td){	td->a_encrypt = _mcrypt_encrypt;	td->a_decrypt = _mcrypt_decrypt;	td->a_set_key = _mcrypt_set_key;	td->algo_name = "3DES";	td->algo_version = 19991129;	td->num_key_sizes = 1;	td->key_sizes[0] = 24; 	td->key_size = 24;	td->is_block_algo = 1;	td->algo_block_size = 8;	td->algo_size = sizeof(TRIPLEDES_KEY);}#endif

⌨️ 快捷键说明

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