Hi guys

i have recently begun exploring encryption and came across XOR though i have attempted to understand it and implement it in reading files and then encrypting it but i cannot understand the decryption process of it at all and keep getting confused

``````srand(time(NULL));

for(index=0;index<MAX;index++)
{
Matrix[index]=rand()%63000000;
KeyFile1<<Matrix[index]<<endl;
}

the actual encryption:

while((count = getc(in)) != EOF)
{

for(index=0;index<MAX;index++)
{
count = (count^Matrix[index]);

}

putc(count,out);
}``````

But i cant understand how to make a decryption loop for that encryption. Can someone please help me.
Thankyou.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by mvmalderen

NOTE: Throughout my whole post I assume that bytes consist out of eight bits.

When you use the XOR-algorithm to hash/encrypt your data, there's no need to implement a different way to decrypt your data.

XOR means: eXclusive OR.
As you may know: a normal OR operation requires that either one of both operands is true, to make the result of the operation true, but if both operands are true, the result will be true as well. Only if both operands are false, the result will be false.

It's a bit different with XOR: if you want an XOR operation to have true as result, only (and only) one of the both operands may be true, if they're both true, then the result will be false, if they're both false, the result is also false, if only one of them is true (and the other one is false), the operation will yield true as its result.

And now a practical explanation on what it means when you apply this approach to bits (that is: apply XOR bitwise):
Say, you have two bytes which respectively look like this:

``````Byte 1: 10110111 ([I]data to encrypt[/I])
Byte 2: 00001101 ([I]encryption key[/I])``````

When you XOR both together, you'll get this result:

``Byte 3: 10111010 ([I]encrypted data[/I])``

Let's assume that Byte 2 was a character from the encryption key, then we can easily decrypt the whole thing by exactly doing the same: (bitwise) XOR both, the encrypted data and the encryption key together, like this:

``````10111010 ([I]encrypted data[/I])
00001101 ([I]encryption key[/I])
-------- ([B]XOR[/B])

10110111 ([I]decrypted data, original data[/I])``````

So, conclusion of this post: Once you've a working encryption loop (like you said you have), you can re-use the same loop for decryption.
You don't have to have a separate loop for encryption/decryption, you only use one in fact.
When you want to decrypt a file, you just run your program again on the encrypted file, enter the password which you used to encrypt the file, and your program should properly decrypt the file.

Only one very important thing when using XOR encryption is, what are you going to do when there are so-called nul-bytes in the file?
nul-bytes are bytes which look like this:

``00000000``

XORing nul-bytes together with a character from within the encryption key (aka: encryption password), will reveal parts of (or in the worst case: the whole) password.

My first rep++. Thanks for the refresher.

Only one very important thing when using XOR encryption is, what are you going to do when there are so-called nul-bytes in the file?
nul-bytes are bytes which look like this:

``00000000``

XORing nul-bytes together with a character from within the encryption key (aka: encryption password), will reveal parts of (or in the worst case: the whole) password.

This will make it particulary easy for John the ripper/a cracker to decrypt the data and steal it.
And that's probably not what you want.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.