Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

novice_programmer

Problem with my encryption program!

This topic is 5659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I decided to make a program to do with the C subset of C++. It is a simple file encryption program. It reads 1.txt in its working directory and encrypts it into 2.txt. Then it decrypts 2.txt into 3.txt. Every time I open 3.txt I see that text file has two more characters than the original. I would like to able to solve this problem because I want to use the program for encrypting other types of files like .wav or .doc. main.cpp:
#include "globals.h"
char MASK_NUMBER;

int main()
{
	srand(time(NULL));
	MASK_NUMBER = (char) rand();
	
	FILE *sourceFile;
	FILE *targetFile;

	sourceFile = fopen("1.txt", "rb");
	targetFile = fopen("2.txt", "wb");

	printf("Error code: %lu\n", Encrypt(sourceFile,
		targetFile, MASK_NUMBER));
	
	fflush(targetFile);
	
	fclose(sourceFile);
	fclose(targetFile);

	sourceFile = fopen("2.txt", "rb");
	targetFile = fopen("3.txt", "wb");
		
	printf("Error code: %lu\n", Decrypt(sourceFile, targetFile));

	fclose(sourceFile);
	fclose(targetFile);
	return 0;
}
globals.cpp
#include "globals.h"

/************************************************************
 * Encrypt/Decrypt Error Codes                              *
 *                                                          *
 * 0: Success.                                              *
 * 1: Unable to open source and/or target file.             *
 * 2: Unable to read from source file.                      *
 * 3: Unable to write to target file.                       *
 * 4: Unknown file signature                                *
 ************************************************************/

char ENCRYPTION_VERSION = (char)196;// version 0.1 


int Encrypt(FILE *source, FILE *target, char MASK)
{
	if(source == NULL || target == NULL)
		return 1;

	fwrite(&ENCRYPTION_VERSION, sizeof(ENCRYPTION_VERSION),
		1, target);

	fwrite(&MASK, sizeof(MASK), 1, target);
	if(ferror(target)) return 3;

	while(!feof(source))
	{

		char buffer;
		fread(&buffer, sizeof(char), 1, source);
		if(ferror(source)) return 2;
		
		if(buffer != EOF || buffer != NULL)
		{
			buffer = buffer ^ MASK;
			fwrite(&buffer, sizeof(char), 1, target);
		}
	};

	rewind(source);
	rewind(target);
	
	return 0;

}

int Decrypt(FILE *source, FILE *target)
{
	if(source == NULL || target == NULL)
		return 1;
	
	char signature;
	fread(&signature, sizeof(ENCRYPTION_VERSION),
		1, source);
	
	if(ferror(source)) return 2;
	if(signature != ENCRYPTION_VERSION) return 4;
	
	char header;
	fread(&header, sizeof(header), 1, source);
	

	while(!feof(source))
	{
		char buffer;
		fread(&buffer, sizeof(buffer), 1, source);
		if(ferror(source)) return 2;
		if(buffer == EOF) exit(0);

		if(buffer != EOF)
		{
			buffer = buffer ^ header;
			fwrite(&buffer, sizeof(buffer), 1, target);
		}
	};

	rewind(source);
	rewind(target);
	
	return 0;
}
globals.h
#ifndef GLOBALS_H
#define GLOBALS_H

#include <cstdlib>
#include <cstdio>
#include <ctime>


int Encrypt(FILE *source, FILE *target, char MASK);
int Decrypt(FILE *source, FILE *target);

#endif
I think the code only works on a C++ compiler (not C89 standard).

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!