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

Edited 5 Years Ago by shadowscape: n/a

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.

Edited 5 Years Ago by shadowscape: n/a

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?

This article has been dead for over six months. Start a new discussion instead.