Tuesday, July 4, 2017

Programming Challenge 12.10 - File Decryption Filter

Example Files: encrypted.txt
                          decrypted.txt


/* File Decryption Filter - This program decrypts the file produced by the
    program in Programming Challenge 12.9. It reads the contents of the coded
    file, restores the data to its original state, and writes it to another
    file. */

#include "Utility.h"

int    readFile(fstream &, string &);
string decryptText(string);
int    writeFile(fstream &, const string);

int main()
{
    fstream textFile;
    fstream textDecrypt;

    int    fOpen = 0;
    string cryptedText = "";
    string decrypted = "";

    cout << "File Decryption Filter\n\n";

    fOpen = readFile(textFile, cryptedText);

    if (fOpen != -1)
    {
        cout << "\nYour file will be decrypted now ...\n";
        decrypted = decryptText(cryptedText);

        cout << "File decrypted. Writing decrypted text to file ...\n";
        fOpen = writeFile(textDecrypt, decrypted);

        if (fOpen != -1)
        {
            cout << "\nFile successfully created. You can now close this program.\n"
                  << "Have a nice day!";
        }
    }

   pauseSystem();
   return 0;
}

/* **********************************************************
    Definition: readFile

    This function uses a reference parameter to an fstream
    object and a reference to a string object as parameters.
    Upon success, the contents of the file is read-in and
    stored in a string object.
   ********************************************************** */

int readFile(fstream &textFile, string &clearText)
{
    string fileName = "";
    string tmpText = " ";

    cout << "Enter name of file you wish to open: ";
    cin >> fileName;

    textFile.open(fileName, ios::in);

    if (!textFile.fail())
    {
        while (getline(textFile, tmpText))
        {
            clearText += tmpText;
        }
    }
    else
    {
        cout << "\nFile Read Error. Could not read " << fileName;
        return -1;
    }

    return 0;
}

/* **********************************************************
    Definition: decryptText

    This function uses a string object as its parameter. The
    contents in the string object is decrypted, and the string
    returned.
   ********************************************************** */

string decryptText(string decrypted)
{
    int basEnc = 0;
    int locEnc = 0;
    int locFin = 0;
    int locTxt = 0;

    basEnc = 32 / 3;
    locEnc = (5 + (basEnc + 5) * (basEnc + basEnc / 8));
    locFin = ((3 * basEnc) + (locEnc - 6));
    locTxt = ((1 - (basEnc + locEnc) * (basEnc + locEnc)) + locFin);

    for (int i = 0; i < decrypted.size(); i++)
    {
        decrypted[i] -= locTxt + locFin / locEnc;
    }

    return decrypted;
}

/* **********************************************************
    Definition: writeFile

    This uses a reference to an fstream object and a constant
    string object variable as parameters. Upon success, the
    file is opened, and the encrypted text contained in the
    string object is written to it.
   ********************************************************** */

int writeFile(fstream &cryptText, const string decrypted)
{
    string fileName = "";
    string tmpText = "";

    cout << "\nEnter name of file you wish to write to: ";
    cin >> fileName;

    cryptText.open(fileName, ios::out);
  
    if (!cryptText.fail())
    {  
        cryptText << decrypted << "\n";
    }
    else
    {
        cout << "File Write Error. Could not write data to " << fileName;
        return -1;
    }
    cryptText.close();

    return 0;
}

Example Output:



No comments:

Post a Comment