XOR Encryption???

This is a discussion on XOR Encryption??? within the C++ Programming forums, part of the General Programming Boards category; Hi,
I am relitivly new at programing and I have been reading all the tutorials at this site. I read ...

XOR Encryption???

Hi,
I am relitivly new at programing and I have been reading all the tutorials at this site. I read the XOR encyption tutroial and have implemented it into my own program but I was wondering about one thing? Decryption? Whats the deal? Can it be done? If so how can I do it? I have been fiddling around with some codes and stuff but It always ends up encrypting it further. Thanx for your help in advance.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

The principle behind XOR encryption has to do with a curious property of XOR operation (exclusive OR),

As you know OR only produces false when both operands are false... 6 is bigger than 7 or smaller than 5. Both are false, so the overal statement is false.

With XOR (EOR, as some like to call it) when both operands are true, it also produces false. That is, It's only true when one and only one of the operands is false.

When doing a bitwise comparison you get this:

Code:

A B C
1 XOR 0 = 1
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 0 = 0

Now, check this out. You see the end result there? 1010? Now, do again a bitwise XOR against the 2n operand:

Code:

C B A
1 XOR 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0
0 XOR 0 = 0

See how by xoring the end result (C) again by the second operand(B) you go back to the initial value.

That's how you XOR encrytpion works.
So basically:

A XOR B encrypts to C
C XOR B decrypts back to A

Also note that the operation doesn't care for where the operand is in relation to the operator. I don't know how it is called in English. But it's the same property as with addition and multiplication... 1 + 2 = 2 + 1
In portuguese it's called commutative property ("propriedade comutativa")

And it still gives me a bunch of gibbity gork text (which i know is encrypted) I want it to output the text that was input into another program and encrypted to a text file??????
here is my encrytion source????

Well, like I said, you use the same thing to decode the text that you use to encode the text. That means you don't need two different pieces of code to make this work, a single program will do it for you. One problem is that you were outputting your encrypted text using cout when you should have been using a file so that the data could be saved somewhere. A second problem was that your input filename was fixed. It is much better to use two files, one for reading data from and a one to write data to. Also, let the user specify the filenames by passing them into the program from the command-line. With some modifications to your original code, I got this to work:

The program is run from the command-line and passed two parameters, the name of the input file, and the name of the output. I called the program encrypt and tested it like so:

encrypt file1.txt file2.txt

This encrypted the plain text data in file1.txt and created a file named file2.txt that contained the encrypted text. Then I ran the program a second time like so:

encrypt file2.txt file3.txt

This took the encypted text data in file2.txt and converted it back into plain text in a file named file3.txt. If you try this yourself, you can then print out the contents of all the files and see what they look like. The contents of file1.txt and file3.txt in my case were identical indicating that everything worked. file2.txt contained what appeared to be garbage meaning the encyrption worked as well. As you can see there is no separate decrypt program, just a single program that does both encryption and decryption.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens