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

📄 enc_des.c

📁 经典的unix下telnetd代码
💻 C
📖 第 1 页 / 共 2 页
字号:
}int fb64_reply(unsigned char *data, int cnt, struct fb *fbp){	int state = fbp->state[DIR_ENCRYPT-1];	if (cnt-- < 1)		goto failure;	switch (*data++) {	case FB64_IV_OK:		fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);		if (state == FAILED)			state = IN_PROGRESS;		state &= ~NO_RECV_IV;		encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);		break;	case FB64_IV_BAD:		memset(fbp->temp_feed, 0, sizeof(des_cblock));		fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);		state = FAILED;		break;	default:		if (encrypt_debug_mode) {			printf("Unknown option type: %d\r\n", data[-1]);			printd(data, cnt);			printf("\r\n");		}		/* FALL THROUGH */	failure:		state = FAILED;		break;	}	return(fbp->state[DIR_ENCRYPT-1] = state);}void cfb64_session(Session_Key *key, int server){	fb64_session(key, server, &fb[CFB]);}void ofb64_session(Session_Key *key, int server){	fb64_session(key, server, &fb[OFB]);}static void fb64_session(Session_Key *key, int server, struct fb *fbp){	if (!key || key->type != SK_DES) {		if (encrypt_debug_mode)			printf("Can't set krbdes's session key (%d != %d)\r\n",				key ? key->type : -1, SK_DES);		return;	}	memcpy(fbp->krbdes_key, key->data, sizeof(des_cblock));	fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);	fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);	if (fbp->once == 0) {#ifndef OLD_DES_RANDOM_KEY		des_init_random_number_generator(&fbp->krbdes_key);#endif		fbp->once = 1;	}	des_key_sched(&fbp->krbdes_key, fbp->krbdes_sched);	/*	 * Now look to see if krbdes_start() was was waiting for	 * the key to show up.  If so, go ahead an call it now	 * that we have the key.	 */	if (fbp->need_start) {		fbp->need_start = 0;		fb64_start(fbp, DIR_ENCRYPT, server);	}}/* * We only accept a keyid of 0.  If we get a keyid of * 0, then mark the state as SUCCESS. */int cfb64_keyid(int dir, unsigned char *kp, int *lenp){	return(fb64_keyid(dir, kp, lenp, &fb[CFB]));}int ofb64_keyid(int dir, unsigned char *kp, int *lenp){	return(fb64_keyid(dir, kp, lenp, &fb[OFB]));}int fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp){	int state = fbp->state[dir-1];	if (*lenp != 1 || (*kp != '\0')) {		*lenp = 0;		return(state);	}	if (state == FAILED)		state = IN_PROGRESS;	state &= ~NO_KEYID;	return(fbp->state[dir-1] = state);}void fb64_printsub(unsigned char *data, int cnt, 		   unsigned char *buf, int buflen, char *type){	char lbuf[32];	int i;	char *cp;	buf[buflen-1] = '\0';		/* make sure it's NULL terminated */	buflen -= 1;	switch(data[2]) {	case FB64_IV:		snprintf(lbuf, sizeof(lbuf), "%s_IV", type);		cp = lbuf;		goto common;	case FB64_IV_OK:		snprintf(lbuf, sizeof(lbuf), "%s_IV_OK", type);		cp = lbuf;		goto common;	case FB64_IV_BAD:		snprintf(lbuf, sizeof(lbuf), "%s_IV_BAD", type);		cp = lbuf;		goto common;	default:		snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[2]);		cp = lbuf;	common:		for (; (buflen > 0) && (*buf = *cp++); buf++)			buflen--;		for (i = 3; i < cnt; i++) {			snprintf(lbuf, sizeof(lbuf), " %d", data[i]);			for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)				buflen--;		}		break;	}}void cfb64_printsub(unsigned char *data, int cnt, 		    unsigned char *buf, int buflen){	fb64_printsub(data, cnt, buf, buflen, "CFB64");}void ofb64_printsub(unsigned char *data, int cnt,		    unsigned char *buf, int buflen){	fb64_printsub(data, cnt, buf, buflen, "OFB64");}void fb64_stream_iv(des_cblock seed, struct stinfo *stp){	memcpy(stp->str_iv, seed,sizeof(des_cblock));	memcpy(stp->str_output, seed, sizeof(des_cblock));	des_key_sched(&stp->str_ikey, stp->str_sched);	stp->str_index = sizeof(des_cblock);}void fb64_stream_key(des_cblock key, struct stinfo *stp){	memcpy(stp->str_ikey, key, sizeof(des_cblock));	des_key_sched((des_cblock*)key, stp->str_sched);	memcpy(stp->str_output, stp->str_iv, sizeof(des_cblock));	stp->str_index = sizeof(des_cblock);}/* * DES 64 bit Cipher Feedback * *     key --->+-----+ *          +->| DES |--+ *          |  +-----+  | *	    |           v *  INPUT --(--------->(+)+---> DATA *          |             | *	    +-------------+ *          * * Given: *	iV: Initial vector, 64 bits (8 bytes) long. *	Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt). *	On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output. * *	V0 = DES(iV, key) *	On = Dn ^ Vn *	V(n+1) = DES(On, key) */void cfb64_encrypt(unsigned char *s, int c){	struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];	int index;	index = stp->str_index;	while (c-- > 0) {		if (index == sizeof(des_cblock)) {			des_cblock b;			des_ecb_encrypt(&stp->str_output, &b,stp->str_sched, 1);			memcpy(stp->str_feed, b, sizeof(des_cblock));			index = 0;		}		/* On encryption, we store (feed ^ data) which is cypher */		*s = stp->str_output[index] = (stp->str_feed[index] ^ *s);		s++;		index++;	}	stp->str_index = index;}int cfb64_decrypt(int data){	struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];	int index;	if (data == -1) {		/*		 * Back up one byte.  It is assumed that we will		 * never back up more than one byte.  If we do, this		 * may or may not work.		 */		if (stp->str_index)			--stp->str_index;		return(0);	}	index = stp->str_index++;	if (index == sizeof(des_cblock)) {		des_cblock b;		des_ecb_encrypt(&stp->str_output,&b, stp->str_sched, 1);		memcpy(stp->str_feed, b, sizeof(des_cblock));		stp->str_index = 1;	/* Next time will be 1 */		index = 0;		/* But now use 0 */ 	}	/* On decryption we store (data) which is cypher. */	stp->str_output[index] = data;	return(data ^ stp->str_feed[index]);}/* * DES 64 bit Output Feedback * * key --->+-----+ *	+->| DES |--+ *	|  +-----+  | *	+-----------+ *	            v *  INPUT -------->(+) ----> DATA * * Given: *	iV: Initial vector, 64 bits (8 bytes) long. *	Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt). *	On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output. * *	V0 = DES(iV, key) *	V(n+1) = DES(Vn, key) *	On = Dn ^ Vn */void ofb64_encrypt(unsigned char *s, int c){	struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];	int index;	index = stp->str_index;	while (c-- > 0) {		if (index == sizeof(des_cblock)) {			des_cblock b;			des_ecb_encrypt(&stp->str_feed,&b, stp->str_sched, 1);			memcpy(stp->str_feed, b, sizeof(des_cblock));			index = 0;		}		*s++ ^= stp->str_feed[index];		index++;	}	stp->str_index = index;}int ofb64_decrypt(int data){	struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];	int index;	if (data == -1) {		/*		 * Back up one byte.  It is assumed that we will		 * never back up more than one byte.  If we do, this		 * may or may not work.		 */		if (stp->str_index)			--stp->str_index;		return(0);	}	index = stp->str_index++;	if (index == sizeof(des_cblock)) {		des_cblock b;		des_ecb_encrypt(&stp->str_feed,&b,stp->str_sched, 1);		memcpy(stp->str_feed, b, sizeof(des_cblock));		stp->str_index = 1;	/* Next time will be 1 */		index = 0;		/* But now use 0 */ 	}	return(data ^ stp->str_feed[index]);}#endif

⌨️ 快捷键说明

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