XOR encryption on c++

Recently I studied about encryption; It was quite hard for me to catch up but at least now I know a little bit about it. My target was simple, I am able to implement most simple encryption by my self. After reading some article and tutorial I found about XOR chipper.

What is it?

XOR operation is simple; If there is two clause and one of them is true so the result is true, but not if both of them true.

So here is truth table of XOR operation :

A | B | XOR
-------------
F | F | False
T | F | True
F | T | True
T | T | False

The special part of XOR operation result is you can’t tell which input is true or false by looking from the result. Also when you apply it for second time with same key, it means you decrypt it.

What I like about XOR chipper (by what I read)  is quite hard to crack when used properly. You can find what it properly means here.

There are a lot of samples of XOR chipper implementation in any programming language, but here is my version.

string xor_crypt(string data, string key)
{
 string encrypted = data;
 int keyPos=0;
 for(int a=0;a<data.size();a++)
 {
  if(keyPos==key.size())
   keyPos = 0;
  encrypted[a] = data[a] ^ key[keyPos];
  keyPos++;
 }
 return encrypted;
}

Tips :

  1. Use long and strong key
  2. Apply more than one time if possible

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