简体   繁体   English

反^运算符进行解密

[英]Reverse ^ operator for decryption

I'm trying to reverse the following code in order to provide a function which takes the buffer and decrypts it. 我正在尝试反转以下代码,以提供一种使用缓冲区并将其解密的功能。

void crypt_buffer(unsigned char *buffer, size_t size, char *key) {
    size_t i;
    int j;

    j = 0;
    for(i = 0; i < size; i++) {
        if(j >= KEY_SIZE)
            j = 0;
        buffer[i] ^= key[j];
        j++;
    }
}

I was wordering if a simple buffer[i] ^= (1/key[j]); 如果一个简单的buffer[i] ^= (1/key[j]); will be enough to decrypt the encrypted file. 足以解密加密的文件。 But there will be some issues with truncation, isn't it? 但是截断会有一些问题,不是吗?

This is not a power operator. 这不是电力运营商。 It is the XOR operator. 它是XOR运算符。 The thing that you notice for the XOR operator is that x ^ k ^ k == x . 您为XOR运算符注意到的是x ^ k ^ k == x That means that your encryption function is already the decryption function when called with the same key and the ciphertext instead of the plaintext. 这意味着当使用相同的密钥和密文而不是明文调用时,您的加密功能已经是解密功能。

In C, the ^ is not the “power” operator (there is no such operator) but the “xor” operator. 在C语言中, ^不是“幂”运算符(没有此类运算符),而是“ xor”运算符。

Which is great news for you (I assume), as the reverse operation of “xor” is “xor” itself :-) 对我来说,这对您是个好消息,因为“ xor”的反向操作是“ xor”本身:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM