Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse process of XOR

Tags:

c#

reverse

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?

like image 367
Giliweed Avatar asked May 29 '14 20:05

Giliweed


People also ask

Is XOR its own inverse?

The XOR function is only true if just one (and only one) of the input values is true, and false otherwise. XOR stands for eXclusive OR. As can be seen, the output values of XNOR are simply the inverse of the corresponding output values of XOR.

What is the negation of XOR?

The negation of XOR is the logical biconditional, which yields true if and only if the two inputs are the same.

What is the rule for the XOR operation?

If both inputs are false (0/LOW) or both are true, a false output results. XOR represents the inequality function, i.e., the output is true if the inputs are not alike otherwise the output is false. A way to remember XOR is "must have one or the other but not both".

Is XOR a binary operation?

XOR is one of the sixteen possible binary operations on Boolean operands. That means that it takes 2 inputs (it's binary) and produces one output (it's an operation), and the inputs and outputs may only take the values of TRUE or FALSE (it's Boolean) – see Figure 1.


2 Answers

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

like image 109
Pete Baughman Avatar answered Nov 08 '22 00:11

Pete Baughman


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

like image 43
bkane521 Avatar answered Nov 08 '22 02:11

bkane521