一种简单的table异或加密算法



一种简单的table异或加密算法

const char* str =  "AA479BC8-02FB-4B64-BE5C-6105F0789668";//key

void SetTable(byte* dst, int len)
{
	memset(dst, 0, len);

	int i = 0;
	while (i<len)
	{
		dst[i] = i;
		i++;
	}

	byte v3 = 0;
	int index = 0;
	int a2 = strlen(str);
	byte v6;

	//置换
	do
	{
		v3 = (v3 + *(byte *)(index % a2 + str) + *(byte *)(index + dst)) % 256;
		v6 = *(byte*)(index + dst);
		*(byte *)(index++ + dst) = *(byte *)(v3 + dst);
		*(byte *)(v3 + dst) = v6;
	}
	while ( index < 256 );

	return ;
}

void decrypt_bytes(char* src, int src_len, char * dst, int dst_len)
{

	if (dst_len<src_len)
	{
		return;
	}

	byte* table = new byte[0x100];
	if (!table)
	{
		return ;
	}

	SetTable(table, 0x100);

	int bytes_len = src_len;

	int index = 0;
	byte* table_p;
	int base = 0;
	int i = 0;
	byte byte_t;

	do
	{
		//表置换
		index = (index + 1) % 256;
		table_p = (table + (base + table[index]) % 256);
		base = (base + table[index]) % 256;
		byte_t = table[index];
		table[index] = *table_p;
		*table_p = byte_t;

		//XOR加密
		dst[i] = src[i] ^ *((byte *)table + (table[index]+byte_t) % 256);
		i++;
		--bytes_len;
	}
	while ( bytes_len );

	delete[] table;

	return ;
}

bool crypt_file(const char* file_path)
{
	HANDLE hFile = CreateFileA(file_path, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return false;
	}
	DWORD file_size = GetFileSize(hFile, 0);

	char* file_bytes = new char[file_size];
	if (!file_bytes)
	{
		CloseHandle(hFile);
		return false;
	}

	DWORD bytes_read;
	if(!ReadFile(hFile, file_bytes, file_size, &bytes_read, 0))
	{
		delete[] file_bytes;
		CloseHandle(hFile);
		return false;
	}

	char * debuff = new char[file_size+1];
	decrypt_bytes(file_bytes, file_size, debuff, file_size+1);

	char file_out[100] = "";
	strcat_s(file_out, file_path);
	strcat_s(file_out, ".out.txt");

	HANDLE hFile2 = CreateFileA(file_out, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return false;
	}

	WriteFile(hFile2, debuff, file_size, &bytes_read, 0);

	CloseHandle(hFile);
	CloseHandle(hFile2);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值