Search code examples
c#reversexor

Reverse process of XOR


I was thinking, when two byte array is XOR'ed, is there any way to reverse the process? As I am curious to find the answer I found this code in the google which is the XOR code and this is the pseudocode of step1 (XOR code)

enter image description here

The XOR code:

public static byte[] XOR(byte[] first, byte[] second)
    {
        if (first.Length == second.Length)
        {
            byte[] result = new byte[first.Length];
            for (int i = 0; i < first.Length; i++)
            {
                result[i] = (byte)(first[i] ^ second[i]);
            }

            return result;
        }
        else
        {
            throw new ArgumentException();
        }
    }

The code works fine. Now down below is the pseudocode of step2 (reverse of XOR code) enter image description here

I am very confused how to do this step2. How can I reverse the process to get back the original byte arrays? As the only input I have is the result. Is this ever possible? Is there any way to do this? If the answer is yes then how?


Solution

  • According to what you mentioned in your comment:

    "I have to marge two byte arrays into one (all having same bits) and reverse the process to get back the original two arrays."

    This process can not be carried out by using bitwise operations. XOR, however, can be reversed if and only if you define a decryption key which in case of your question, it is not applicable.

    The best practice to merge two sets of numbers and still be able to get back the original data can be done by creating a 2 linear and 2 unknown equation. Of course, in this process, what you store in your third array is not simply "one number" but a mathematical equation. This can be done using multi-dimensional arrays to keep the values of X, Y, etc for each equation.

    This is the simplest approach for abstracting two collections of data.