# 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.

Exactly what i was looking for. Thanks!

but that is C++ and not C

Yes, that is true, anonymous, fixing the title, it’s weird that this went unnoticed before.

can anyone explain me this program.

#include

#include

int main()

{

char a=’a’;

char b=’b’;

clrscr();

a^=b ^=a^=b++;

printf(“%c,%c”,b,a);

getch();

return 0;

}

am getting output ad ” b,b ” .can anyone explain me.

Hello there! I know this is kinda off topic but I was wondering

if you knew where I could get a captcha plugin for my comment form?

I’m using the same blog platform as yours and I’m having difficulty finding one?

Thanks a lot!

Hey I am so excited I found your site, I really found you by

error, while I was searching on Google for something else,

Anyhow I am here now and would just like to say thank you for a remarkable post and a all round entertaining blog (I also love the theme/design), I

don’t have time to go through it all at the moment but I have book-marked

it and also added your RSS feeds, so when I have time I will be back to read much more,

Please do keep up the fantastic b.

Pretty nice post. I just stumbled upon your weblog and wished to mention that I’ve truly enjoyed surfing around your blog posts.

After all I will be subscribing to your feed and I am hoping

you write again very soon!