# what the XOR!!

• 08-11-2006
what the XOR!!
hey-ho beautiful people!!

Can someone give me a little help understaning 'XOR'??

I'm trying to make an encryption program using it but i'm having inexplicable
troubles. You see my code works fine using 'AND'and 'OR' but 'XOR' gives
me a world of weirdness!!

Using the 'XOR' the loop controling the character encryption seems to terminate
after only three or four loops, so obviously only three or four characters are
actually encoded leaving my little program rather ineffective.
here's my code

Code:

```for(i=0;i<strlen(filec)-2;i++)                      /* filec  is the line i want to encode*/ {       filec[i]=filec[i]^keyfile[i];                /* keyfile is the key copied again and again to */                                                                                        /*  match the lines length */     fprintf(ptr,"%c",filec[i]);           }```
I'm sure this problem results from a lack of education on my part so could someone
help me out and explain it?

oh and if anybody knows of any good books on encryptions let me know!?!
• 08-11-2006
itsme86
XOR is very simple:

AND
---
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

OR
---
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

XOR
---
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

It's like OR, except if they're BOTH true it's false.
• 08-11-2006
dont want to seem ungrateful but i already know that.

Why does it act differently with my program.

are there differences between compilers regarding XOR ?

I'm using dev-c++

Does that make any difference??
• 08-11-2006
itsme86
XOR is standard across all C compilers.

It looks like your problem is that you're ending up doing something like g ^ g. For AND and OR the result will be !0, but as you saw from my little charts, XOR a value with itself will give you 0. And since the value 0 is used to terminate a string, BLAM!
• 08-11-2006
That fits with exactly whats happening!!

so how can i overcome this annoying ocurrence?
• 08-11-2006
Salem
The XOR goes on the left hand side of your boat.
The YOR goes on the right hand side of your boat.
If you don't have both of them, you go round in circles :)
• 08-11-2006
Funny!
• 08-11-2006
Salem
> so how can i overcome this annoying ocurrence?
You need a separate counter to tell you how long the message is, and treat the result as being binary data.
This means
- opening files in "rb" and "wb" mode
- using mem... functions for moving it around in memory.
- using things like %x in printf statements to print each byte in an unambiguous manner.
• 08-11-2006
itsme86
Quote:

That fits with exactly whats happening!!

so how can i overcome this annoying ocurrence?

Umm, avoid setting the character to somechar^samechar result? ;)
• 08-11-2006
OnionKnight
The best way to fix the problem you described is to put the result of strlen(filec) in a variable before entering the loop and then use it instead of strlen(filec) in the conditional statement.
If filec was "abcdefghij" then strlen(filec) would yield 10. Now say that you came to a place where you were getting a \0 as mentioned it could be something like "&#164;&#163;R\0efghij" then strlen(filec) would return 3 and at this point i is 3 and so the loop will terminate. If you had only called strlen(filec) once you'd have a constant 10 throughout the loop and thus it would not end even if you got a \0 from our XOR operation.

Not only is it a good solution but you're also not calling strlen(filec) each time the loop runs. Making it O(n) instead of O(n^2).
• 08-11-2006