Posted February 4, 2011 by Spyros in C/C++ Programming

How The XOR Operation Works and How to Use it In C++ to Create a Small Encryptor


Exclusive OR (xor) is probably one of the most used operations in today’s encryption schemes. If you’re into creating some kind of encryption in your programs, you would need to utilize XOR operations, as XOR is a pretty versatile “tool” that you should use. If you care to know, XOR is actually an exclusive OR operation. While a simple OR is something like ‘x + y’, exclusive OR is the operation xy’ + x’y. In case you’re not familiar with the mathematic language, + stands for the operation OR, xy is sort for x AND y, while x’ is the NOT equivalent of x.

Chances are that you’re not interested in logic gates, though. Hence, let’s go about creating and explaining a simple program that encrypts data using xor. Before actually showing you the program source code, i would like you to know how XOR works. Remember that we are talking about a bitwise operation here. Therefore, you can have the states shown below :

1 XOR 1 = 0

0 XOR 0 = 0

1 XOR 0 = 1

0 XOR 1 = 1

In short, XOR is exactly like OR, with the important difference that 1 XOR 1 gives 0 as a result and not 1 (that OR would give). Hope that makes sense, let’s now take a look at the example program below :

#include <iostream>
#include <string.h>
using namespace std;

int main()
    char* plainText = "Hello";
    char key = '1';

    for (int i=0; i<strlen(plainText); i++ )
        char encrypted = *(plainText + i) ^ key;
        cout << (int)encrypted << " ";

Simple, isn’t it ? In this example program, we have the “hello” plaintext and we use a key that consists of 1 byte. I understand that this is a security problem, this is just for the sake of simplicity here. In your programs you could and should be using a longer key of course. We start traversing the chars of the plaintext and we just use the ^ xor operand to xor our characters against our key and then just print out the result as an integer value. Upon execution of the program, we get the values :

121 84 93 93 94

Now, do you understand why we get these values ? If not, let’s clarify it a bit. The first thing we need to do is convert our “Hello” string to its binary representation. In order to do that, you can just check the ASCII table and get the hex values and easily convert them to binary. Our key is represented as 31 in hex and 0011 0001 in binary. We now perform the xor operations :

Letter ‘H’ against key ‘1’ -> 0100 1000 XOR 0011 0001 -> 0111 1001 -> 79 in hex -> 121 in decimal

Letter ‘e’ against key ‘1’ -> 0110 0101 XOR 0011 0001 -> 0101 0100 -> 54 in hex -> 84 in decimal

Letter ‘l’ against key ‘1’ -> 0110 1100 XOR 0011 0001 -> 0101 1101 -> 5D in hex -> 93 in decimal

Letter ‘o’ against key ‘1’ -> 0110 1111 XOR 0011 0001 -> 0101 1110 -> 5E in hex -> 94 in decimal

You see why we got ‘121 84 93 93 94’ as a result ? Don’t forget that you can do the reverse operation now in order to go back to the plaintext. Just XOR the encrypted values against the key ‘1’ and you will now get the string ‘Hello’ back.