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