The exclusive or operation – a logical function applied to binary bits, like AND, OR, and NOT – is a fundamental encryption technique. It is often used in stream ciphers, which are widely used in web browsers when connecting to secure web servers.
When used properly, this technique provides strong protection. In fact, it is the basis for the one-time pad, the only provably uncrackable encryption. However, this protection is easily eroded if the cipher is not used correctly.
Xor is a trivial operation for computer logic to perform (click here for the details). The operation often appears as a built-in machine instruction so that software can perform it in a single machine operation.
If Alice wants to send a secret message to her friend Bob, she takes the sequence of bits in the message (the plain text) and a sequence of bits known only by her and Bob – the key. To encrypt, she combines the plain text and the key, bit by bit, using xor.
In a one-time pad, Alice and Bob must use a different set of secret, randomly generated bits for every message they exchange.
In a stream cipher, Alice and Bob share a much smaller number of secret bits and use them to generate a long, hard-to-guess sequence of bits. The stream cipher relies on a cryptographic algorithmto generate that long sequence from a small, shared secret. This generated sequence is then combined with the message using xor.
A Graphic Example
Below we have the handwritten message “Send Cash” embedded in a 128 by 128-bit image. Black indicates no color, so the black text in the image contains zero bits, and the white space contains 1 bits. For a key, we have collected a 128 by 128 matrix of random bits. In fact, the bits come from a web site, random.org, that uses radio noise to generate random data for experiments like this. We will combine the two matrices using xor:
Message “Send Cash”
When we apply xor bit-by-bit to the two matrices, we get the following 128 by 128 matrix of encrypted bits:
Encrypted “Send Cash”
Yes, this looks like nothing more than a mottled gray block, and it doesn’t look a lot different from the gray-block image of the encryption key. If we look closely, the actual bits are different. Here is a closeup of the upper-left corner of the key bits and the encrypted bits. Most of the bits are identical in the two closeups. The bits in the lower-right closeup are different.
In the closeup, most of the image is the same in both the key and the ciphertext. The plaintext “Send Cash” message consists of white space (one bits) except where the black letters (zero bits) appear. The xor operation combines black bits in the key with white bits in the message to yield black bits in the encrypted message.
The closeup’s lower right corner captures part of the letter “S” in the message. The black plaintext combines with black key bits to yield white. White key bits are also reversed by the black plaintext.
Even though the key image and the encrypted message look similarly gray, they contain different bits. That difference hides the encrypted message.
Try it yourself
These example images are 128 by 128 bit maps in GIF format. If you have a program that can read GIF files, save them as bit maps, and apply the xor operation bit-by-bit, then you can easily repeat this operation. The examples here were processed by Matlab, a commercial package, but there are numerous other packages that can reproduce the example.
Download these images:
If you apply the xor operation to the first two bit maps, you produce the third. If you combine the key with the encrypted message (k ⊕ e), you will reproduce original “Send Cash” message.
The following table shows how the xor operation transforms individual bits. Let m be a bit from the plain text message, and kbe a bit from the key. The ⊕ column shows the resulting bit.
We can also describe the xor operation in terms of traditional logic operations AND, OR, and NOT. Here we use “C” programming language notation: NOT = !, AND = &, OR = |.
(!m & k) | (m & !k)
To decrypt a message, Bob takes his own copy of the key bits, and applies the same xor transformation to the message, bit by bit.