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

📄 rc4.cpp

📁 一个SCSI的编程
💻 CPP
字号:
#pragma comment (linker,"/subsystem:console")
#include <stdio.h>
#include <string.h>
#include "time.h"

static unsigned char mima[256];
static unsigned char Bitmima[256*8];

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

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

void byte_to_bit(unsigned char *buf,unsigned char *buffer,int readbyte)
{
	for(int i=0;i<readbyte*8;i++)
	{
		buffer[i]=(buf[i>>3]>>(i&7))&1;
	}
}

void bit_to_byte(unsigned char *buf,unsigned char *buffer,int readbyte)
{
	for(int i=0;i<readbyte*8;i+=8)
	{
        buf[i>>3] = buffer[i]+buffer[i+1]*2+buffer[i+2]*4+buffer[i+3]*8+buffer[i+4]*16+buffer[i+5]*32+buffer[i+6]*64+buffer[i+7]*128;
	}
}

void exchage (unsigned char *buf,unsigned char *buffer,int readbyte)
{
	for(int i=0;i<readbyte*8;i++)
	{
    	buf[i] = buffer[i];
	}
}

void XOR (unsigned char *buffer,int readbyte)
{
	for(int i=0;i<readbyte*8;i++)
	{
		buffer[i]^=Bitmima[i];
	}
}

void prepare_key(unsigned char *key_data_ptr,rc4_key *key)
{
	  unsigned char t;
	  unsigned char index1;
	  unsigned char index2;
	  unsigned char* state;
	  int 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) % 128;
	  }
}

void rc4(int buffer_len, rc4_key *key)
{
	  unsigned char t;
	  int x=0;
	  int y=0;
	  unsigned char* state;
	  unsigned char xorIndex;
	  int counter;
	  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;
		mima[counter]=state[xorIndex];
	  }
}

void Encrypt (unsigned char *seed1,int readbyte,unsigned char *buf)
{
	rc4_key key;
	unsigned char buffer[256*8];
	prepare_key(seed1,&key);
	rc4(readbyte,&key);
	memset(buffer, 0, sizeof(buffer));
	byte_to_bit(buf,buffer,readbyte);
	byte_to_bit(mima,Bitmima,readbyte);
	XOR (buffer,readbyte);
	bit_to_byte(buf,buffer,readbyte);
}

void Decrypt (unsigned char *seed1,int readbyte,unsigned char *buf)
{
	rc4_key key;
	unsigned char buffer[256*8];
	prepare_key(seed1,&key);
	rc4(readbyte,&key);
	memset(buffer, 0, sizeof(buffer));
	byte_to_bit(buf,buffer,readbyte);
	byte_to_bit(mima,Bitmima,readbyte);
	XOR (buffer,readbyte);
	bit_to_byte(buf,buffer,readbyte);
}
#if 0
void main(void)
{
	  unsigned char seed1[128]="abcdefghijklmopqrstuywxyz1234567890";
//	  unsigned char password[104];
//	  unsigned char IV[24];
	  clock_t start,end;
      double elapsed;
	  int readbyte;
//    memset(seed1, 5, sizeof(seed1));
  	  unsigned char buf[256];	
	  FILE *fd1, *fd2, *fd3,*fd4;
	  fd1=fopen("h:\\txt\\5.wma","rb");
	  fd2=fopen("h:\\txt\\5m.wma","wb");
	  fd4=fopen("h:\\txt\\5fm.wma","wb");
	  memset(mima, 0, sizeof(mima));
	  memset(buf, 0, sizeof(buf));
	  start = clock();
  		while(!feof(fd1))
		{	
			memset(buf, 0, sizeof(buf));
    		readbyte=fread(buf,sizeof(char),256,fd1);
			Encryption (seed1,readbyte,buf);
			fwrite(buf,sizeof(char),readbyte,fd2);
		}
		fclose(fd2);
		fd3=fopen("h:\\txt\\5m.wma","rb");
		while(!feof(fd3))
		{	
			memset(buf, 0, sizeof(buf));
    		readbyte=fread(buf,sizeof(char),256,fd3);
			Decryption (seed1,readbyte,buf);
			fwrite(buf,sizeof(char),readbyte,fd4);
		}
	end = clock();
	elapsed = ((double)(end-start))/CLOCKS_PER_SEC;
	fclose(fd1);
	fclose(fd3);
	fclose(fd4);
	printf("%fOK!",elapsed);
}
#endif

⌨️ 快捷键说明

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