Hello, i have created a Xor function for a DLL i am developing. I have a problem with this function as the decoded text isn't matching its original state and i am unsure as to the cause of this, please could you help me?

#define export __declspec (dllexport)
#include <stdio.h>

long filesize(FILE *f)
{
	long fs;
	fseek(f,0L,SEEK_END);
	fs = ftell(f);
	fseek(f,0L,SEEK_SET);
	return fs;
}

export double crypto(char *fn, char *sb, double ss)
{
	FILE *f;
	unsigned char fb[BUFSIZ];
	unsigned int bp, sp = 0;
	long rp, wp;
	size_t bs;
	if ((f = fopen(fn,"rb+")) == NULL)
	{
		return 0;
	}
	rp = wp = ftell(f);
	while ((bs = fread(fb, 1, BUFSIZ, f)) != 0)
	{
		rp = ftell(f);
	  for ( bp = 0; bp < bs; ++bp )
		{
			fb[bp] ^= sb[sp++];
			if ( sp == ss )
			{
				sp = 0;
			}
		}
		fseek(f,wp,SEEK_SET);
		fwrite(fb, 1, bs, f);
		fflush(f);
		wp = ftell(f);
		fseek(f,rp,SEEK_SET);
	}
	fclose(f);
	return 1;
}

I have managed to get all the other functions working but the decoding process doesnt seem to be working quite right and it seems to be crashing applications that call the DLL with larger files.

Best Wishes,
Nathaniel Blackburn

Probably not your issue, but why are you using double for ss . You should never really be checking a double with the == operator (unless checking for 0 I suppose). Floating point numbers are prone to small rounding errors.

Please define original state? Are you attempting to decrypt previously encrypted data? If so please post the encryption code or at least a copy of the data.

Please define original state? Are you attempting to decrypt previously encrypted data? If so please post the encryption code or at least a copy of the data.

XOR encryption encrypts and decrypts using the same algorithm, even when using a key.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.