If you're seeing this message, it means we're having trouble loading external resources for Khan Academy.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

# Why must we use XOR?

Does it really matter if we used AND, OR or XOR with the one-time pad? The answer is yes, and it’s extremely important to understand why. Recall from the previous article that AND has a 75% of outputting 0 and a 25% chance of outputting a 1. While OR has a 25% chance of outputting 0 and 75% chance of outputting 1. While the XOR operation has a 50% chance of outputting 0 or 1.

Let’s look at a visual example to see the different scrambling effects of AND vs. OR vs. XOR  by encrypting an image. Here is a digital image of Charles Babbage:

It contains thousands of tiny colored squares called pixels. Each pixel in this image can be represented as a 24 bit sequence as shown in the previous article. Let's call this our plaintext image (or message).

First let’s see what happens when we AND each bit in the image file with a stream of random bits.

# AND

Notice most of the original message shines through. This happens anytime a random shift of 1 is applied, or when the plaintext is 0:

Next let’s see what happens when we OR each bit in the image file with a stream of random bits.

# OR

Notice most of the original message shines through. This happens anytime a random shift of 0 is applied, or when the plaintext is 1:

Finally, let’s see what happens when we XOR each bit in the image file with a stream of random bits.

(drum roll please...)

# XOR

Where did Charles go?
Notice that the plaintext only shines through 50% of the time, which results in noise as each pixel is equally likely to be 0 or 1.
This image contains no information about the original image. If we didn’t provide the shift sequence it would be impossible for you to reverse it back to the original image. You could try every possible sequence, but that would result in every possible image! How could you know it was Babbage? It's equally likely to be a picture of you or anything else you can think of.

Isn’t that interesting? Makes me smile everytime I see it!

Next, let’s practice the XOR, OR and AND operators and discover some more interesting properties while doing so....

image credit