Exclusive-OR (XOR) Encryption

Exclusive-OR (XOR) encryption is an encryption method that is hard to break through with so called “brute force” methods (brute force = using random encryption keys in the hope you find the correct one.), but the encryption method is susceptible to pattern recognition. Patterns can be easily avoided by first compressing the file (compression already makes it unreadable, it removes patterns for you) before it is encrypted.

The XOR encryption method doesn’t make use of a public-key, such as RSA. Instead both the people that encrypt the file as well as the people that want to decrypt the file need to have the encryption key.

The exclusive-OR encryption (as the name already tells you) makes use of the Boolean algebra function XOR.
The XOR function is a binary operator, which means that it takes two arguments when you use it.
If one of the two arguments is true and the other argument is false, then the XOR function will return true.

The so called “truth table” will look as follows:

A

B

A
XOR B

True

True

False

True

False

True

False

True

True

False

False

False

Note: A and B are Boolean inputs (or binary input 1 or 0.)
For example on the first line both A and B are true. As we said before; if only one of the two is true then the XOR function will return true. In this case both are true so a false is returned.

So knowing one of the two initial values of the two argument it is impossible to reverse the operation.
For example if two unknown variables are used on XOR then the outcome can’t be predicted.
Even if you know the outcome, for instance true, you can’t tell which one of the input variables was true and which was false. Even if the outcome is false, you can’t tell if the input values where both true or false.

But you have to be careful. Encryption works on the principle that if you know the encrypted string or you know the encryption key that is used,you always can decrypt it correctly. So in this case; if you know either A or B you can reverse engineer it by performing the following operation: A XOR True (in the case you know A, replace A with B if you know that one.) If this operation returns true then you know A is false. If the operation returns false you know A is true.

You also have to remember the following and that is the longer you make the encryption key the harder it becomes to break it. (But also harder to remember it.)

In the C++ language the ^ sign is used to express a bit-level XOR.
So if you want to encrypt a single character and you have a key of one byte, then you can use the following statement:


	x = x ^ key

If you like to encrypt a string of characters with a longer key you can do something like the following program:


	#include<iostream>
	using namespace std;

	int main(void)
	{
		char my_string[17]="Have a nice day!";
		char my_key[17]="ABCDEFGHIJ123456";

		for(int count=0; count<16; count++)
		{
			my_string[count]=my_string[count]^my_key[count];
			cout << my_string[count];
		}
	}

Note: this program will encrypt each character in the string using the ^ bit operator to XOR the string value with the key value.

That’s all for this tutorial.

This entry was posted in Programming Algorithms. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There is currently one response to “Exclusive-OR (XOR) Encryption”

Why not let us know what you think by adding your own comment!

  1. Cleo Foust on March 10th, 2010:

    By far the most concise and up to date information I found on this topic. Sure glad that I navigated to your page by accident. I’ll be subscribing to your feed so that I can get the latest updates. Appreciate all the information here