encrypted.txt
/* File Encryption Filter - File encryption is the science of writing the
contents of a file in secret code. This encryption program works like
a filter, reading the contents of one file, modifying the data into a
code, and then writing the coded contents out to a second file. The
second file is a version of the first file, but written in secret code. */
#include "Utility.h"
int readFile(fstream &, string &);
string encryptText(string);
int writeFile(fstream &, const string);
int main()
{
fstream textFile;
fstream textCrypt;
int fOpen = 0;
string clearText = "";
string encrypted = "";
cout << "File Encryption Filter\n\n";
fOpen = readFile(textFile, clearText);
if (fOpen != -1)
{
cout << "\nYour file will be encrypted now ...\n";
encrypted = encryptText(clearText);
cout << "File encrypted. Writing encrypted text to file ...\n";
writeFile(textCrypt, encrypted);
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;
}
textFile.close();
return 0;
}
/* **********************************************************
Definition: encryptText
This function uses a string object as parameters. The
contents in the string object is encrypted, and the string
returned.
********************************************************** */
string encryptText(string encrypted)
{
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 < encrypted.size(); i++)
{
encrypted[i] += locTxt + locFin / locEnc;
}
return encrypted;
}
/* **********************************************************
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 encrypted)
{
string fileName = "";
cout << "\nEnter name of file you wish to write to: ";
cin >> fileName;
cryptText.open(fileName, ios::out);
if (!cryptText.fail())
{
cryptText << encrypted;
}
else
{
cout << "File Write Error. Could not write data to " << fileName;
return -1;
}
cryptText.close();
return 0;
}
No comments:
Post a Comment