Thread: wrong wrong with my xor function?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    wrong wrong with my xor function?

    Code:
    char *Encode(char *str, char *key)
    {
    	unsigned int i, j;
    
    	for (i = 0; i < strlen(str); i++)
    	{
    		for (j = 0; j < strlen(key); j++)
    			str[i] ^= key[j];
    
    		str[i] = ~ str[i];
    	}
    
    	return str;
    }
    to use i do

    char *WSAStartupChar ="\xFC\xF8\xEA\xF8\xDF\xCA\xD9\xDF\xDE\xDB"; //WSAStartup
    char *WSAStartupEncode = Encode(WSAStartupChar,key);
    char *WSAStarupDecode = Encode(WSAStartupEncode,key);
    WSAStartup = (WSAS)GetProcAddress(ws2_32_dll,WSAStarupDecode);

    whats can the prob be ,

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I have no idea what you're trying to do. But the method you are using here is equivalent to simply xor'ing each character of the string with a constant byte value. Except that instead of O(N), you've managed to achieve O(N^2 * K^2) where N is the length of the string and K is the length of the key, while at the same time losing the entire advantage of a long key.

    Bravo, you've implemented one of the weakest cryptographic functions known, and you're doing it in ~O(N^4) time!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Why ~O(N^4)? Looks more like O(N*K) to me.

    What is wrong. Well, you pass a constant string to a non-constant string, modify it, which likely results in a crash. Also, you misspelled startup.

    You know, I've done some malware-reversing. And it sounds a whole lot like you are writing malware. These practices are extremely common there. So I shouldn't be helping you. But I don't mind; if you write code like this, you won't be able to write working malware anyway.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by EVOEx View Post
    Why ~O(N^4)? Looks more like O(N*K) to me.
    Notice the calls to strlen()? Those are O(N) themselves.

    And it sounds a whole lot like you are writing malware. These practices are extremely common there.
    Common there, but also more common in legitimate software than you might think at first... You just don't hear about it because those engineers don't go online asking stupid questions.
    Last edited by brewbuck; 04-25-2009 at 02:21 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The first problem is with your question. You tell us what you do, and then you ask what the problem is. But you haven't told us why you think there's a problem in the first place! Does it fail to compile? Post the compiler output. Does it crash? Does it run, but produce incorrect output? Tell us the expected and actual output. As it is, you haven't really asked a proper question.

    Turn up the warning level of your compiler. Read those warnings and heed them. You're writing to read-only memory.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes that's a truly horrid function implementation indeed!
    That fact that a const is missing on key is the least of its problems.
    The only redeeming quality I could find is that the indentation is correct.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM