Xor

This is a discussion on Xor within the C++ Programming forums, part of the General Programming Boards category; I went to a website for XOR encryption and found the code but i don't get what char*out is needed ...

  1. #1
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148

    Xor

    I went to a website for XOR encryption and found the code but i don't get what char*out is needed for and how i would out put the encrypted data to the screen any help thank you!

    Code:
    #include <iostream.h>
    void XOREncrypt(const char* Text, const char* Key, char* Out)
    {
    	long CurIndex = 0, KeyIndex = 0;
    	delete[] Out;
    	Out = new char[strlen(Text)];
    	for(CurIndex = 0; CurIndex < strlen(Text); CurIndex++)
    	{
    		Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
    		KeyIndex++;
    		if(KeyIndex == strlen(Key))
    			KeyIndex = 0;
    	}
    }
    
    int main()
    {
     XOREncrypt("I dont get it", "(Ta5!34hEf$", "");
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    This function is never going to work.

    You allocate the Out pointer with the new operator. This means the Out pointer gets a new address. In stead of giving the output buffer to the Encrypt function just allocate a buffer in the encrypt function and return it:

    Code:
    #include <iostream.h>
    
    /* only works for text(files) */
    char * XOREncrypt(const char* Text, const char* Key)
    {
       long CurIndex = 0, KeyIndex = 0;
       char *Out = new char[strlen(Text)+1];
    
       for(CurIndex = 0; CurIndex < strlen(Text); CurIndex++)
       {
          Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
          KeyIndex++;
          if(KeyIndex == strlen(Key))
             KeyIndex = 0;
       }
       Out[CurIndex] = '\0';
       return Out;
    }
    
    int main()
    {
       char * out = XOREncrypt("I dont get it", "(Ta5!34hEf$");
       cout << out << endl;
       delete[] out;
       return 0;
    }

  3. #3
    Shadow12345
    Guest
    exclusive OR makes the bit 0 when both are 1 correct?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shadow12345
    exclusive OR makes the bit 0 when both are 1 correct?
    Yes
    Code:
    XOR:
    
    a b   a x b
    -----------
    0 0   0
    0 1   1
    1 0   1
    1 1   0
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    devour89: That code was probably off of my website (xsquared.ca)
    Monster: Thanks for fixing that. I've updated that now, I wrote that code when I was just starting C++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    You are right Xsquared it was of your site... well its fixed now thank you for all the help. But since we're encrypting messages we also want to decrypt them....

    Xsquared do you have anything on that site of yours which can help me??

    Thank you

    -Devouring One-

  7. #7
    Shadow12345
    Guest
    This is somewhat off of the topic, but monster (or someone else perhaps?) didn't you come up with a way to swap the values of variables using exclusive or? You should show that to me again, I don't remember exactly how you did that.

    a ^= b ^= c or something like that?

    How useful is manipulating bits in C++? I haven't come across a time where I've needed to do that. Are there any other major purposes for doing it (other than encrypting messages?)

  8. #8
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    How useful is manipulating bits in C++? I haven't come across a time where I've needed to do that. Are there any other major purposes for doing it (other than encrypting messages?)
    it's very useful. When makin algorithms and stuff I use it all the time since it's often the fastest(and the easiest) way to do it.
    Also when manipulating bit-data and conversion between, for instance, 2 8-bit vars into one 16-bit, and stuff like that.
    I often find myself in situations where it's more or less necissary to use it..

    /btq
    ...viewlexx - julie lexx

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shadow12345
    How useful is manipulating bits in C++? I haven't come across a time where I've needed to do that. Are there any other major purposes for doing it (other than encrypting messages?)
    I don't know if there is a situation where you have to use bit-manipulation, but it sure makes things easier in some situations. In encryption as was mentioned above, and to save memory if you use boolean variables. What would you prefer? 1 char with 8 boolean flags (1 byte) or 8 boolean variables (8 bytes) .
    Last edited by Magos; 12-06-2002 at 09:19 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    How useful is manipulating bits in C++? I haven't come across a time where I've needed to do that. Are there any other major purposes for doing it (other than encrypting messages?)
    As addition to the already given examples, in datacommunications you have a lot to do with the single bits in datapackets.

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Shadow12345
    This is somewhat off of the topic, but monster (or someone else perhaps?) didn't you come up with a way to swap the values of variables using exclusive or? You should show that to me again, I don't remember exactly how you did that.

    a ^= b ^= c or something like that?

    How useful is manipulating bits in C++? I haven't come across a time where I've needed to do that. Are there any other major purposes for doing it (other than encrypting messages?)
    I think it was DavidP, the method was a^=b^=a^=b; I ran some tests... and in all cases it seems to be slower than a conventional swap.

  12. #12
    Shadow12345
    Guest
    a^=b^=a^=b;

    a equals a crossed with be crossed with a crossed with b...what the hell how do you people think of these things it doesn't even make sense to me. I guess I am dumb.

    ok lemme see
    Xor makes the bit 0 when both are 1, if one of the bits is 1 then the crossed bit is 1

    a = 1100
    b = 0101

    axb = 1001
    a crossed with b equals 1001

    axb x axb
    1001
    1001
    ---------
    0000

    i am confused
    oh well

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    a = 1100
    b = 0101

    a = a ^ b = 1100 ^ 0101 = 1001
    b = b ^ a = 0101 ^ 1001 = 1100
    a = a ^ b = 1001 ^ 1100 = 0101

    a = 0101
    b = 1100

  14. #14
    Shadow12345
    Guest
    ohhh

    dude

    that's sweet

    i like that a lot damn i wish i'd thought of that, you guys are geniouses (sp? or maybe im just slow)

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>a^=b^=a^=b;
    >>you guys are geniouses
    Err.... no you're not
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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. what the XOR!!
    By the bassinvader in forum C Programming
    Replies: 10
    Last Post: 08-11-2006, 02:08 PM
  4. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 07:35 PM
  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