我正在尝试使用 XOR 加密/解密文件。我有以下加密/解密例程,其中每个字节都被异或,结果减去位于前一个位置的字节的值。ASM 表示如下
crypt:
mov dl, [eax+ecx] ; read byte
xor dl, 0C5h ; xor it with oxC5
sub dl, [eax+ecx-1] ; sub the previous byte
mov [eax+ecx], dl ; save the new byte
dec eax ; decrement pointer
test eax, eax
jg short crypt ;
这就是我的加密例程应该是什么样子,我正在尝试将此 C/C++ 移植。我的代码如下
#include <stdio.h>
unsigned int xorkey = 0xC5;
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("usage: encoder input output\n");
return -1;
}
FILE *in = fopen(argv[1], "rb");
if(in == NULL)
{
printf("failed to open: %s", argv[2]);
return -1;
}
FILE *out = fopen(argv[2], "wb");
if(out == NULL)
{
fclose(in);
printf("failed to open '%s' for writing.",argv[2]);
return -1;
}
int count;
char buffer[1024];
while(count = fread(buffer, 1, 1024, in))
{
int i;
int end = count;
for(i = 0;i < end; ++i)
{
((unsigned int *)buffer)[i] ^= xorkey;
}
if(fwrite(buffer, 1, count, out) != count)
{
fclose(in);
fclose(out);
printf("fwrite() error\n");
return -1;
}
}
fclose(in);
fclose(out);
return 0;
}
我无法弄清楚如何在 C++ 中减去字节。XOR 例程本身看起来是正确的,不是吗?请注意,我还尝试从文件末尾到开头加密文件。有任何想法吗?
谢谢!