Hello Everyone.

i have created a XOR Encryption libary in c...well tried with some help. But there is something wrong with my coding as when i try and decrypt a file, it doesnt always decrypt as plain text as some charfacters seem malformed. Please could you help me fix this problem and possibly update this to c++, thanks alot people, you rock.

Requests...

Fix the main problem below
Fix mistakes in the code
Make necessary improvements to the code
Revise code and update to C++ if possible

Problem...

Original File
[B][I]This is a test of the encryption libary.[/I][/B]

Encrypted File
[B][I]'ᜈṏ䌀倀攑琖漠漛ฤ⁥牣灹楴湯氠扩牡⹹[/I][/B]

Decrypted File
[B][I]This is a test oAÀòXe encryp`Æ<ldÃø`ary.[/I][/B]

The Functions...

crypt - This will crypt a given file with a given seed string.
cryptf - Similar to crypt, only you provide a seed File instead of a seed string.
crypto - This will crypt a given file with a given seed string.
cryptof - This will crypt a given file with a given seed file

The Source Code...

#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 crypt(char *rn, char *wn, char *sb, double ss)
{
	FILE *rf, *wf;
	unsigned char fb[BUFSIZ];
	unsigned int bp, sp = 0;
	size_t bs;
	if ((rf = fopen(rn,"rb")) == NULL) { return 0; }
	if ((wf = fopen(wn,"wb")) == NULL) { return 0; }
	while ((bs = fread(fb, 1, BUFSIZ, rf)) != 0)
	{
		for ( bp = 0; bp < bs; ++bp )
		{
			fb[bp] ^= sb[sp++];
			if ( sp == ss )
			{
				sp = 0;
			}
		}
		fwrite(fb, 1, bs, wf);
	}
	fclose(wf);
	fclose(rf);
	return 1;
}

export double cryptf(char *fn, char *tn, char *sn)
{
	FILE *f;
	unsigned char *sb;
	double ss;
	if ((f = fopen(sn,"rb")) == NULL) { return 0; }
	ss = filesize(f);
	if ((sb = (char *) malloc(sizeof(char) * ss)) == NULL) { return 0; }
	fread(sb,ss,1,f);
	fclose(f);
	double r = crypt(fn,tn,sb,ss);
	free(sb);
	return r;
}

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;
}

export double cryptof(char *fn, char *sn)
{
	FILE *f;
	char *sb;
	double ss;
	if ((f = fopen(sn,"rb")) == NULL) { return 0; }
	ss = filesize(f);
	if ((sb = (char *) malloc(sizeof(char) * ss)) == NULL) { return 0; }
	fread(sb,ss,1,f);
	fclose(f);
	double r = crypto(fn,sb,ss);
	free(sb);
	return r;
}

Kind Regards,
Nathaniel Blackburn

Recommended Answers

All 4 Replies

Could you tell how you tested each function during writing the code?

We need proof that you did the code, so give us brief pseudo code or add plenty of comments.

Surely it is pure chance that http://www.daniweb.com/software-development/c/threads/331884/1417642#post1417642 looks quite similar to your code. You seemed to have forgotten the lines 1 and 2, though.

That is my post too if you look at the author its me, but that was when i was trying to create the code, but now it semi works with some minor problems which is why im asking for help because im really stuck.

By seed string and/or seed file, do you mean that you generate a hash from that and then use the hash to encrypt/decrypt the plain-text data?

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.