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

📄 decrypt_3.cpp

📁 一种非线性流式加密算法,对称加密
💻 CPP
字号:
#pragma comment (linker,"/subsystem:console")
#include <stdio.h>
#include <string.h>
#include <time.h>

//定义用作交换的宏
#define SwapByte(x,y) t = *(x); *(x) = *(y); *(y) = t

//mima数组用于以byte形式存放加密序列,Bitmima数组用于以bit流形式存放加密序列
static unsigned char mima[256];
static unsigned char Bitmima[256*8];

//状态盒
typedef struct cKey
{      
	unsigned char state[256];       
	unsigned char x;        
	unsigned char y;
}cKey;


void BytetoBit(unsigned char *buf,unsigned char *buffer,int readbyte)
{
	//逐位获得二进制码,每字节的二进制高bit位存储在数组高位中
	for( int i=0; i<readbyte*8; i++)
		buffer[i] = ( buf[i>>3] >> ( i & 7 ) ) & 1;
}

void BittoByte(unsigned char *buf,unsigned char *buffer,int readbyte)
{
	//从bit流数组中取每8位转化为byte
	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 DoXor (unsigned char *buffer,int readbyte)
{
	//把buffer的内容与Bitmima中的内容异或,并存放到buffer中
	for( int i=0; i<readbyte*8; i++)
		buffer[i] ^= Bitmima[i];
}

void PrepareKey(unsigned char *key_data_ptr,cKey *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;
		SwapByte(&state[counter], &state[index2]);
		index1 = ( index1 + 1 ) % 128;
	}
}

void CreateStream(int buffer_len, cKey *key)
{
	unsigned char t;
	int x = 0;
	int y = 0;
	unsigned char* state = &key->state[0];
	unsigned char xorIndex;
	int counter;

	//生成加密序列
	for( counter = 0; counter < buffer_len; counter++ )
	{
		x = ( x + 1 ) % 256;
		y = ( state[x] + y ) % 256;

		SwapByte(&state[x], &state[y]);
		xorIndex = ( state[x] + state[y] ) % 256;
		mima[counter] = state[xorIndex];
	}
}

void Decrypt (unsigned char *seed1,int readbyte,unsigned char *buf)
{
	cKey key;
	unsigned char buffer[256*8];

	//生成加密序列
	PrepareKey(seed1,&key);
	CreateStream(readbyte,&key);

	memset(buffer, 0, sizeof(buffer));
	BytetoBit(buf,buffer,readbyte);
	BytetoBit(mima,Bitmima,readbyte);
	
	//解密密文
	DoXor (buffer,readbyte);

	BittoByte(buf,buffer,readbyte);
}
void main(void)
{
	int readbyte;
	unsigned char buf[256];	
	unsigned char seed1[128];
	FILE *fd1, *fd2;

	memset(seed1, 1, sizeof(seed1));
	memset(mima, 0, sizeof(mima));
	memset(buf, 0, sizeof(buf));

	fd1=fopen(".\\5m.wma","rb");
	fd2=fopen(".\\5fm.wma","wb");

	while(!feof(fd1))
	{	
		memset(buf, 0, sizeof(buf));
		readbyte=fread(buf,sizeof(char),256,fd1);
		Decrypt (seed1,readbyte,buf);
		fwrite(buf,sizeof(char),readbyte,fd2);
	}

	fclose(fd2);
	fclose(fd1);
}

⌨️ 快捷键说明

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