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

📄 rc4.h

📁 U盘数据加密程序 用C语言编写的 非常经典的哟
💻 H
字号:
/*************************************************************
 *
 * Module:
 *
 *        RC4.H
 *
 *
 * Histroy:
 * 
 *        Dec 31,2005,Nanjing
 *
 */
#ifndef _INCLUDE_RC4_H_FILE_
#define _INCLUDE_RC4_H_FILE_


typedef struct _RC4_KEY {
	
	unsigned char  state[256];
	unsigned char  x;
	unsigned char  y;
	
}  RC4_KEY,
*LPRC4_KEY;

/**************************************************************
 * rc4_swap_byte
 *
 */
__inline static void __stdcall rc4_swap_byte(unsigned char *a,unsigned char *b){

	unsigned char   c   = *a;
	*a  = *b;
	*b  = c;
	return ;
}

/**************************************************************
 * build_rc4_key
 *
 */
__inline void __stdcall build_rc4_key(unsigned char *key_data,int key_len,RC4_KEY *aKey){

	unsigned char	index1;
	unsigned char	index2;
	unsigned char*	state;
	short			counter;     
	
	state = &aKey->state[0];         
	for(counter = 0; counter < 256; counter++){       
		state[counter] = (unsigned char)counter;               
	}
	aKey->x = 0;
	aKey->y = 0;
	index1  = 0;     
	index2  = 0;             
	for(counter = 0; counter < 256; counter++){               
		
		index2 = (key_data[index1] + state[counter] + index2) % 256;                
		rc4_swap_byte(&state[counter], &state[index2]);            
		index1 = (index1 + 1) % key_len;  
	} 

	return ;
}

/**************************************************************
 * rc4_handler
 *
 */
__inline void __stdcall rc4_handler(unsigned char *buffer_ptr, int buffer_len, RC4_KEY *aKey){ 
	
	unsigned char	x;
	unsigned char	y;
	unsigned char*	state;
	unsigned char	xorIndex;
	short			counter;
	
	x     = aKey->x;     
	y     = aKey->y;     
	state = &aKey->state[0];         
	for(counter = 0; counter < buffer_len; counter ++){               
		
		x = (x + 1) % 256;
		y = (state[x] + y) % 256; 

		rc4_swap_byte(&state[x], &state[y]);
		
		xorIndex			 = (state[x] + state[y]) % 256;
		buffer_ptr[counter] ^= state[xorIndex];         
	}

	aKey->x = x;     
	aKey->y = y;

	return ;
}

#endif	//_INCLUDE_RC4_H_FILE_

⌨️ 快捷键说明

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