I have this encryption code, it encrypt the file but doesnt want to decrypt it: it gives a segmentation fault error.
By the way i am using linux...
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
int n, c, l;
void gen_code(string word1, string word2)
{
char ifr, isr, itr;
ifr = word1[rand() % word1.size()];
isr = word2[rand() % word2.size()];
cout<<"[**] Computing ( n )..."<<endl;
n = (ifr - 1) * (isr - 1);
cout<<"[=] ( n ) = "<<n<<endl;
cout<<"[**] Computing ( c )..."<<endl;
c = (n % (n-3));
cout<<"[=] ( c ) = "<<c<<endl;
}
void encrypt(string infile, string outfile)
{
int j;
string convline, encline, line;
ifstream ifile(infile.c_str());
ofstream ofile;
ofile.open(outfile.c_str(), ios::app);
while(getline(ifile, line))
{
j = line.size();
for(int i = 0; i < line.size(); i++)
{
encline += line[i] - (n + 1) % c;
//convline += encline[j-1];
ofile<<encline;
//j--;
}
}
}
void decrypt(string infile, string outfile)
{
int j, N, C, L;
string line, encline, convline;
cout<<"Enter ( n ): ";
cin>>N;
cout<<"Enter ( C ): ";
cin>>C;
ifstream ifile(infile.c_str());
ofstream ofile;
ofile.open(outfile.c_str(), ios::app);
while(getline(ifile, line))
{
j = line.size();
for(int i = 0; i < line.size(); i++)
{
encline += line[i] + (N + 1) % C;
//convline += encline[j-1];
ofile<<encline;
//j--;
}
}
}
int main()
{
while(1){
string temp, input;
stringstream ss;
vector<string>vstring;
cout<<"\n>: ";
getline(cin, input);
ss << input;
while(ss >> temp)
{
vstring.push_back(temp);
}
if(vstring[0] == "gencode" && vstring[1] != "" && vstring[2] != "")
gen_code(vstring[1], vstring[2]);
else if(vstring[0] == "encrypt" && vstring[1] != "" && vstring[2] != "")
encrypt(vstring[1], vstring[2]);
else if(vstring[0] == "decrypt" && vstring[1] != "" && vstring[2] != "")
decrypt(vstring[1], vstring[2]);
vstring.clear();
}
}
Please tell me what is wrong and how to fix it.
Thanks.