Giliweed
Giliweed

Reputation: 5177

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?

Upvotes: 6

Views: 11784

Answers (3)

Transcendent
Transcendent

Reputation: 5755

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.

Upvotes: 2

Pete Baughman
Pete Baughman

Reputation: 3034

Forget about the arrays - just solve it for one bit.

Starting with the truth table for XOR:

a | b | c
0   0 | 0
0   1 | 1
1   0 | 1
1   1 | 0

If C (the output) is zero, can you figure out A and B? The answer is . . . no!

If you know that C is zero, then [A,B] was either [0,0] or [1,1]. The same thing happens if C is 1. You know that [A,B] was either [0,1] or [1,0].

Now, if you happen to know A and C, then you can figure out B. More generally, if you know two then you can figure out the 3rd. If you only know one then you can narrow down the other values to two choices, but that's as far as you can get without additional information

Upvotes: 9

bkane521
bkane521

Reputation: 386

XOR is a reversible process; if you XOR the result with the same 2-byte array you used in the first example, you will get back the original. E.g. you have a two byte array Array1, and you XOR it with a two byte array Array2 to get the result Array3. If you want to get back Array1, simply take Array3 XOR Array2. Hope this helps

Upvotes: 5

Related Questions