I wrote a very simple encryption program, well the algorithm is simple but there is one thing I am strugling with.
Basicly after the user's key is generated the program will write it to a file.

Then when the user want to decrypt the encrypted file, the file with the key must be in the same directory as the .exe.

The program then reads the line from the file and converts the string into an int. It here where i get an error from the compiler.

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

#define FILE_KEY "your_key.dkf"

class Digi13
{
public:
    Digi13();
    void GenKey();
    void Gen13();
    void Gen26();
    void Gen39();

    int EncryptFile();
    int ToInt(char c);
    int DecryptFile();

private:
    struct KEY_13
    {
        int key[13][13];
    };

    struct KEY_26
    {
        int key[26][26];
    };

    struct KEY_39
    {
        int key[39][39];
    };

    bool KEY13;
    bool KEY26;
    bool KEY39;

    string const_back_end;
};

Digi13::Digi13()
{
    KEY13 = false;
    KEY26 = false;
    KEY39 = false;
    const_back_end = ".d13";
}

void Digi13::GenKey()
{
    string choice;

    cout<<"1 - 13 Digit (Minimum security)"<<endl;
    cout<<"2 - 26 Digit (Medium security)"<<endl;
    cout<<"3 - 39 Digit (Maximum security)"<<endl;
    cout<<"Q - Quit."<<endl;
    cout<<"\n>> ";

    getline(cin, choice);

    if(choice == "1")
    {
        Gen13();
    }
    else if(choice == "2")
    {
        Gen26();
    }
    else if(choice == "3")
    {
        Gen39();
    }
    else
    {
        cout<<"Incorect input. Please try again."<<endl;
    }
}

void Digi13::Gen13()
{
    KEY_13 k13;

    cout<<"Generating 13 key..."<<endl;

    time_t ltime;
    ctime(&ltime);

    srand(ltime);

    for(int i = 0; i <= 13; i++)
    {
        k13.key[i][i] = rand() % 13;
    }

    cout<<"Done."<<endl;
    cout<<"Your key is: ";

    for(int i = 0; i <= 13; i++)
        cout<<k13.key;

    cout<<endl;

    KEY13 = true;
}

void Digi13::Gen26()
{
    KEY_26 k26;

    cout<<"Generating 26 key..."<<endl;

    time_t ltime;
    ctime(&ltime);

    srand(ltime);

    for(int i = 0; i <= 26; i++)
    {
        k26.key[i][i] = rand() % 26;
    }

    cout<<"Your key is: ";

    for(int i = 0; i <= 26; i++)
        cout<<k26.key;

    cout<<endl;

    KEY26 = true;
}

void Digi13::Gen39()
{
    KEY_39 k39;

    cout<<"Generating 26 key..."<<endl;

    time_t ltime;
    ctime(&ltime);

    srand(ltime);

    for(int i = 0; i < 39; i++)
    {
        k39.key[i][i] = rand() % 39;
    }

    cout<<"Your key is: ";

    for(int i = 0; i <= 39; i++)
        cout<<k39.key;

    cout<<endl;

    KEY39 = true;
}

int Digi13::EncryptFile()
{
    if(KEY13 == true)
    {
        string inFileName;
        string outFileName;
        string newLine;
        string line;
        KEY_13 k13;

        cout<<"Please enter the path to the file you wish to encrypt: ";
        getline(cin, inFileName);

        outFileName = inFileName + const_back_end;

        ifstream inFile;
        inFile.open(inFileName.c_str());

        if(!inFile)
        {
            cout<<"Could not open file \'"<<inFileName<<"\' for reading."<<endl;
            return 1;
        }

        ofstream outFile;
        outFile.open(outFileName.c_str(), ios::binary | ios::ate);

        if(!outFile)
        {
            cout<<"Could not create output file."<<endl;
            return 1;
        }

        ofstream keyFile;
        keyFile.open(FILE_KEY, ios::ate | ios::binary);

        while(getline(inFile, line))
        {
            int j = 0;
            for(int i = 0; i < line.size(); i++)
            {
                newLine += line[i] * k13.key[j][j];

                if(j == 13)
                    j = 0;

                j++;
            }
            outFile<<newLine<<endl;
        }

        for(int i = 0; i <= 13; i++)
            keyFile<<k13.key[i];


        keyFile.close();
        outFile.close();
        inFile.close();

        cout<<"Done."<<endl;
        cout<<"Your encryption key has been saved to a file named \'"<<FILE_KEY<<"\'"<<endl;

        return 0;
    }

    else if(KEY26 == true)
    {
        string inFileName;
        string outFileName;
        string newLine;
        string line;
        KEY_26 k26;

        cout<<"Please enter the path to the file you wish to encrypt: ";
        getline(cin, inFileName);

        outFileName = inFileName + const_back_end;

        ifstream inFile;
        inFile.open(inFileName.c_str());

        if(!inFile)
        {
            cout<<"Could not open file \'"<<inFileName<<"\' for reading."<<endl;
            return 1;
        }

        ofstream outFile;
        outFile.open(outFileName.c_str(), ios::binary | ios::ate);

        if(!outFile)
        {
            cout<<"Could not create output file."<<endl;
            return 1;
        }

        ofstream keyFile;
        keyFile.open(FILE_KEY, ios::ate | ios::binary);

        while(getline(inFile, line))
        {
            int j = 0;
            for(int i = 0; i < line.size(); i++)
            {
                newLine += line[i] * k26.key[j][j];

                if(j == 26)
                    j = 0;

                j++;
            }
            outFile<<newLine<<endl;
        }

        for(int i = 0; i <= 26; i++)
            keyFile<<k26.key[i];

        keyFile.close();
        outFile.close();
        inFile.close();

        cout<<"Done."<<endl;
        cout<<"Your encryption key has been saved to a file named \'"<<FILE_KEY<<"\'"<<endl;

        return 0;
    }

    else if(KEY39 == true)
    {
        string inFileName;
        string outFileName;
        string newLine;
        string line;
        KEY_39 k39;

        cout<<"Please enter the path to the file you wish to encrypt: ";
        getline(cin, inFileName);

        outFileName = inFileName + const_back_end;

        ifstream inFile;
        inFile.open(inFileName.c_str());

        if(!inFile)
        {
            cout<<"Could not open file \'"<<inFileName<<"\' for reading."<<endl;
            return 1;
        }

        ofstream outFile;
        outFile.open(outFileName.c_str(), ios::binary | ios::ate);

        if(!outFile)
        {
            cout<<"Could not create output file."<<endl;
            return 1;
        }

        ofstream keyFile;
        keyFile.open(FILE_KEY, ios::ate | ios::binary);

        while(getline(inFile, line))
        {
            int j = 0;
            for(int i = 0; i < line.size(); i++)
            {
                newLine += line[i] * k39.key[j][j];

                if(j == 13)
                    j = 0;
            }
            outFile<<newLine<<endl;
        }

        for(int i = 0; i <= 39; i++)
            keyFile<<k39.key[i];

        keyFile.close();
        outFile.close();
        inFile.close();

        cout<<"Done."<<endl;
        cout<<"Your encryption key has been saved to a file named \'"<<FILE_KEY<<"\'"<<endl;

        return 0;
    }

    else 
    {
        cout<<"Please generate a key first."<<endl;
    }

    return 0;
}

int Digi13::DecryptFile()
{
    string choice;

    cout<<"\nPlease ensure that the key file \'your_key.dkf\' is in the sam directory as this program."<<endl;
    cout<<"\nWhat level of encryption did you use?"<<endl;
    cout<<"1 - 13 Digit."<<endl;
    cout<<"2 - 26 Digit."<<endl;
    cout<<"3 - 39 Digit."<<endl;

    cout<<"\n>> ";
    getline(cin, choice);

    if(choice == "1")
    {
        string line;
        string newLine;
        string inFileName;
        string outFileName;
        KEY_13 k13;

        ifstream keyFile;
        keyFile.open(FILE_KEY, ios::binary);

        if(!keyFile)
        {
            cout<<"Could not open the key file. Please ensure that it is in the same directory as this program."<<endl;
            return 1;
        }

        cout<<"Enter the path of the file you wish to decrypt: ";
        getline(cin, inFileName);

        ifstream inFile;
        inFile.open(inFileName, ios::binary);

        if(!inFile)
        {
            cout<<"Could not open file \'"<<inFileName<<"\'"<<endl;
            return 1;
        }

        ofstream outFile;
        outFile.open(outFileName, ios::ate);

        getline(keyFile, line);
        for(int i = 0; i < line.size(); i++)
            k13.key[i][i] = ToInt(line[i]);  // Here is the error

        while(getline(inFile, line))
        {
            int j = 0;
            for(int i = 0; i < line.size(); i++)
            {
                newLine += line[i] / k13.key[j][j]; 

                if(j == 13)
                    j = 0;

                j++;
            }
            outFile<<newLine<<endl;
        }

        cout<<"Done."<<endl;

        return 0;
    }

    else if(choice == "2")
    {
        string line;
        string newLine;
        string inFileName;
        string outFileName;
        KEY_13 k13;

        ifstream keyFile;
        keyFile.open(FILE_KEY, ios::binary);

        if(!keyFile)
        {
            cout<<"Could not open the key file. Please ensure that it is in the same directory as this program."<<endl;
            return 1;
        }

        cout<<"Enter the path of the file you wish to decrypt: ";
        getline(cin, inFileName);

        ifstream inFile;
        inFile.open(inFileName, ios::binary);

        if(!inFile)
        {
            cout<<"Could not open file \'"<<inFileName<<"\'"<<endl;
            return 1;
        }

        ofstream outFile;
        outFile.open(outFileName, ios::ate);

        getline(keyFile, line);
        for(int i = 0; i < line.size(); i++)
            k13.key[i][i] = ToInt(line[i]); // Here is the error

        while(getline(inFile, line))
        {
            int j = 0;
            for(int i = 0; i < line.size(); i++)
            {
                newLine += line[i] / k13.key[j][j];

                if(j == 13)
                    j = 0;

                j++;
            }
            outFile<<newLine<<endl;
        }

        cout<<"Done."<<endl;

        return 0;
    }

    else if(choice == "3")
    {
        string line;
        string newLine;
        string inFileName;
        string outFileName;
        KEY_13 k13;

        ifstream keyFile;
        keyFile.open(FILE_KEY, ios::binary);

        if(!keyFile)
        {
            cout<<"Could not open the key file. Please ensure that it is in the same directory as this program."<<endl;
            return 1;
        }

        cout<<"Enter the path of the file you wish to decrypt: ";
        getline(cin, inFileName);

        ifstream inFile;
        inFile.open(inFileName, ios::binary);

        if(!inFile)
        {
            cout<<"Could not open file \'"<<inFileName<<"\'"<<endl;
            return 1;
        }

        ofstream outFile;
        outFile.open(outFileName, ios::ate);

        getline(keyFile, line);
        for(int i = 0; i < line.size(); i++)
            k13.key[i][i] = ToInt(line[i]); // Here is the error

        while(getline(inFile, line))
        {
            int j = 0;
            for(int i = 0; i < line.size(); i++)
            {
                newLine += line[i] / k13.key[j][j];

                if(j == 13)
                    j = 0;

                j++;
            }
            outFile<<newLine<<endl;
        }

        cout<<"Done."<<endl;

        return 0;
    }

    else
    {
        cout<<"Incorect input. Please try again."<<endl;
    }

    return 0;
}

// Function to convert string into int;
int Digi13::ToInt(char c)
{
    return (int)c;
}


int main()
{
    Digi13 digi;

    digi.GenKey();
    digi.EncryptFile();
    digi.DecryptFile();

    cin.get();

    return 0;
}

And any more advice on what can be done better will be apreciated.

Thanks...

And any more advice on what can be done better will be apreciated.

If you better describe the problem, it will help a lot. Which line is going wrong? Or is it a function you can't get right? Is it the design you're struggling with? Does this compile? Does it link?

Help us.

Help us to help you.

The program then reads the line from the file and converts the string into an int. It here where i get an error from the compiler.

I'm pretty sure this will return the character codes for the characters not the integer values represented by the characters. The error might be from reading values that can't diplay as a string. Hard to say without a description of the compiler error. An integer array might serve you better, or possibly a string stream.

Edited 3 Years Ago by tinstaafl

This question has already been answered. Start a new discussion instead.