问题描述
我在网上找到了如下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
}
所以基本上在我加密字符串之后,我会使用相同的加密密钥来解密之前加密的字符串.
So basically after I encrypt the string, I would use the same encryption key to decrypt the previously encrypted string.
我对编程很陌生,我只想知道如何解密以前加密的字符串.谢谢,感谢所有帮助.
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 中,您获取字符串和密钥并返回字符串 ^ 密钥.如果,现在,您再次使用密钥对它进行异或,您将得到 (string ^ key) ^ key = string ^ (key ^ key) = string ^ identity = string(按 XOR 运算符的属性: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.
这篇关于如何解密简单的异或加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!