Categories

The real power of xor.

Ok, let’s go down talk about some real programming.
This time, it is about xor (exclusive-or).

I will show some real benefit that we can gain from using it.

Imagine that you want to send some message to your friend, but you want to encrypt it, doesn’t allow anyone to easily grab out the information you send.

How we can make use of xor to apply to this case?

At first, from the mathematical sense, we all know that

x xor y xor y = x

It means that if we send some information, x, and then take the xor-operation with y. Now we can encrypt the messege and bravely send it out in the open world.

As you can suggest, if we take xor-operation again of that encrypted message with y again, we will get the real information. WOW!!!

In this point, all you need is just that y information, in which we must know between each other in order to read the real message.

(The message can be anything range from normal text, images file or byte data.)

Let’s follow my step to implement it, and assume that in this case I have an image to send to my friend.

Our image is ‘input.jpg’.

input file

From the image above, we will take its every byte to be xored with our key string “MEETMEATTOGARPARTY”.
Then we will have the encrypted image which is ready to be send.

ENCRYPTION

See the code below (Please right click and save as)
image_encrypt

From the code we can define our known key which will be known only by the two sides as I have said to you.
Or just simply said that, other end will use this key to decrypt the message just to read it.

This program is designed to accept the input file named ‘input.jpg’ and it will encrypt it and send out ‘dataen.bin’, the latter file that you will use it to send to your friend.

If you are very curious about this stuff, just try to encrypt the some image file then open it with any favourite image viewer, you cannot view your image file or you will see the heavily distort and corrupted pattern of the image file.

DECRYPTION

Let’s we turn to the decryption part.
See the code below.
image_decrypt

The same for encrypt code, and use the same key too.
After run this code, you will get the original message thus ‘input.jpg’.

Note that this is only the simple encryption/decryption algorithm, so we should increase the strength by included various and some other method too.

Get any idea?
Haxpor

1 comment to The real power of xor.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">