# VBForums CodeBank > CodeBank - C++ >  Ben Tiny Encryption.

## BenJones

hi, this is some code I been working on since yesterday to encrypt files. I posting here to find out how I can improve it or make it stronger, it still uses xor like in simple encryption, but I made a table array to hold the password unto 256 bytes, and then done some basic maths on the values. to make it seem random. I did this to avoid any repeated runs of the same, I am only starting to get into encryption so this is one of my first try's. anyway tell us what you think.



```
#include <iostream>
#include <time.h>
using namespace std;

unsigned int pbox[256];

void Abort(int id, char*msg){

	//Bens Tiny Encription.
	printf("BTE By Benjamin George\nEasy encrypt your private data.\n\n");

	switch(id){
		case 0:
			printf("Syntax: FileToEncrypt OutputFile Password\n");
			printf(" bcrypt.exe private.txt private.enc password\n");
			break;
		case 1:
			printf("Error reading source filename:\n ");
			printf(msg);
			break;
		case 2:
			printf("Error writeing output filename:\n ");
			printf(msg);
			break;
		case 3:
			printf("Password is required.");
			break;
	}
	printf("\n");
}

void PrepPws(char *pws){
	int I = 0;
	char c = 0;
	unsigned phash = 0;

	//Build hash sum from original password.
	while(I < 256){
		//Get single char from password.
		c = pws[I % strlen(pws)];
		phash+= (rand() + c) % 256;
		//INC Counter.
		I++;
	}

	I = 0;

	//Set random starting seed.
	srand(phash);

	//Here were we randomize our password so it harder and not so repeated.
	while(I < 256){
		//Get simgle char value.
		c = pws[I % strlen(pws)];
		//Get a nise large random number.
		phash =(phash + rand() + c);
		//Store the value, remmber we must mod by 256, so we get a vaild char code.
		pbox[I] = (phash % 256);
		//INC Counter.
		I++;
	}
}

int main(int argc, char *argv[]){
	FILE *fin,*fout;
	unsigned int bCount = 0;
	unsigned char ch = 0;
	unsigned pValueIdx = 0;
	char bXor = 0;

	if(argc != 4){
		Abort(0,"");
		return 0;
	}

	//Open source input file.
	fin = fopen(argv[1],"rb");
	//Check if the file was opened.
	if(!fin){
		Abort(1,argv[1]);
		return 1;
	}

	//Create output source file.
	fout = fopen(argv[2],"wb");

	//Check if output file was created.
	if(!fout){
		Abort(2,argv[2]);
		return 2;
	}

	//Get the key.
	if(strlen(argv[3]) == 0){
		Abort(3,"");
		return 3;
	}

	//Prep the password.
	PrepPws(argv[3]);

	while(!feof(fin)){
		//Read char.
		ch = fgetc(fin);
		//While not end of file write data to output source file.
		if(!feof(fin)){
			pValueIdx = (rand() % 256);
			//Crypt the byte with bXor
			bXor = (pbox[pValueIdx] ^ ch);
			//Write byte to file.
			fputc(bXor,fout);
		}
		//File byte counter.
		bCount++;
	}

	//Clear pbox.
	for(int i = 0;i<256;i++){
		//Clear each slot.
		pbox[i] = 0;
	}

	//Close output file.
	fclose(fout);
	//Close input source file.
	fclose(fin);

	return 4;
}
```

----------

