#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <dos.h>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>
#define cls system("cls")
#define frz system("pause");
#define yl system("color 0e");
using namespace std;
//********************************** Type definitions
void layout(); // declaration of display function
string ReadFromFile(); // declaration of function to read from files
string encrypt(string code, string text); // declaration of function to encrypt files
string decrypt(string code, string text); // declaration of function to decrypt files
string codeCreation(string string1, char alphabet[26]); // declaration of function create code
string numericalValue(string s); // find the number value
//********************************** Function Prototypes
//********************************** Main Function
//declaration of string variables
string AlphabetCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string converUppercase(string strn1);
string s1, s3, original, s7 = "ETRERTRHFG";
string string2;
string string3;
//the main function were everything starts and ends
int main()
{
//declaration of the original alphabet
char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
start:
//initialization of string
string s2 = ""; // making string2 a null string
//get string from user
cout << "Enter string to create the decryption alphabet code : ";
cin >> s1;
cout << '\n';
original = s1;
//convert string to upper case
s7 = converUppercase(s1);
//create encryption code
s3 = codeCreation(s7, alphabet);
//encrypt the file
s1 = encrypt(s3, s7);
//decrypt the file
decrypt(s3, s1);
cout << '\n';
//clear the user screen
clrscr();
//read from files
ReadFromFile();
//prove user interaction
layout();
//paused for 3 seconds
sleep(3);
//allow user to decode a file again
int choice;
cout << '\n'<< '\n';
cout << "to decode another file press 2, or 9 to exit" << '\n';
cin >> choice;
switch (choice){
case 2:
goto start;
case 9:
exit (1);
default:
cout<<"invalid choice";
}
getch();
return 0;
}//end main
//the folowing is the initialization of the code us in the decrypt function
string codeCreation(string string1, char alphabet[26]){
//initialization of string
string3= "";
for(int i=0; i < string1.length()-1; i++)
for(int j=i+1; j< string1.length(); j++)
{
if(string1[i] !=' ' ) // skipping spaces
{
if (string1[i] == string1[j]) {// finding duplicates
// if (string1[j] !='*')
//string3 += string1[j];
string1[j] = '*'; }
// making the duplicate letter a '*'
}// end if
} // end for j
//cout << "string1 = " << string1;
// Removing the *'s, spaces and creating a new string
for(int i=0; i < string1.length(); i++)
{
if(string1[i] != '*' && string1[i] != ' ') // checking for an asterick (*) and a space
string2 += string1[i];
//else
//string3 += string1[i];
} // end for i
//check do +
for(int i=0; i < 26; i++)
{
for(int j=0; j < string2.length(); j++)
if(alphabet[i] == string2[j])
alphabet[i] = '+';
} // end for i
//create string3
for(int i=0; i < 26; i++)
{
if(alphabet[i] != '+')
string3 += alphabet[i];
} // end for i
cout << '\n';
//cout << string1;
cout << '\n';
string3 = string2 + string3;
//cout << "string 3 = " << string3;
cout << '\n';
//cout << "string 2 = " << string2;
cout << '\n';
return string3;
}//end function
//the following function will encrypt a file
string encrypt(string code, string text)
{
//cout << '\n';
// cout << "your original text is first converted to uppercase to give "
// << "the result below : "<<'\n'<<"Uppercase Original text : "
// << text<<'\n';
for(int i=0; i < text.length(); i++)
{
if (text[i] == code[0] )
text[i] = 'A';
else if (text[i] == code[1] )
text[i] = 'B';
else if (text[i] == code[2] )
text[i] = 'C';
else if (text[i] == code[3] )
text[i] = 'D';
else if (text[i] == code[4] )
text[i] = 'E';
else if (text[i] == code[5] )
text[i] = 'F';
else if (text[i] == code[6] )
text[i] = 'G';
else if (text[i] == code[7] )
text[i] = 'H';
else if (text[i] == code[8] )
text[i] = 'I';
else if (text[i] == code[9] )
text[i] = 'J';
else if (text[i] == code[10] )
text[i] = 'K';
else if (text[i] == code[11] )
text[i] = 'L';
else if (text[i] == code[12] )
text[i] = 'M';
else if (text[i] == code[13] )
text[i] = 'N';
else if (text[i] == code[14] )
text[i] = 'O';
else if (text[i] == code[15] )
text[i] = 'P';
else if (text[i] == code[16] )
text[i] = 'Q';
else if (text[i] == code[17] )
text[i] = 'R';
else if (text[i] == code[18] )
text[i] = 'S';
else if (text[i] == code[19] )
text[i] = 'T';
else if (text[i] == code[20] )
text[i] = 'U';
else if (text[i] == code[21] )
text[i] = 'V';
else if (text[i] == code[22] )
text[i] = 'W';
else if (text[i] == code[23] )
text[i] = 'X';
else if (text[i] == code[24] )
text[i] = 'Y';
else if (text[i] == code[25] )
text[i] = 'Z';
}//END FOR
cout << text << " ";
return text;
}
//the following function will convert from lower case to uppercase
string converUppercase(string strn1)
{
cout << " ";
for(int i=0; i < strn1.length(); i++)
if(strn1[i] == 'a')
strn1[i] = 'A';
else if(strn1[i] == 'b')
strn1[i] = 'B';
else if(strn1[i] == 'c')
strn1[i] = 'C';
else if(strn1[i] == 'd')
strn1[i] = 'D';
else if(strn1[i] == 'e')
strn1[i] = 'E';
else if(strn1[i] == 'f')
strn1[i] = 'F';
else if(strn1[i] == 'g')
strn1[i] = 'G';
else if(strn1[i] == 'h')
strn1[i] = 'H';
else if(strn1[i] == 'i')
strn1[i] = 'I';
else if(strn1[i] == 'j')
strn1[i] = 'J';
else if(strn1[i] == 'k')
strn1[i] = 'K';
else if(strn1[i] == 'l')
strn1[i] = 'L';
else if(strn1[i] == 'm')
strn1[i] = 'M';
else if(strn1[i] == 'n')
strn1[i] = 'N';
else if(strn1[i] == 'o')
strn1[i] = 'O';
else if(strn1[i] == 'p')
strn1[i] = 'P';
else if(strn1[i] == 'q')
strn1[i] = 'Q';
else if(strn1[i] == 'r')
strn1[i] = 'R';
else if(strn1[i] == 's')
strn1[i] = 'S';
else if(strn1[i] == 't')
strn1[i] = 'T';
else if(strn1[i] == 'u')
strn1[i] = 'U';
else if(strn1[i] == 'v')
strn1[i] = 'V';
else if(strn1[i] == 'w')
strn1[i] = 'W';
else if(strn1[i] == 'x')
strn1[i] = 'X';
else if(strn1[i] == 'y')
strn1[i] = 'Y';
else if(strn1[i] == 'z')
strn1[i] = 'Z';
else
strn1[i] = strn1[i];
return strn1;
}
//the following function will decode a file
string decrypt(string code, string text)
{
//initialization of string
string answer = " ";
int hold = 99, temp = 3;
//cout << '\n';
//cout << "decry befor CODE " << code;
//cout << '\n';
//cout << "decrypted text " << text << '\n';
// the following is a loop that read all characters from the text input file
for(int j=0; j < text.length(); j++)
{
//ensure that j is not repeated
if(temp == j) {}
else
// the following is a loop that read all characters from the text input file
for(int c=code.length()-1; c >=0 ; c--)
{
//cout << temp << " diff temp = " << "j " << j << '\n';
if(j == temp)
cout << temp << " temp = " << "j " << j << '\n';
//break;//j = temp++;
//start the encryption
if(AlphabetCode[c] == text[j] && hold !=j)
{
text[j] = code[c];
//cout << " j code " << j;
//cout << " c code " << c ;
//cout << " text code " << code[c] << '\n';
break;
}
else
//do no changes
text[j] = text[j];
hold = temp;
} //sleep(1);
//check that j has change
if(temp != j){
temp = j;
}
else
cout << " same ";
// hold = j;
}
//output to screen
cout << " " << text;
//return string
return text;
}
//function that reads information from file
string ReadFromFile(){
//declaration of strings
string TextFromFile, sConvertToUpper, printToFile, encryptFile;
//the following provide interaction will files on the computer
ofstream outfile ("decodeFile.txt", ios::out);
ofstream outdecode ("output.txt", ios::out);
ifstream inencrypt ("output.txt", ios:: in);
ifstream infile ("f1234.txt", ios:: in);
//check if file was open
if (!infile)
{
cout << "Error opening file"; exit (1);
}
//check if file was open
if (!inencrypt)
{
cout << "Error opening file"; exit (1);
}
//check if file was open
if (!outdecode)
{
cout << "Error opening file for printing"; exit (1);
}
//check if file was open
if (!outfile)
{
cout << "Error opening file for printing"; exit (1);
}
//read all from the text file
while (infile >>TextFromFile)
{
//conver each word in the file f1234.txt to uppercase
sConvertToUpper = converUppercase(TextFromFile);
//encrypt the ininformation read from f1234.txt
encryptFile = encrypt(s3, sConvertToUpper);
//print the encrypted information to a file called output.txt
outdecode << encryptFile << " ";
//decrypt the information that was encrypt
printToFile = decrypt(s3, encryptFile);
//print the decrypt information to a file called decodeFile.txt
outfile << printToFile << " ";
}
//cout << TextFromFile << " ";
//conver each word in the file f1234.txt to uppercase
sConvertToUpper = converUppercase(TextFromFile);
//encrypt the ininformation read from f1234.txt
encryptFile = encrypt(s3, sConvertToUpper);
//print the encrypted information to a file called output.txt
outdecode << encryptFile << " ";
//decrypt the information that was encrypt
printToFile = decrypt(s3, encryptFile);
//print the decrypt information to a file called decodeFile.txt
outfile << printToFile << " ";
//return the string
return TextFromFile;
}
//the following function provides interaction for the user
void layout()
{
//clear the screen
clrscr();
cout << '\n';
clrscr();
//display the headder information
cout << "****************************************************************************";
cout << '\n';
cout << "******* THIS PROGRAM WILL READ FROM THE FILE f1234.txt *******";
cout << '\n';
cout << "******* THEN *******";
cout << '\n';
cout << "******* ENCRYPT THE FILE AND PRINT TO output.txt *******";
cout << '\n';
cout << "******* THEN FINALLY *******";
cout << '\n';
cout << "******* DECRYPT THE ENCRYPTED FILE AND PRINT TO decodeFile.txt *******";
cout << '\n';
cout << "****************************************************************************";
cout << '\n';
//display the original string of characters you entered tfor code creation
cout << "your original alphabet text is : " << original;
cout << '\n'<< '\n'<< '\n' ;
//display the code
cout << "the decrytion alphabet code is below: ";
cout << '\n'<< "code : "<< s3 << '\n' ;
//declaration of string variables
string readFromf1234,readfromoutput, readFromdecodeFile;
//provide interaction with files from the computer
ifstream readdecodeFile ("decodeFile.txt", ios:: in);
ifstream readoutput ("output.txt", ios:: in);
ifstream readf1234 ("f1234.txt", ios:: in);
//check if file was open
if (!readf1234)
{
cout << "Error opening file f1234.txt"; exit (1);
}
//check if file was open
if (!readoutput)
{
cout << "Error opening file output.txt"; exit (1);
}
//check if file was open
if (!readdecodeFile)
{
cout << "Error opening file decodeFile.txt"; exit (1);
}
//paused for 2 seconds
sleep(2);
cout << '\n'<< '\n'<< '\n';
cout << "the following is a copy of the original file read from the file f1234.txt";
cout << '\n' << '\n';
//read all input from f1234.txt
while (readf1234 >>readFromf1234)
{
cout << readFromf1234 << " ";
}
cout << readFromf1234 << " ";
cout << '\n';
//paused for 2 seconds
sleep(2);
cout << '\n'<< '\n'<< '\n';
cout << "the following is a copy of the file that was encrypted in the file output.txt";
cout << '\n'<< '\n';
//read all input from output.txt
while (readoutput >>readfromoutput)
{
cout << readfromoutput << " ";
}
cout << readfromoutput << " ";
cout << '\n';
//paused for 2 seconds
sleep(2);
cout << '\n'<< '\n'<< '\n'<< '\n';
cout << "the following is a copy of the file that was decoded in the file decodeFile.txt";
cout << '\n'<< '\n';
//read all input from decodefile.txt
while (readdecodeFile >>readFromdecodeFile)
{
cout << readFromdecodeFile << " ";
}
cout << readFromdecodeFile << " ";
cout << '\n';
}//end layout function