The reason of using XOR operation in cryptographic algorithms

XOR is an operation that can always be reversed, all information is retained. It is as simple as that. Using AND and OR may result into information loss because you cannot tell if certain bits were either 1 or 0 in the operands.

Playing a little with the python repl, we will use 19 (10011) and 5 (101) since these numbers make for good examples.

Using OR you lose the information about which of the operands had a certain bit on:

>>> bin(19|5)
'0b10111'

Isn't that 19? Yep, that's 19. The resulting number cannot tell us that it has been ORed with 5.

Using AND is the same:

>>> bin(19&5)
'0b1'

That's 1. There is not way for us to recover neither number (19 or 5) easily, we lost information again.

XOR is good for cryptographical tools because you do not lose information when XORing:

>>> bin(19^5)
'0b10110'

That's 22. Apparently that does not tell us much, but see this:

>>> bin(22^5)
'0b10011'
>>> bin(22^19)
'0b101'

You can recover either number by XORing the 22 with the other number. That's a very primitive conception of a secret key. Assuming that you and someone else can have a huge list of very big and random numbers (both of you have the same numbers), you can simply XOR messages and then securely exchange between yourselves. As long as you guys use a different number every time you communicate that is a secure algorithm. The algorithm is called an one-time pad.


XOR is used in cryptography for two main reasons: it is reversible e.g if A xor with B results in C then A xor with C will give B. it makes equal probability of 0 and 1 unlike AND & OR. if AND is used, there are 75% chances of output 0 and 25% of 1,if OR is used, there are 75% chances of output 1 and 25% of 0 so '1' and '0' output is not uniformly distributed and thus will not create randomness.