"encripting" by using ^

This is a discussion on "encripting" by using ^ within the C++ Programming forums, part of the General Programming Boards category; here's my code. Code: char * Encrypt ( const char * string, const char * key ) { //char * ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    "encripting" by using ^

    here's my code.

    Code:
    char * Encrypt ( const char * string, const char * key )
    {
    	//char * newString = const_cast<char*>(string);
    	std::string result = string;
    
    	for (int i=0; i<strlen(string); i++)
    	{
    		int pos = i;
    		while ( (pos+1) > strlen(key) )
    			pos -= strlen(key);
    
    		result[i] = string[i] ^ key[pos];
    	}
    
    	return const_cast<char*>(result.c_str());
    }
    
    int main()
    {
    	char * lala = "Hi. This is a test.";
    	const char * key = "STEAM_0:0:4549308";
    	char * result = NULL;
    
    	result = Encrypt( lala, key );
    
    	printf("%s \n%s\n%s", lala, key, result);
    	//for (int i=0; i<strlen(result); i++)
    	//	printf("%i", result[i]);
    	//printf("\n");
    
    	return 0;
    }
    It doesn't seem to be working... Any ideas?
    It doesn't seem to do what its supposed to, I get almost all the same character as my answer.

    What I want to do is 'encrypt' using the key, and loop the key so that it doesn't go out of range.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just find the length of the key, and use:
    Code:
    foo = key[ pos % key_length ];
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    For one thing, your Encrypt() method is returning a pointer to a local array. It would be better to copy the result into a passed buffer.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Quzah, thanks for the idea. didn't think of using modulus =)

    Bithub, thanks for the headsup =)

    Bet you its working inside the function =)

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    OK, I'm stuck on getting it to store it properly.

    How can I do this without strdup? If I just directly put it in the pointer, it doesn't work.

    if it were a single character, you'd do something like
    void Encrypt(char source, char & output);

    But how do you do that with an array?

    Code:
    char * Encrypt ( const char * input, const char * key, char * output )
    {
    	std::string result	= "\0";
    	int inputlength		= strlen(input);
    	int keylength		= strlen(key);
    
    	for (int i=0; i<inputlength; i++)
    		result += input[i] ^ key[i % keylength];
    
    	output = strdup(result.c_str());
    
    	return output;
    }
    
    int main()
    {
    	char * lala = "Hi. This is a test.";
    	const char * key = "STEAM_0:0:4549308";
    	char * result = NULL;
    
    	result = Encrypt( lala, key, result );
    
    	printf("%s \n%s\n%s", lala, key, result);
    	//for (int i=0; i<strlen(result); i++)
    	//	printf("%i", result[i]);
    	//printf("\n");
    
    	free(result);
    
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Code:
    void Encrypt ( const char * input, const char * key, char * output )
    {
    	int inputlength		= strlen(input);
    	int keylength		= strlen(key);
    
    	for (int i=0; i<inputlength; i++)
    		output[i] = input[i] ^ key[i % keylength];
    
    }
    One thing you may want to consider adding to the above code, is passing in the length of your output buffer. This way you can be sure you dont write past the end of it.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    No, in C++ you should return an std::string.
    Code:
    std::string Encrypt ( std::string str, const char * key )
    {
        std::string result;
        //magic ...
        return result;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Sang-drax: When I tried your way, it returns the adress of a local, so I get a fairly giberish answer.

    I would prefer to do it bithub's way. (i don't want to use a std::string)

    It works, except when I try to free my memory.

    Code:
    void Encrypt ( const char * input, const char * key, char * output ) 
    {
    	int inputlength		= strlen(input);
    	int keylength		= strlen(key);
    
    	for (int i=0; i<inputlength; i++)
    		output[i] = input[i] ^ key[i % keylength];
    	output[inputsize] = '\0'; // Do i need this?
    }
    
    int main()
    {
    	char * lala = "Hi. This is a test.";
    	const char * key = "STEAM_0:0:4549308";
    	char * result = new char[strlen(lala+1)];
    
    	Encrypt( lala, key, result );
    
    	printf("%s \n%s\n%s", lala, key, result);
    	//for (int i=0; i<strlen(result); i++)
    	//	printf("%i", result[i]);
    	//printf("\n");
    
    	delete [] result; // THIS CRASHES IT!!!
    
    	return 0;
    }
    this is the error I get:

    DAMAGE: after Normal block (#53) at 0x00320850

    I'm using Visual C++ 2003
    Last edited by Trauts; 11-10-2004 at 05:43 PM.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    found it.

    i was shifting the start of the strlen to the right one, not adding one to it

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >output[inputsize] = '\0'; // Do i need this?

    Where does 'inputsize' come from? Im guessing in this line your writing past the end of the buffer allocated for output buffer, which would explain why 'delete [] result;' crashes your program.

    edit: nevermind :P
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I'm still having issues (I've expanded it now).

    I have a string, which looks like this:

    13 8 4.586000 128465278 1248
    0.900000 1.200000 1.500000
    1.150000 1.300000 1.200000
    0.500000 0.300000 1.100000 1.200000 1.500000
    There are newlines at the end of each line.

    When I run the encrypt function on it, it seems like it only runs off of the first line! It gets a drastically shorter result than what I put in. I checked, and the input variable is correct.

    If I run encrypt again, and therefore remove the xoring effect, I get ONLY this:

    13 8 4.586000 12
    As you can see, it is cutting off almost everything!!!

    I believe the problem lies with

    Code:
    key[i % keylength];
    Because if I change that to a constant, it works fine. It seems that after i >= keylength, thats where it stops outputting a value

    Here's my actual code. Keep in mind, the long string is read from a file, but identical valuewise.

    Code:
    char* Encrypt ( const char * input, const char * key, char * output ) 
    {
    	int inputlength		= strlen(input);
    	int keylength		= strlen(key);
     
    	for (int i=0; i<inputlength; i++)
    		output[i] = input[i] ^ key[i % keylength];
    	output[inputlength] = '\0';
     
    	return output;
    }
     
    int main()
    {
    	const char * key = "STEAM_0:0:4549308";
    	char str[] = "13 8 4.586000 128465278 1248\n0.900000 1.200000 1.500000\n1.150000 1.300000 1.200000\n0.500000 0.300000 1.100000 1.200000 1.500000\n";
    	char buf[2048];
    	printf(Encrypt(str, key, buf));
     
    	return 0;
    }
    Last edited by Trauts; 11-10-2004 at 08:09 PM.

Popular pages Recent additions subscribe to a feed

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