XOR Encryption

I’m sure many of you may have heard about this encryption. It is known as the XOR Encryption. It uses the XOR bit-wise operator to compare two given numbers and output a result relative to their binary values. So let’s say you have the numbers (in binary) 0110101 and 0010011. You first would compare them column by column:

+---+---+---+---+---+---+---+
| 0 | 1 | 1 | 0 | 1 | 0 | 1 |
|   |   |   |   |   |   |   |
| 0 | 0 | 1 | 0 | 0 | 1 | 1 |
+---+---+---+---+---+---+---+

Then, if both numbers in a column are 0 then the result is 0
If both numbers are 1 then the result is 0
If one number is 1 and the other is 0 then the result is 1.

Basically, it’s just: if both numbers are different then result = 1, else result = 0
They named it XOR to mean “Either but not both” – If either of the values are 1 but not both.

Here’s a ‘truth table’ for the XOR operation:

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

The result will be the value of the same digit column of the final number.
Here is the result of the XOR operation of the two bit arrays:

+---+---+---+---+---+---+---+
| 0 | 1 | 1 | 0 | 1 | 0 | 1 |
|   |   |   |   |   |   |   |
| 0 | 0 | 1 | 0 | 0 | 1 | 1 |
+---+---+---+---+---+---+---+
  |   |   |   |   |   |   |
  V   V   V   V   V   V   V
+---+---+---+---+---+---+---+
| 0 | 1 | 0 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+---+---+

So the result of the XOR operation between 0110101 and 0010011 is 0100110.

So, in concept, that is the encryption. To decrypt the result just take one of the original values we used for the XOR encryption (also known as our encryption key) and XOR that number with the result. The result will be value of the other number you used for the XOR encryption.

Now when encrypting a string it’s almost the same thing except you are converting their ascii decimal (or hexadecimal) values to binary, then executing the XOR operation between the ascii character binary value and the binary value of the encryption key.

For example, we have our message “hello” and our encryption key is 11011011.

  • First we get the ascii character codes of each character in our string: 104 101 108 108 111
  • Convert each number to binary: 01101000 01100101 01101100 01101100 01101111
  • For each binary number, we XOR that with our key. The result: 10110011 10111110 10110111 10110111 10110100

To un-encrypt the 1’s and 0’s just xor each bit array with the key to get the original binary values of the character codes back.
Now if your key is small enough that when XORed with the message results in binary values that are small enough to represent different printable characters, then by all means take the binary and convert that to their ascii representation to make it look better and more complex, this makes it look less like a XOR encryption. Then you may extract their ascii char calues into binary and XOR it with the key to un-encrypt the secret message.

One thought on “XOR Encryption

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: