How to decrypt simple XOR encryption

前端 未结 2 564
余生分开走
余生分开走 2020-12-05 08:30

I found the following XOR encryption function on the internet:

void xor_encrypt(char *key, char *string)
{
    int i, string_length = strlen(string);
    for         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 09:33

    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.

    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)

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

提交回复
热议问题