一种简单的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);
}