Skip to main content
gamedev.net
🔒 Locked

XOR encryption code help

Started by DimSum Oct 24, 2002 at 6:44 PM 11 replies 1.9k views
Original Post
DimSum
DimSum
Hi guys, I have been stuck on implementing an XOR encryption and decryption function for a text file. Here''s my encryption function:
  
/**************************************************************
* Encrypts a string using "cryptKey" string defined
* in the header file.
* Uses XOR encryption. 
* (this is why the same function is used to both encryption and 
*  decryption).
* The whole key is continuously applied at a time to a part 
* of the string until the string ends.
/************************************************************
char* CMetricFunctions::cptfnEncryptString (char* strInputString)
{
	int i = 0;
	BOOL isEndOfStr = FALSE;
	char* strPtr = strInputString;
		
	// keep encryting until string ends or key string ends

        // sizeOfKey = size of encryption key which is 11 chars

	while ((!isEndOfStr) || (i < sizeOfKey))
	{
		if (i == sizeOfKey)
			i = 0;

		*strPtr = *strPtr ^ strCryptKey[i];
		strPtr++;
		
		if (*strPtr == ''\0'')
			isEndOfStr = TRUE;		
		i++;
	}

	return strPtr;
}
  
Here''s my decrytion function:
  
char* CMetricFunctions::cptfnDecryptString (char* strInputString)
{	
	char* strPtr = strInputString;

	// keep encryting until string ends or key string ends

	for (int i=0; i < sizeOfKey; i++)
	{
		*strPtr ^= strCryptKey[i];
		strPtr++;
	}

	return strInputString;
}
  
However, its not working when I use to encrypt and decrypt a string longer than the key string (which is set to 11 characters right now). But it does work on string shorter than the key string. Can anyone help?? thanks all.
Shannon Barber
Shannon Barber
You need to ''mod'' into the key-string - otherwise you''re xor''ing in spurious crap from the heap or stack, YASBO.


(YASBO - Yet Another String Buffer Overrun).
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
DrPizza
DrPizza
quote:
char* strInputString

What a gloriously redundant variable declaration.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
DimSum
DimSum
Magmai, sounds like your suggesting might be my problem.

What do you mean by "''mod'' into the key-string"?? how do I do that??

And DrPizza, if I don''t use "char* strInputString" in the function parameter, how am I suppose to pass in the string?

thanks all.

-Phil
Alpha_ProgDes
Alpha_ProgDes
char* strInputString; <- redundant variable declaration.

char* <- implies a string
str <- implies a string
InputString <- implies you''re inputting a string

see what we mean?
Beginner in Game Development?  Read here. And read here.  
DimSum
DimSum
oh!
hahahaha
didn''t notice that.
i''ll change that.
thanks

can anyone help with getting the encrytption and decryption working though??

thanks.

Stoffel
Stoffel
pseudo-code (e.g. I haven''t compiled it, bugs may be yours to find):

  
char* CMetricFunctions::cptfnDecryptString (char* strInputString)
{
char* strPtr = strInputString; // keep encryting until string ends

int offset = 0;
while (*strPtr) // continue until null char

{
*strPtr ^= strCryptKey[offset%sizeOfKey];
++offset;
++strPtr;
}
return strInputString; // why? It will always be the input ptr

}
DimSum
DimSum
I see!
how ingenious Stoffel!!!!

thanks a bunch all!!

-Phil
Thunder_Hawk
Thunder_Hawk
quote:
Original post by Stoffel
while (*strPtr) // continue until null char



I thought that was an error until I tested it, low and behold ''\O'' = 0. You learn something new every day...

______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
DimSum
DimSum
Uh oh!
The code provided by Stoffel works well except for one instance. I ran into a situation where the a character in the string gets encrypted into a ''\0'' character. This will cause the string "strInputString" to end prematurely.... or the computer will think it ends prematurely even though there is more to the string. Does anyone know how to handle this situation??

I was thinking of handling the special case where the encrypted character is a ''\0'' but how would you handle it nicely??

thanks.
daerid
daerid
pass in the length of the string to be encoded.

that way, you can encode binary data too!
daerid@gmail.com
alexmoura
alexmoura
quote:
Original post by Alpha_ProgDes
char* strInputString; <- redundant variable declaration.

char* <- implies a string
str <- implies a string
InputString <- implies you''re inputting a string

see what we mean?


I''d say that the str and String are redundant (as in, only one is required), but char * is not going to be visible when actually using the variable (you should still keep one of them if you''re interested in knowing the variable type futher down your code)

You know, I never wanted to be a programmer...

Alexandre Moura

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.