如何XOR用C具有相同功能争抢一个字符串,然后再返回? [英] How to XOR scramble a string in C and back again with the same function?

查看:103
本文介绍了如何XOR用C具有相同功能争抢一个字符串,然后再返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图混淆程序中的字符串。目前,我只有一个简单的字符串逆转工作。我希望能够执行XOR的数据加扰,使其更安全,但是我已经试过的方法是行不通的。

I am trying to obfuscate a string in a program. Currently, I only have a simple string reversal working. I would like to be able to perform XOR scrambling on the data to make it much more secure, however the method I have tried is not working.

相同的功能和输入类型用于脱code中的字符串。这是字符串的逆转没有问题的,因为它只是颠倒回来,但可以这样用异或轻松完成没有得到太复杂?如果进程一直只是一个字符串,我会preFER,像逆转一样。这是我的反转功能。

The same function and input type is used to decode the string. This is no problem with string reversal, as it just reverses back, but can this be done easily with XORing without getting too complex? I would prefer if the process kept just the one string, like the reversal does. Here is my reversal function.

void reverse_string(unsigned char *buf, int length)
{
int i;
unsigned char temp;

    for (i = 0; i < length / 2; i++)
    {
        temp = buf[i];
        buf[i] = buf[length - i - 1];
        buf[length - i - 1] = temp;
    } 
}

这里是企图在一个XOR函数

And here is the attempt at a XOR function

void charxor(char * text, int len) {
const unsigned char enc[8]={173,135,131,121,110,119,187,143};
char ch;
int i;
int ind=0;
for (i=0;i<len;i++) {
    ch=*text++;
    if (ch)
        *text = ch ^ enc[ind++];
    ind %=8;
}


}

谁能帮助?将非常AP preciated!

Can anyone help? Would be much appreciated!

推荐答案

您似乎过于复杂的事情了一下。试试这个:

You seem to be overcomplicating things a bit. Try this instead:

void charxor (unsigned char *text, int len) {
    const unsigned char enc[8] = {173,135,131,121,110,119,187,143};
    int i;
    for (i = 0; i < len; i++) {
        text[i] ^= enc[i % 8];
    }
}

请注意,该XOR操作可以引入空字符到字符串,所以你真的需要跟踪它的长度,而不是仅仅依靠尾随空字符的presence。

Note that the XOR operation can introduce null chars into the string, so you really do need to keep track of its length instead of just relying on the presence of a trailing null char.

同样需要牢记的是,虽然这可能确实是的相对的讲话更安全不仅仅是扭转了弦,任何合理聪明的人可以访问的输出足够的样本大概可以计算出如何去code将其在各地的十五分钟左右。

Also keep in mind that, while this may indeed be relatively speaking "much more secure" than just reversing the string, any reasonably clever person with access to enough samples of the output can probably figure out how to decode it in around fifteen minutes or so.

这篇关于如何XOR用C具有相同功能争抢一个字符串,然后再返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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