Thread: XOR encryption problem

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    XOR encryption problem

    Im wanting to learn basic encryption to stop editing of .txt files for my game. But I have run into a problem my program crashes ! I just can't find the problem

    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    using namespace std;
    
    void Encode_m(char message[256],char key [256])
    {
    	for (int i=0;i<strlen(message);i++)
    	{
    		message[i] = message[i]^key[i];
    		cout << message[i];
    	}
    }
    int main()
    {
    	Encode_m("This is a test","ABCDEFG");
    	system("pause");
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    your algorithm looks good.. i think it's just the way you are passing your strings into Encode_m. You could strcpy( ) your strings into your character arrays before passing them into the function.. or you could use string class objects. also, you might want to make sure all 256 elements of the key array have been initialized, or you'll be xor'ing with possible unknown garbage data.



    optimization tips:

    based on this try taking out function calls in your loops. this saves you an extra push/pop from the stack per each loop iteration. try one of these methods:

    set a variable before you enter the loop
    Code:
    int max = strlen(message) ;
    
    for (int i=0;  i<max;  ++i)

    or even set a variable in the initalization part of your for loop
    Code:
    for (int i=0, max=strlen(message);  i<max;  ++i)
    also, pre-incrementing your loop counter is supposed to save you a bit of processor time.
    Last edited by The Brain; 07-29-2005 at 06:28 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    pre-incrementing your loop counter is supposed to save you a bit of processor time.
    ??? how?

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by The Brain
    also, pre-incrementing your loop counter is supposed to save you a bit of processor time.
    It doesn't.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    also, pre-incrementing your loop counter is supposed to save you a bit of processor time.
    For a user defined type with appropriate post/pre-increment operators, that might help, but any decent compiler will optimize the difference out between the two for you with primitive data types. Even if it didn't, if you really need something optimized that much, then you probably ought to be writing in assembly, as the difference (unoptimized) is likely to be barely perceptible.

    ??? how?
    The theory is (in pseudo-code):

    pre-inc (++x):
    > x = x + 1
    > return x

    post-inc (x++):
    > tmp = x
    > x = x + 1
    > return tmp

    So, in theory, you save having to create a temporary variable. But, again, your compiler will probably optimize this for primitive types.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    14
    It's simply because the key and message doesn't have the same length. Use this:
    Code:
    message[i] = message[i]^key[i%strlen(key)];

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by cgod
    Im wanting to learn basic encryption to stop editing of .txt files for my game. But I have run into a problem my program crashes ! I just can't find the problem

    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    using namespace std;
    
    void Encode_m(char message[256],char key [256])
    {
    	for (int i=0;i<strlen(message);i++)
    	{
    		message[i] = message[i]^key[i];
    		cout << message[i];
    	}
    }
    int main()
    {
    	Encode_m("This is a test","ABCDEFG");
    	system("pause");
    }

    The reason why it doesnt work is because the message is larger than the key, therefor at the last letter for example "t" (position 13) , there is no matching letter in the key for that position.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not to mention the fact that "strings like this" are likely in read-only memory, and thus cannot be modified.
    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.

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    also
    Code:
    message[i] = message[i]^key[i];
    could be written like this:
    Code:
    message[i] ^= key[i];
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    void Encode_m(char* message, char *key) {
        for (int i = 0; i < strlen(message); i ++) {
            message[i] ^= key[i];
            cout << message[i];
        }
    }
    
    int main() {
        Encode_m("This is a test","ABCDEFG");
        system("pause");
        return 0;
    }
    I don't see why that won't work. (Not right now at least . . . . )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    Because ...

    When variable "i" is equal to 6 it XOR's the "s" from "this is a test" with G in "ABCDEFG".
    When variable "i" is equal to 7 it XOR's the " " from "this is_a test" with a garbage value from "ABCDEFG" array because there IS NO CHARACTER IN THE 7TH SPOT!!!

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    And another suggestiong, I dont know if someone already told you this (too lazy to re-read everything).

    But use strings. They are not the enemy... and you are also using c++ so its common to use strings.

    If you NEED a c string use a string then use "MyString.c_str()"

    Strings make life so much easier IMHO.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sorry, this code
    Code:
    key[i]
    should be
    Code:
    key[i%strlen(key)]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    *this
    Join Date
    Mar 2005
    Posts
    498
    Actually it shouldnt... That would slow it down.

    Call strlen once and save it to a variable. Also if he choses to do it that way he can althogh there are many other algorithms that he can choose from.

    Plus thats already been said, and again this is C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM