C / C ++中的简单XOR加密例程 [英] Simple XOR encryption routine in C/C++

查看:151
本文介绍了C / C ++中的简单XOR加密例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XOR加密/解密文件。我有以下加密/解密程序,其中每个字节都是xor'd,结果被减去位于前一个位置的字节的值。 ASM表示如下

I'm trying to encrypt/decrypt a file using XOR. I have the following encryption/decryption routine where every byte is xor'd and the result is being subtracted by the value of the byte that is located at the previous location. The ASM representation is as follows

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 ++ 。我的代码如下

That is what my encryption routine should look like, I'm trying to port this this C/C++. My code is as follows

#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例程本身看起来正确,不, 请注意,我还要从文件结尾开始加密文件,直到开始。任何想法?

I cannot figure out how to do the subtracting of bytes in C++. The XOR routine itself looks correct though, no? Please note that I'm also trying to encrypt the file starting from the end of the file till the beginning. Any ideas?

谢谢!

推荐答案

下面是如何在C中编写汇编语言函数。我保留了变量名与注册名相同所以你可以看到各个部分如何匹配。

Here's how you could write that assembly language function in C. I've kept the variable names the same as the register names so you can see how the various parts match up.

void do_xor_crypt(char *buffer, int count) {
    char *ecx = buffer;
    int eax = count - 1;
    if (eax > 0) {
        do {
            char dl = ecx[eax];
            dl ^= 0xC5;
            dl -= ecx[eax-1];
            ecx[eax] = dl;
            eax--;
        } while (eax > 0);
    }
}

请注意,我已经检查以确保 eax 大于零(意思是 count 是两个或更多),以便循环有一些减法。您可以将此代码集成到您的阅读循环中,如:

Note that I have checked to make sure eax is greater than zero (meaning count is two or more) so that the loop has something to subtract. You could integrate this code into your reading loop like:

while (count = fread(buffer, 1, 1024, in))
{
    do_xor_crypt(buffer, count);
    if (fwrite(buffer, 1, count, out) != count)
    {
        // ...
    }
}

这篇关于C / C ++中的简单XOR加密例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆