如何解密简单的XOR加密 [英] How to decrypt simple XOR encryption

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

问题描述

我发现在互联网上以下XOR加密功能:

I found the following XOR encryption function on the internet:

void xor_encrypt(char *key, char *string)
{
    int i, string_length = strlen(string);
    for(i=0; i<string_length; i++)
    {
        string[i]=string[i]^key[i];
        printf("%i", string[i]);
    }
}

这完美的作品,但我想也解密字符串。

It works perfect, but I would like to decrypt the string also.

例如:

void xor_decrypt(char *key, char *encrypted_string)
{
    //decrypt method goes here
}

所以基本上我后加密字符串,我会使用相同的加密密钥来解密previously加密的字符串。

So basically after I encrypt the string, I would use the same encryption key to decrypt the previously encrypted string.

我是pretty新的节目,我只是想知道如何解密previously加密的字符串。感谢所有帮助AP preciated。

I'm pretty new to programming and I would just like to know how to decrypt the previously encrypted string. Thanks, all help is appreciated.

推荐答案

一个关于XOR加密很酷的事情是,当你申请两次,你回来原始的字符串 - 看的http://en.wikipedia.org/wiki/XOR_cipher

One of the cool things about XOR encryption is that when you apply it twice, you get back the original string – see http://en.wikipedia.org/wiki/XOR_cipher.

在你的函数,xor_decrypt,你把字符串键和返回的字符串^键。如果,现在,你异或与一次该键,你会得到(字符串^键)^键=字符串^(^键键)=串^身份=字符串
(通过XOR运算符的属性:<一个href=\"http://en.wikipedia.org/wiki/Exclusive_or#Properties\">http://en.wikipedia.org/wiki/Exclusive_or#Properties)

In your function, xor_decrypt, you take string and key and return string ^ key. If, now, you xor that with the key again, you get (string ^ key) ^ key = string ^ (key ^ key) = string ^ identity = string (by properties of XOR operator: http://en.wikipedia.org/wiki/Exclusive_or#Properties)

因此​​,你可以运行你的函数,xor_encrypt,就先xor_encrypt输出第二次。

Thus, you can just run your function, xor_encrypt, a second time on the output of the first xor_encrypt.

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

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