what the XOR!!

This is a discussion on what the XOR!! within the C Programming forums, part of the General Programming Boards category; hey-ho beautiful people!! Can someone give me a little help understaning 'XOR'?? I'm trying to make an encryption program using ...

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    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!?!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    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??

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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!
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Wink

    That fits with exactly whats happening!!

    so how can i overcome this annoying ocurrence?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    Funny!

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > 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 fread and fwrite
    - using mem... functions for moving it around in memory.
    - using things like %x in printf statements to print each byte in an unambiguous manner.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by the bassinvader
    That fits with exactly whats happening!!

    so how can i overcome this annoying ocurrence?
    Umm, avoid setting the character to somechar^samechar result?
    If you understand what you're doing, you're not learning anything.

  10. #10
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    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).
    Last edited by OnionKnight; 08-11-2006 at 01:17 PM.

  11. #11
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Talking

    Thanx to everybody for the help!!

    especially to onionknight who managed to solve the problem in one simple step

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. Simple Xor Encryption help
    By soadlink in forum Windows Programming
    Replies: 13
    Last Post: 10-18-2006, 11:51 AM
  3. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 07:35 PM
  4. xor encryption and streams
    By mazo in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2002, 08:47 AM
  5. XOR lists?
    By bgrahambo in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2002, 09:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21