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.

Have you tried to run your code in the debugger to find out which line the segmentation fault happens on? Have you tried to run your code through valgrind to find out when and where you write to or read from invalid memory?

Also can you give us some sample input we can enter into your program to reproduce your error?

input:

    gencode word1 word2

it then computes 'n' and 'c'

    encrypt /home/user/Desktop/File.txt /home/user/Desktop/SecondFile.txt

The encryption goes fine...

     decrypt /home/user/Desktop/SecondFile.txt /home/user/Desktop/ThirdFile.txt

It creates the ThirdFile.txt but gives the segment error and doesnt write anything into the file...

Well, if the error is caused by the two lines that are commented out (66 and 68) then I am not surprised. Line 66 is for sure going to cause a seg-fault because the string encline is not yet big enough (say, at the first iteration) to have an element at index j-1.

Actually for me the decryption seemed to work fine-ish (the third.txt file contained the original file's contents repeated multiple times). However after finishing decrypting, it goes back to main and causes a segmentation fault on line 90.

This happens because you mix getline and operator>>. After decrypts uses cin >> to read n and c, there will still be a newline character in the stream. Then when main calls getline, getline gives you an empty line because the first thing it sees is that newline character. So then you put that empty line in a string stream and populate vstring from that. So vstring will be empty. And when you try to access vstring[0] on line 90, you get a segmentation fault.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.