Thread: pointers, chas, XOR encryption crashing

  1. #1
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332

    pointers, chas, XOR encryption crashing

    rightyo, something i absolutley hate in C++ (being a PHP programmer at heart ) is pointers and memory issues. every time i think i've understood when to use * or &, i try to compile and end up taking it off anyways, thats not the problem. problem is that when i run this code, it crashs my computer. now to my understanding(which came a bit after) what happend was that XORing a memory adress made it crash. but even with all the * and () stuck every which way, i can't get it to work could someone correct this and explain whats wrong. cheers

    Code:
    #include <iostream.h>
    #include <string.h>
    char * encrypt(char * input, char * key)
    {
    
        for (int i=0; i<=strlen(input);i++)
        {
    
           for (int u=0; u<=strlen(key);u++)
           {
           //cout<<input[i]<<endl;
           input[i] = input[i] ^ key[u];
           }
        }
    
        return(input);
    }
    
    int main()
    {
        char * message = "hell, this is a bangin message";
        char * key = "this is a bangin key";
    
        message = encrypt(message, key);
        cout<<message<<endl<<endl;
        message = encrypt(message, key);
        cout<<message<<endl<<endl;
    
        cin.get();
    
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, the most likely cause is that you are crashing because you are going outside of the bounds of your array (<= should be <). Remember, you start at zero.

    Additionally, you might want to note that your return value really doesn't do anything... That is, you are not copying the string, just the pointer.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char * message = "hell, this is a bangin message";
    String constants are in read-only memory - that's why it crashes when you try and change them

    char message[] = "hell, this is a bangin message";
    Now this is something you can modify


    > input[i] = input[i] ^ key[u];
    If the message letter and the key letter are the same, then the result is 0
    Bad news when you come to output it, because that is where the output will stop
    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.

  4. #4
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    thank you

    what is the differnce between char* message and char message[] then? i read somewhere they were the same

    how do i return a string then if it isn't as a pointer or from the heap? let me guess... i don't?

    could someone tell me the BEST way to go about writing functions for strings?

    ooooh i hate pointers

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A couple of tutorials on why arrays are not identical to pointers
    http://www.eskimo.com/~scs/C-faq/s6.html
    http://pweb.netcom.com/~tjensen/ptr/pointers.htm

    > how do i return a string then if it isn't as a pointer or from the heap? let me guess... i don't?
    Your method is correct, except for the way you store a new string
    Try it with just calling strcpy(), you'll see
    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.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Your code encrypts each character from the source string with *every* character from the phrase string. Are you sure this is intended?

    Didn't you want to do something like this?

    Code:
    void encrypt(char * inout, char * key) {
       int u= 0, umax= strlen(key);
       for (int i= 0; i < strlen(inout); i++) {
         inout[i]= inout[i] ^ key[u];
         u++;
         if (u >= umax)
           u= 0;
       }
     }
    I didn't test it - just so you can understand what I mean.

    On a sidenote, you could also artificially up the size of input and key to a multiple of 4 (pad some \0), cast them to and int array and xor the ints. The advantage is that xoring an int is almost as fast as xoring a byte but you need to loop a lot less often (because an int is 4 times the size of a char). That should speed up your algorithm by a factor of about 3 alone. You could also check what google says about loop unrolling.
    Last edited by Nyda; 07-03-2004 at 07:42 AM.

  7. #7
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    yep tis intended. i don't know if it makes it more secure, but i felt like looping in a loop for some reason. oh well..

    strncpy()... DOH! i always forget about that one its happend to me before too

    thanks folks

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by whackaxe
    yep tis intended.
    Just note that it's actually (a lot) less secure because you are basically encrypting with the same character over and over. You would get the same result by running your inner loop once, store it's result and apply it to each character in the source.

  9. #9
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    oh could you suggest a way to enhance XOR encrytption?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM