XOR を使用してファイルを暗号化/復号化しようとしています。次の暗号化/復号化ルーチンがあり、すべてのバイトが 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 ルーチン自体は正しいように見えますが、そうではありませんか? ファイルの最後から最初までファイルを暗号化しようとしていることに注意してください。何か案は?
ありがとう!