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

📄 rc4.cpp

📁 优化过得RC4加密解密原代码
💻 CPP
字号:
// Rc4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Rc4.h"
#include<stdio.h>
#include<memory.h>
#include<string.h>
#include<stdlib.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define swap_byte(x,y) t = *(x); *(x) = *(y); *(y) = t
#define buf_size 1024

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

typedef struct rc4_key
{      
	unsigned char state[256];       
	unsigned char x;        
	unsigned char y;
	
} rc4_key;


int test();
CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		int datalen = 12 ,c , i;
		char key[256] = {'1','2','g'} , data[256] = {0};
		for(i=c=0; i<datalen; i++)
		{
			key[c] ^= (i|i>>4)&0xFF;
			data[i] ^= key[c];
			c ^= data[i]&7;
		}
	}
	
	return nRetCode;
}



void prepare_key(unsigned char *key_data_ptr, int key_data_len, rc4_key *key)
{
	
	int i;
	unsigned char t;
	unsigned char swapByte;
	unsigned char index1;
	unsigned char index2;
	unsigned char* state;
	short counter;
	
	
	state = &key->state[0];
	for(counter = 0; counter < 256; counter++)
	{state[counter] = counter;}
	
	
	
	key->x = 0;
	key->y = 0;
	index1 = 0;
	index2 = 0;
	
	
	
	for(counter = 0; counter < 256; counter++)
	{
		
		
		
		index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;
		
		
		
		swap_byte(&state[counter], &state[index2]);
		
		
		
		index1 = (index1 + 1) % key_data_len;
		
		
		
	}
	
	
	
}







void rc4(unsigned char *buffer_ptr, int buffer_len, rc4_key *key)
{
	

	unsigned char t;
	unsigned char x;
	unsigned char y;
	unsigned char* state;
	unsigned char xorIndex;
	short counter;
	

	x = key->x;
	y = key->y;
	

	state = &key->state[0];

	for(counter = 0; counter < buffer_len; counter++)	
	{
		
		x = (x + 1) % 256;
		y = (state[x] + y) % 256;
		swap_byte(&state[x], &state[y]);
		xorIndex = (state[x] + state[y]) % 256;
		buffer_ptr[counter] ^= state[xorIndex];
	
	}
	

	key->x = x;
	key->y = y;
	

	
}







int test()
{
	
	
	char seed[256];     //key in fact	
	char data[512];			
	char fname[512];			
	char *tn;			
	char buf[buf_size];			
	char digit[5];		
	int hex, rd,i;		
	int n;		
	rc4_key key;		
	FILE *fp,*des;
	
	
	memset(fname,0,512*sizeof(char));
	if((fp=fopen("key.txt","r"))==NULL)
		exit(1);
	
	
	
	fscanf(fp,"%s %s",data,fname);
	fclose(fp);
	
	
	if(strcmp(fname,"")==0)	
	{
		
		fprintf(stderr,"can''''t find the name of the file for encryption.\n");
		return 1;
	
	}

	n = strlen(data);

	if(n>512)	
	{

		fprintf(stderr,"can''''t tolerate key longer than 512 characters.\n");
		return 1;
	
	}
	

	if (n&1)     //n is odd number	
	{
		
		strcat(data,"0");
		n++;     //convert n to even number
			
	}
	

	n/=2;
	memset(digit,0,5*sizeof(char));
	strcpy(digit,"AA");
	

	for (i=0;i<n;i++)	
	{

		digit[2] = data[i*2];
		digit[3] = data[i*2+1];
		sscanf(digit,"%x",&hex);     //characters in the file key.txt are better to be recognizable hex numbers
		seed[i] = hex;     //only reserve the two lower hex numbers in varible ''''hex'''' 
	
	}
	
	
	prepare_key((unsigned char *)seed,n,&key);	
	if((fp=fopen(fname,"r"))==NULL)
		exit(1);
	

	tn=strdup(fname);
	strcat(fname,".en");
	if((des=fopen(fname,"w"))==NULL)
		exit(1);
	
	
	rd = fread(buf,1,buf_size,fp);
	while (rd>0)	
	{
		rc4((unsigned char *)buf,rd,&key);
		fwrite(buf,1,rd,des);
		rd = fread(buf,1,buf_size,fp);
	
	}
	
	
	
	fclose(fp);
	fclose(des);
	sprintf(fname,"rm %s",tn);
	system(fname);
	sprintf(fname,"mv %s.en %s",tn,tn);
	system(fname);
	
	
	
}



⌨️ 快捷键说明

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