Hi I was wondering why I have an error in this program every time I try to do a long encryption. For example, encrypting an ebook or long document. I only occurs in the advanced encryption and advanced decryption functions. I'm pretty sure it has something to do with memory or access violation, according to the code, but a point in the direction to fix it would be greatly appreciated.
Thanks.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

	char path1[256], path2[256]; 
	int EnORDe;
	char a,b;
	char newline='\r\n';
	int pavalue[32];
void Sencrypt();
void Sdecrypt();
void passcode(int *pvalue);
void aencrypt(int *code);
void adecrypt(int *code);

int main ()
{
	cout << "what is the path to the text file: ";
	cin.getline(path1,257);
	cout << "where do you want the new text file: ";
	cin.getline(path2,257);
	cout << "would you like to encrypt or decrypt a file(1 or 2)"<<"\n";
	cout <<"advance encrypt (3) and advanced decrypt (4): ";
		cin>>EnORDe;
		cout<<newline;
	if(EnORDe == 1)
	{
		Sencrypt();
	}
	else if(EnORDe == 2)
	{
		Sdecrypt();
	}
	else if(EnORDe == 3)
	{
		passcode(pavalue);
		aencrypt(pavalue);
	}
	else if(EnORDe == 4)
	{
		passcode(pavalue);
		adecrypt(pavalue);
	}
	else
	{
		cout<<"ERROR!!!"<<'\n';
		cout<<"Please enter one of the numbers listed"<<'\n';
	}
	
system("PAUSE");

return 0;
}
//simple encryption
void Sencrypt()
	{

	ifstream text(path1);
	a=('a'+'A'-'b')/'5';
	ofstream myfile;
  myfile.open (path2);
	while(text.get(b))
	{	
		b+=a;
		myfile <<b;
	}
	myfile.close();

	return;
	}
//simple decryption
void Sdecrypt()
{
	
	
	ifstream text(path1);
	a=('a'+'A'-'b')/'5';
	ofstream myfile;
	myfile.open (path2);
	while(text.get(b))
	{
		b-=a; 
		myfile <<b;

	}
			myfile.close();
	return;
}


//reads passcode for value of array to use
void passcode(int *pvalue)
{
	char pword[32];
	cout<< "Enter passcode(up to 32 characters): ";
	cin>>pword;
	cout<<pword<<"\n";
		
	int i;

	for(i=0; pword[i]; i++)
{	

	// Lower Case letters
		if(pword[i]=='a' ||pword[i]== 'p')
	{	
			pvalue[i]=0;
		}
		else if(pword[i]=='b' ||pword[i]== 'q')
		{	
			pvalue[i]=1;
		}
		else if(pword[i]=='c' ||pword[i]== 'r')
		{	
			pvalue[i]=2;
		}
		else if(pword[i]=='d' ||pword[i]== 's')
		{	
			pvalue[i]=3;
		}
		else if(pword[i]=='e' ||pword[i]== 't')
		{	
			pvalue[i]=4;
		}
		else if(pword[i]=='f' ||pword[i]== 'u')
		{	
			pvalue[i]=5;
		}
		else if(pword[i]=='g' ||pword[i]== 'v')
		{	
			pvalue[i]=6;
		}
		else if(pword[i]== 'h' ||pword[i]== 'w')
		{	
			pvalue[i]=7;
		}
		else if(pword[i]=='i' ||pword[i]== 'x')
		{	
			pvalue[i]=8;
		}
		else if(pword[i]=='j' ||pword[i]== 'y')
		{	
			pvalue[i]=9;
		}
		else if(pword[i]=='k' ||pword[i]== 'z')
		{	
			pvalue[i]=10;
		}
		else if(pword[i]=='l')
		{	
			pvalue[i]=11;
		}
		else if(pword[i]=='m')
		{	
			pvalue[i]=12;
		}
		else if(pword[i]=='n')
		{	
			pvalue[i]=13;
		}
		else if(pword[i]=='o')
		{	
			pvalue[i]=14;
		}


		// Capital letters
		else if(pword[i]=='A' ||pword[i]== 'B')
	{	
			pvalue[i]=15;
		}
		else if(pword[i]=='C' ||pword[i]== 'D')
		{	
			pvalue[i]=16;
		}
		else if(pword[i]=='E' ||pword[i]== 'F')
		{	
			pvalue[i]=17;
		}
		else if(pword[i]=='G' ||pword[i]== 'H')
		{	
			pvalue[i]=18;
		}
		else if(pword[i]=='I' ||pword[i]== 'J')
		{	
			pvalue[i]=19;
		}
		else if(pword[i]=='K' ||pword[i]== 'L')
		{	
			pvalue[i]=20;
		}
		else if(pword[i]=='M' ||pword[i]== 'N')
		{	
			pvalue[i]=21;
		}
		else if(pword[i]== 'O' ||pword[i]== 'P')
		{	
			pvalue[i]=22;
		}
		else if(pword[i]=='Q' ||pword[i]== 'R')
		{	
			pvalue[i]=23;
		}
		else if(pword[i]=='S' ||pword[i]== 'T')
		{	
			pvalue[i]=24;
		}
		else if(pword[i]=='U' ||pword[i]== 'V')
		{	
			pvalue[i]=25;
		}
		else if(pword[i]=='W')
		{	
			pvalue[i]=26;
		}
		else if(pword[i]=='X')
		{	
			pvalue[i]=27;
		}
		else if(pword[i]=='Y')
		{	
			pvalue[i]=28;
		}
		else if(pword[i]=='Z')
		{	
			pvalue[i]=29;
		}
		else if(pword[i]==' ')
		{	
			pvalue[i]=30;
		}

		//NUMBERS
		else
		{	
			pvalue[i]=31;
		}

	}
 return;
}

//advanced encryption
void  aencrypt(int *code) 
{
	char codearray[32]={'j','b','m','o','O','s','e','t','i','w','z','Z','6','2',
		'7','x','p','K','1','d','r','M','n','k','q','I','H','v','T','U','0'};
	int i=0;
	ifstream text(path1);
	ofstream myfile;
	myfile.open (path2);
	while(text.get(b))
	{
		if(i%2==1)
		{b+=codearray[pavalue[i]];}
		else if(i%2==0)
		{b-=codearray[pavalue[i]];}
		myfile <<b;
		if(i>=sizeof(codearray))
		{i=0;}
		i++;
	}
	myfile.close();
	return;
}

//advanced decryption
void  adecrypt(int *code) 
{
	char codearray[32]={'j','b','m','o','O','s','e','t','i','w','z','Z','6','2',
		'7','x','p','K','1','d','r','M','n','k','q','I','H','v','T','U','0'};
	int i=0;
	ifstream text(path1);
	ofstream myfile;
	myfile.open (path2);
	while(text.get(b))
	{
		if(i%2==1)
		{b-=codearray[pavalue[i]];}
		else if(i%2==0)
		{b+=codearray[pavalue[i]];}
		myfile <<b;
		if(i>=sizeof(codearray))
		{i=0;}
		i++;
	}
	myfile.close();
	return;
}

You need to have room for '\0'. Change

int EnORDe;
char a,b;
char newline='\n';  // <--- Changed, was getting a warning can't have two chars set to one char
int pavalue[33];   // <--- 32 to 33 <--------------LOOK HERE
//---------------
//reads passcode for value of array to use
void passcode(int *pvalue)
{
  char pword[33] = {0};  // <------ 32 to 33 <----- LOOK HERE EDIT added the = {0} looked like it worked
  cout<< "Enter passcode(up to 32 characters): ";
  cin>>pword;
  cout<<pword<<"\n";

  int i;
  for(i=0; i< strlen(pword); i++) // <---- use strlen<--------------LOOK HERE

I think that is all I changed. Didn't crash and it did work. I only tested option 3 and 4

Edited 4 Years Ago by histrungalot: Added the pword init to zero

You need to have room for '\0'. Change

int EnORDe;
char a,b;
char newline='\n';  // <--- Changed, was getting a warning can't have two chars set to one char
int pavalue[33];   // <--- 32 to 33 <--------------LOOK HERE
//---------------
//reads passcode for value of array to use
void passcode(int *pvalue)
{
  char pword[33] = {0};  // <------ 32 to 33 <----- LOOK HERE EDIT added the = {0} looked like it worked
  cout<< "Enter passcode(up to 32 characters): ";
  cin>>pword;
  cout<<pword<<"\n";

  int i;
  for(i=0; i< strlen(pword); i++) // <---- use strlen<--------------LOOK HERE

I think that is all I changed. Didn't crash and it did work. I only tested option 3 and 4

While that does fix some warnings and such it doesn't fix the major problem I had. I was trying to encrypt a larger file like a book and while it looks like the advanced encryption works as the files come out with relatively similar size the advanced decryption only decodes about the first few lines and I that's what I'm trying to fix. Did you not have that problem?
Thanks for the help so far though. :)

After encryption and decryption, it's hard to read back.. what you do is that for each line, encode/decode using base64 as if u don't, sometimes the characters won't be read back exactly how they were..

Had this happen to me before.. so I just used base64 or hexadecimal for each char.. and it fixed it..

Dunno if that'll help u though.

With the changes I made it worked for me. Here is a run with a 454 MB tar file that I encrypted and decrypted, then ran tar tvf to see if there were error and not errors. Is ~1/2 Gig as large as your file?

$ ls -l my*.tar
-rw-r--r--@ 1 xx  xx  454612480 Dec 19  2010 mysql-5.5.8-osx10.6-x86_64.tar
$ tar tvf mysql-5.5.8-osx10.6-x86_64.tar | wc -l
    6577
$ ../code/c++/a.out
what is the path to the text file: mysql-5.5.8-osx10.6-x86_64.tar
where do you want the new text file: t.en
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 3

Enter passcode(up to 32 characters): 12345678901234567890123456789012
12345678901234567890123456789012
$ tar tvf t.en
tar: Unrecognized archive format: Inappropriate file type or format
tar: Error exit delayed from previous errors.
$ ../code/c++/a.out
what is the path to the text file: t.en
where do you want the new text file: mysql.tar
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 4

Enter passcode(up to 32 characters): 12345678901234567890123456789012
12345678901234567890123456789012
$ tar tvf mysql.tar | wc -l
    6577
$

Edited 4 Years Ago by histrungalot: n/a

The reason that the program is working for you is because essentially you aren't using the advanced encryption because numbers all have the same value it's just added or subtracted if you try with another password using letters it doesn't work. At least I think that the problem has something to do with the different values caused by the letters.

I do think I see one more issue

void  aencrypt(int *code)
// ...
       i++;     // <--- Should inc it first then check.         
       if(i>=sizeof(codearray))
         {i=0;}
// ....
void  adecrypt(int *code)
       i++;     // <--- Should inc it first then check.         
       if(i>=sizeof(codearray))
         {i=0;}
$ ../code/c++/a.out
what is the path to the text file: mysql-5.5.8-osx10.6-x86_64.tar
where do you want the new text file: t.en
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 3

Enter passcode(up to 32 characters): QAWSEDFFgthyjuij445&^$#dd5
QAWSEDFFgthyjuij445&^$#dd5
$ tar tvf t.en
tar: Unrecognized archive format: Inappropriate file type or format
tar: Error exit delayed from previous errors.
$ ../code/c++/a.out
what is the path to the text file: t.en
where do you want the new text file: my.tar
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 4

Enter passcode(up to 32 characters): QAWSEDFFgthyjuij445&^$#dd5
QAWSEDFFgthyjuij445&^$#dd5
$ tar tvf my.tar | wc -l
    6577

If this works you might want to use a getline with a std::string so you can check the size against the limit of 32. Currently if someone enters 55 chars, it would be overwriting memory on the stack which would be bad.

Edited 4 Years Ago by histrungalot: n/a

Sorry I'm still having the same problem. I've made all the changes you've made so far but unfortunately it hasn't fix the fact the long files are missing almost the entire file by the time I decrypt them. I get four lines out of the .txt book I'm testing this on. I attached the text file I'm testing maybe you'll see what I mean if you try encrypting it and decrypting it.
Sorry for causing so much trouble.

Attachments
CHAPTER ONE 
Warlock
The demon howled its outrage.
Amirantha, Warlock of the Satumbria, reeled backwards from the unexpected explosion of mystic energy hurled at him. Had his protective wards not been firmly established, he would have died instantly. The demon responsible was powerful enough to force through the barrier and slam the magic user hard against the cave wall behind him. The blow Amirantha took on the back of the head was going to raise a nasty bump.
Demons always carried a large amount of mystic energies, enough to destroy any unprepared mortal standing nearby as the monsters entered this plane of reality. It was one of the reasons for erecting wards, beyond merely confining the demon to a specific location. This one had arrived with a much more impressive explosion than the Warlock anticipated, and had surprised him.
Amirantha incanted a single word, a collection of otherwise meaningless syllables that together formed a key, a word of power that activated a much more complicated enchantment; a trick taught to him years before that had often meant the difference between controlling a summoned demon effectively and dismemberment at its hands. The word strengthened the ward spell that now confined the creature.
Amirantha regained his feet as the demon continued to howl at discovering itself summoned and confined. Experience had taught the Warlock that demons rarely objected to being summoned as they found this world easy to plunder, but they hated being trapped and controlled. Their hate was the one thing that made Amiranthas area of study problematic; his subjects kept trying to kill him.
He took a deep breath to calm himself and studied the enraged conjuration. The demon was not a type he recognized, though obviously a battle demon of some sort. Amirantha knew more about demons and their nature than any mortal on Midkemia, but still possessed only a tenth of the understanding he wished for. This specimen was new to him. He did not have exhaustive knowledge of every demon in the Fifth Circle, but he recognized its basic type: massive upper torso, roughly human in design, with a bulls head, or at least something that resembled a bovine; long, forward-arching horns, giving weight to its minotaur-like appearance. As he began to conjure a spell designed to immobilize any demon, Amirantha wondered if such a monster had been the basis for the ancient myth of the Minotaur.
Its legs were almost goat-like, but there anything remotely familiar about the creature ended. Its body was covered in some black substance up to its waist, though it was no wool, hair, or fur that Amirantha recognized. Its upper body looked like it was made from black leather, but slick and shiny, as if its skin had been tanned, dyed, and highly polished. Its horns were blood red, and its eyes burned like hot coals.
From the howls shaking the cave, Amirantha could tell that the demons disposition was getting nastier by the second. The creature even looked on the verge of rending its way through wards that should be impenetrable, though Amirantha knew better than to place too much stock in the world should when a demon was involved.
He finished strengthening his spell of confinement and saw the demon step back a moment, shudder, then return to battering the wards, accompanying its renewed efforts with even louder bellowing.
Amiranthas eyes widened slightly, his only outward concession to surprise. The demon had just shrugged off a spell designed to immobilize any conjured entity. Looking at the raging demon, the Warlock of Satumbria stroked his chin whiskers and considered what he observed. He was a vain man by any measure, and had his servant trim his beard and hair weekly, knowing exactly how it should look each time. His receding hairline had caused him to let his dark hair fall to his shoulders, and his dark brows and pointed chin beard gave him an appropriate cast for his calling in life: a summoner of demons. Or at least made him look the part for those willing to pay gold for his services.
Adjusting his purple robe, covered with fine silver needlework at the collar and upon the sleeves, he muttered a reliable invocation and watched. The demon should have instantly knelt in abject obedience, but instead he could sense the summoned creatures rage intensifying at the command. Amirantha sighed in a mixture of frustration and confusion, and wondered what he had conjured this time.
Ignoring the ringing in his ears, the Warlock reached into a large belt pouch. He had sewn this pouch years ago, patiendy weaving magic into the threads under the supervision of a master artificer named Leychona, in the great City of the Serpent River, his one and only attempt at fabricating magic cloth. He had been pleased with the results, the confining bag let him carry many stones of power without provoking disastrous consequences. He was proud of the needlework, but had found the entire process so tedious and exasperating, he now paid artificers and tailors to fashion what he needed in exchange for his skills or gold.
Amiranthas finger rubbed lightly against a series of embroidered knots, each indicating a pocket he had fashioned. Swiftly, he found the one he sought and withdrew the stone he had prepared for a time such as this. Holding it aloft, he incanted a spell that drew forth the power stored in the stone and directed it to the hastily reinforced barrier. As he did, he felt the shock reverberating through the ward as the demon hurled itself against the mystic defence.
Then the creature paused, and looked at the space in the air where the barrier stood as if it could see it. Pulling back its massive right fist it unleashed a blow that could shatter a bull-hide shield. Amirantha imagined that he felt the shock from it travel through the air to strike him. Then the demon struck the wards even harder, and Amirantha raised his hand to reinforce the barrier with even more power. To his astonishment, this time he could feel the demons energy translated into a blow that ran up his arm. He stepped back, until he stood hard against the wall. What do I do now? he muttered absently.
Again the demon hurled itself at the barrier and Amirantha, Warlock of the Satumbria, decided it was going to get through. Pushing aside a sudden urge to laugh - the unexpected and dangerous often affected him this way - he drew another object from his belt pouch and smashed it on the floor.
A noxious gas erupted from its ruin and as it spread, Amirantha fled from the deep cave in which he had conjured the monster. It was a summoning area he had especially prepared for this ritual, protected by multiple wards and other safeguards he had erected against such a mishap. He hurried along a narrow tunnel, muttering, What next?
Reaching a large open cavern, closer to the entrance of the stone warren, he cursed himself for a fool. All of his most powerful items had been stored in the smaller cave. He had been so surprised by the conjuration, that he had left them on the floor. He had thought himself ready for any eventuality surrounding demon summoning; it never occurred to him that one he hadnt summoned might appear unexpectedly.
Shaking his head at his own stupidity, he stopped. He had at least stored a lantern here; although such forethought had simply been intended to indicate the way out, rather than in anticipation that he might be forced to flee for his life, having abandoned his other lantern. Muttering to himself, he said, Sometimes I wish I was as clever as I claim to be.
Amirantha turned back towards the tunnel, realizing that if he didnt stop the demon here, the creature would be free to choose from exits. Not only would that be bad for anyone living within the demons reach, almost ten thousand people by the last census, it would also prove disastrous for Amiranthas reputation.
The Governor of Landa waited for him near a particular cave mouth, accompanied by a sizeable retinue of soldiers, but nothing that could stop this monster should it come their way. Not only would the Maharajahs Court look down upon an itinerant Warlock responsible for the disembowelment of a regional governor, he was almost certainly not going to be paid for performing this banishment.
Pulling a long wand of ash from his belt, the Warlock readied himself. The device had been commissioned from the finest wand maker in the Kingdom of Muboya, and was capable of seven effective theatrical stunts, each designed to illicit oohs and aahs of wonder from onlookers. But it also possessed four very powerful enchantments that could inflict significant damage should the need arise. Amirantha was fairly certain the need had arisen.
He was greeted by the stench of the gas moving through the corridor from the summoning cave. It was designed to weaken and eventually incapacitate demons, and was not at all pleasant for humans to inhale. He knew that probably meant the demon was through the wards and coming towards him. Then Amirantha winced.
It wasnt the odour that made him shudder, but a sudden cave-rattling sound; a combination of tones and vibrations that made his heart jump and cringe at the same time. The angry shriek made his skin crawl, as if he were listening to a smith sharpen a sword on a turning wheel. If nothing else, the Governor of Lanada was receiving a better performance than the one Amirantha had originally planned for him.
Then the demon came straight at him.
A voice from behind Amirantha said, Need any help?
It would be appreciated, the Warlock said to Brandos. His companion had been waiting outside the cave mouth, reinforcement for eventualities such as this, and to make sure that the Governor became curious enough to send in his guards to help the Warlock banish the demon.
Amirantha gripped his ornately carved wand and spoke a single word in a language known to very few men. A searing burst of heat washed over the two men as a massive fireball exploded away from them through the tunnel, sweeping over the demon and forcing it back.
Im going to need a few moments to banish it.
The old fighter was still powerful, though nearing fifty years of age, and he had more experie

Works for me. See if there is something different.

$ ls -tlr doc1.txt 
-rw-r--r--@ 1 x  x  662210 Feb 29 16:58 doc1.txt
$ ../code/c++/a.out
what is the path to the text file: doc1.txt
where do you want the new text file: doc.enc
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 3

Enter passcode(up to 32 characters): QAWSedwsedRFTHGYU5678
QAWSedwsedRFTHGYU5678
$ ../code/c++/a.out
what is the path to the text file: doc.enc
where do you want the new text file: doc1.denc.txt
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 4

Enter passcode(up to 32 characters): QAWSedwsedRFTHGYU5678
QAWSedwsedRFTHGYU5678
$ wc -l doc1.txt 
    3068 doc1.txt
$ wc -l doc1.denc.txt 
    3068 doc1.denc.txt

Here is your code

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

	char path1[256], path2[256]; 
	int EnORDe;
	char a,b;
	char newline='\n';
	int pavalue[33];
void Sencrypt();
void Sdecrypt();
void passcode(int *pvalue);
void aencrypt(int *code);
void adecrypt(int *code);

int main ()
{
	cout << "what is the path to the text file: ";
	cin.getline(path1,257);
	cout << "where do you want the new text file: ";
	cin.getline(path2,257);
	cout << "would you like to encrypt or decrypt a file(1 or 2)"<<"\n";
	cout <<"advance encrypt (3) and advanced decrypt (4): ";
		cin>>EnORDe;
		cout<<newline;
	if(EnORDe == 1)
	{
		Sencrypt();
	}
	else if(EnORDe == 2)
	{
		Sdecrypt();
	}
	else if(EnORDe == 3)
	{
		passcode(pavalue);
		aencrypt(pavalue);
	}
	else if(EnORDe == 4)
	{
		passcode(pavalue);
		adecrypt(pavalue);
	}
	else
	{
		cout<<"ERROR!!!"<<'\n';
		cout<<"Please enter one of the numbers listed"<<'\n';
	}
	
system("PAUSE");

return 0;
}
//simple encryption
void Sencrypt()
	{

	ifstream text(path1);
	a=('a'+'A'-'b')/'5';
	ofstream myfile;
  myfile.open (path2);
	while(text.get(b))
	{	
		b+=a;
		myfile <<b;
	}
	myfile.close();

	return;
	}
//simple decryption
void Sdecrypt()
{
	
	
	ifstream text(path1);
	a=('a'+'A'-'b')/'5';
	ofstream myfile;
	myfile.open (path2);
	while(text.get(b))
	{
		b-=a; 
		myfile <<b;

	}
			myfile.close();
	return;
}


//reads passcode for value of array to use
void passcode(int *pvalue)
{
	char pword[33] = {0};
	cout<< "Enter passcode(up to 32 characters): ";
	cin>>pword;
	cout<<pword<<"\n";
		
	int i;

	for(i=0; i< strlen(pword); i++)
{	

	// Lower Case letters
		if(pword[i]=='a' ||pword[i]== 'p')
	{	
			pvalue[i]=0;
		}
		else if(pword[i]=='b' ||pword[i]== 'q')
		{	
			pvalue[i]=1;
		}
		else if(pword[i]=='c' ||pword[i]== 'r')
		{	
			pvalue[i]=2;
		}
		else if(pword[i]=='d' ||pword[i]== 's')
		{	
			pvalue[i]=3;
		}
		else if(pword[i]=='e' ||pword[i]== 't')
		{	
			pvalue[i]=4;
		}
		else if(pword[i]=='f' ||pword[i]== 'u')
		{	
			pvalue[i]=5;
		}
		else if(pword[i]=='g' ||pword[i]== 'v')
		{	
			pvalue[i]=6;
		}
		else if(pword[i]== 'h' ||pword[i]== 'w')
		{	
			pvalue[i]=7;
		}
		else if(pword[i]=='i' ||pword[i]== 'x')
		{	
			pvalue[i]=8;
		}
		else if(pword[i]=='j' ||pword[i]== 'y')
		{	
			pvalue[i]=9;
		}
		else if(pword[i]=='k' ||pword[i]== 'z')
		{	
			pvalue[i]=10;
		}
		else if(pword[i]=='l')
		{	
			pvalue[i]=11;
		}
		else if(pword[i]=='m')
		{	
			pvalue[i]=12;
		}
		else if(pword[i]=='n')
		{	
			pvalue[i]=13;
		}
		else if(pword[i]=='o')
		{	
			pvalue[i]=14;
		}


		// Capital letters
		else if(pword[i]=='A' ||pword[i]== 'B')
	{	
			pvalue[i]=15;
		}
		else if(pword[i]=='C' ||pword[i]== 'D')
		{	
			pvalue[i]=16;
		}
		else if(pword[i]=='E' ||pword[i]== 'F')
		{	
			pvalue[i]=17;
		}
		else if(pword[i]=='G' ||pword[i]== 'H')
		{	
			pvalue[i]=18;
		}
		else if(pword[i]=='I' ||pword[i]== 'J')
		{	
			pvalue[i]=19;
		}
		else if(pword[i]=='K' ||pword[i]== 'L')
		{	
			pvalue[i]=20;
		}
		else if(pword[i]=='M' ||pword[i]== 'N')
		{	
			pvalue[i]=21;
		}
		else if(pword[i]== 'O' ||pword[i]== 'P')
		{	
			pvalue[i]=22;
		}
		else if(pword[i]=='Q' ||pword[i]== 'R')
		{	
			pvalue[i]=23;
		}
		else if(pword[i]=='S' ||pword[i]== 'T')
		{	
			pvalue[i]=24;
		}
		else if(pword[i]=='U' ||pword[i]== 'V')
		{	
			pvalue[i]=25;
		}
		else if(pword[i]=='W')
		{	
			pvalue[i]=26;
		}
		else if(pword[i]=='X')
		{	
			pvalue[i]=27;
		}
		else if(pword[i]=='Y')
		{	
			pvalue[i]=28;
		}
		else if(pword[i]=='Z')
		{	
			pvalue[i]=29;
		}
		else if(pword[i]==' ')
		{	
			pvalue[i]=30;
		}

		//NUMBERS
		else
		{	
			pvalue[i]=31;
		}

	}
 return;
}

//advanced encryption
void  aencrypt(int *code) 
{
	char codearray[32]={'j','b','m','o','O','s','e','t','i','w','z','Z','6','2',
		'7','x','p','K','1','d','r','M','n','k','q','I','H','v','T','U','0'};
	int i=0;
	ifstream text(path1);
	ofstream myfile;
	myfile.open (path2);
	while(text.get(b))
	{
		if(i%2==1)
		{b+=codearray[pavalue[i]];}
		else if(i%2==0)
		{b-=codearray[pavalue[i]];}
		myfile <<b;
		i++;
		if(i>=sizeof(codearray))
		{i=0;}
	}
	myfile.close();
	return;
}

//advanced decryption
void  adecrypt(int *code) 
{
	char codearray[32]={'j','b','m','o','O','s','e','t','i','w','z','Z','6','2',
		'7','x','p','K','1','d','r','M','n','k','q','I','H','v','T','U','0'};
	int i=0;
	ifstream text(path1);
	ofstream myfile;
	myfile.open (path2);
	while(text.get(b))
	{
		if(i%2==1)
		{b-=codearray[pavalue[i]];}
		else if(i%2==0)
		{b+=codearray[pavalue[i]];}
		myfile <<b;
		i++;
		if(i>=sizeof(codearray))
		{i=0;}
	}
	myfile.close();
	return;
}

My code is the exact same but when I open the decrypted file only 4 lines of text are in there instead of the entire document.

Run it in a debugger or put some print statements in the code to see when it stops reading. Does the encrypt process all of the data? Put in a counter to see. And the same for the decrypt. Compare that number to the size of the file, they should match.

I've found that the problem doesn't lie in the code it actually works for me if I switch the decryption and encryption method for my txt file but that just means their is a different problem not having to do with the written code. I think I'm not accounting for something in the text file. it runs 659142 times with the encryption originally, before I switched the encrypt and decrypt, but only 134 times with the decryption. Since that is just changing the signs I don't think it would cause such an error maybe I'm causing a null ASCII value in the program and it causes my while(text.get(b)) to be false before the program is over? Is that at all logical?

Whatever you are doing to the data shouldn't cause while(text.get(b)) to be false before the EOF. What is the "passcode(up to 32 characters)" that you are using? I would like to test exactly what your are to see if I can get it to happen for me.
That said, what is the value at char 134 (0x86) in your encrypt file. You can use hexdump to see.

$ hexdump -C ../../Downloads/doc.enc | more
00000000  2a 98 fb b9 f2 bf e0 b4  03 8f e4 99 d4 51 64 61  |*............Qda|
00000010  c1 57 61 72 6c d9 f9 d5  a3 74 ea d2 fb 8a fa cf  |.Warl....t......|
00000020  02 e7 26 91 19 de 03 db  16 d3 b5 b4 03 a4 ef c3  |..&.............|
00000030  2c 74 72 61 67 cf c4 77  a0 ab 03 d3 08 cb 04 de  |,trag..w........|
00000040  fd d9 e4 91 08 d0 fe db  20 d2 00 6b fe 97 ef c8  |........ ..k....|
00000050  1f 65 20 53 61 de 0b d7  f8 dc ff cb c2 8a 08 cf  |.e Sa...........|
00000060  fa e4 1d d5 d1 d1 ed d2  1c e6 f6 bd f3 a4 ef ba  |................|
00000070  29 6f 6d 20 74 d2 fb 8a  0b d8 fb e2 06 cf f9 de  |)om t...........|
00000080  fa dc d8 d6 29 df [B][U]f8[/U][/B] de  24 d8 04 b9 af a0 35 74  |....)...$.....5t|

For the key I used it was 0xf8.

I used zKidMKt4 and it hangs on the word explosion right after printing the 's'. The while goes false for some reason and it then it goes to myfile.close and return. So I deleted the word "explosion" and it read until the 614th character. The program hung right before and 'i' again in the word "reality". For some reason the while is coming back false at those values. So far all I've determined is that the value from the text file has something to do with it and I would bet the value of the password.
Thanks for all the help.

Your code is working for me. Do you have enough disk space?

$ ./a.out
what is the path to the text file: doc1.txt
where do you want the new text file: doc1.en
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 3

Enter passcode(up to 32 characters): zKidMKt4
zKidMKt4

$ ./a.out
what is the path to the text file: doc1.en
where do you want the new text file: doc1.de.txt
would you like to encrypt or decrypt a file(1 or 2)
advance encrypt (3) and advanced decrypt (4): 4

Enter passcode(up to 32 characters): zKidMKt4
zKidMKt4

$ [U][B]diff doc1.txt doc1.de.txt [/B][/U]  [B]<------ No difference![/B]

$ ls -l doc1*
[B]-rw-rw-r-- 1 xx xx 662210 Mar  1 23:46 doc1.de.txt
-rw-rw-r-- 1 xx xx 662210 Mar  1 23:45 doc1.en
-rw-r--r-- 1 xx xx 662210 Mar  1 23:38 doc1.txt[/B]

I have 300+ gigabytes of disc space so that isn't the issue. I'm completely out of ideas as to what to do with it. You really have nothing wrong with it but my program hangs on "i" for some reason.

Also in the zKidMKt4 the "t" is what hangs on the 'i' in the text document.

Edited 4 Years Ago by davi0011: found something else.

Add theses to both functions right after the while loop, just to see what it prints.

cout << "aencrypt:  good(" << text.good() << ") fail("
     << text.fail() << ") badbit("
     << text.bad() << ") eofbit("
     << text.eof() << ")" << endl; 
// And
cout << "adecrypt:  good(" << text.good() << ") fail("
    << text.fail() << ") badbit(" 
    << text.bad() << ") eofbit(" 
    << text.eof() << ")" << endl;

Could you post just your aencrypt and adecrypt functions again. Just them.

Edited 4 Years Ago by histrungalot: n/a

correct me if I'm wrong but isn't that more for linux? I'm using windows 7.

Also if I place the bit checker outside of the while loop it gives me a fail(1) and .EOF(1) at the value of 134 durring decryption.

Edited 4 Years Ago by davi0011: n/a

If it is an issue with corruption of the ifstream object, for whatever reason, you can move the ifstream text off of the stack to see if it makes a difference. Just for debugging, make the ifstream text a global and see what happens. If it works then the object is getting corrupted on the stack in the decrypt function.
Try that first and if it still doesn't work then try changing file I/O to C rather than C++ iostream.

Just curuois, what is your compiler and OS?

Windows 7 and I've used vc++, devc++, and code::blocks. does the same thing in each. I'll try making the iostream a global in the next couple days due to some high school inconvenience.

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