1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
# include <cstring>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <string>
# include <cmath>
using namespace std;
const int highLimit = 129;
char menuChoice;
char key[highLimit];
string inTxtLoc, outTxtLoc, inMsg, outMsg;
fstream inTxtFile;
fstream outTxtFile;
int countMax;
char getMenuChoice (char);
char getKey (char [], int);
void getEncrypt (char [], int, string, string, string, string);
void runDecrypt (char [], int, string, string, string, string);
int main ()
{
do
{
cout << "|----------------------------------------------------------------------|\n";
cout << "| Secure Message Delivery System |\n";
cout << "|----------------------------------------------------------------------|\n";
cout << "| This program will ENCRYPT and DECRYPT the given message. |\n";
cout << "|----------------------------------------------------------------------|\n";
cout << "| Press E to ENCRYPT message. |\n";
cout << "| Press D to DECRYPT message. |\n";
cout << "| Or press Q to exit the program. |\n";
cout << "|----------------------------------------------------------------------|\n\n\n";;
cout << "Please enter your choice (E, D, or Q) now :\n\n";
menuChoice = getMenuChoice(menuChoice);
if (menuChoice == 'e' || menuChoice == 'E')
{
getKey(key, countMax);
getEncrypt(key, countMax, inTxtLoc, outTxtLoc, inMsg, outMsg);
}
else if (menuChoice == 'd' || menuChoice == 'D')
{
getKey(key, countMax);
runDecrypt(key, countMax, inTxtLoc, outTxtLoc, inMsg, outMsg);
}
}
while ( menuChoice != 'q' || menuChoice != 'Q');
cout << "Press enter to exit.";
cin.ignore(2);
}
char getMenuChoice (char)
{
char menuChoice;
cin >> menuChoice;
cout << endl;
switch(menuChoice)
{
case 'd':
case 'D':
return menuChoice;
break;
case 'e':
case 'E':
return menuChoice;
break;
case 'q':
case 'Q':
exit(0);
break;
default:
cout << "\n You must choose D, E, or Q. Please choose again.\n\n" << endl;
getMenuChoice(menuChoice);
break;
}
}
char getKey(char key[], int countMax)
{
cout << "Input up to 128 characters (lower case letters a-z) for the encryption key :\n\n";
cin >> key;
countMax = strlen(key);
cout << endl << endl;
return key[129], countMax;
}
void getEncrypt(char key[], int countMax, string inTxtLoc, string outTxtLoc, string inMsg, string outMsg)
{
int inCount;
int eCount = 0;
cout << "Please enter the location of the file you wish to encrypt:\n\n";
cin.ignore();
cin >> inTxtLoc;
inTxtFile.open(inTxtLoc);
getline(inTxtFile, inMsg);
inCount = inMsg.length();
cout << "\n\n";
cout << "'" << inMsg << "'" << endl << "\nIs to be encrytpted\n\n";
cout << "Please enter the location of the file to save the encryption: \n\n";
cin >> outTxtLoc;
outTxtFile.open(outTxtLoc);
while (eCount < inCount)
{
for (eCount = 0; eCount <= inCount; eCount++)
{
char swap;
inTxtFile.get(swap);
swap = swap + key[eCount];
swap = swap - key[0];
outTxtFile << swap;
}
}
getline(outTxtFile, outMsg);
cout << "\n\n";
cout << "File was encrypted to read :\n\n" << outMsg << endl;
inTxtFile.close();
outTxtFile.close();
cout << "\n Press the ENTER to return to menu" << endl;
cin.ignore(2);
}
void runDecrypt(char key[], int countMax, string inTxtLoc, string outTxtLoc, string inMsg, string outMsg)
{
int inCount;
int dCount;
cout << "Please enter the location of the file you wish to decrypt:\n\n";
cin >> inTxtLoc;
inTxtFile.open(inTxtLoc);
getline(inTxtFile, inMsg);
inCount = inMsg.size();
cout << "\n\n";
cout << """" << inMsg << """" << endl << "Is to be decrytpted.\n\n";
cout << "Please enter the location of the file to save the decryption: \n\n";
cin >> outTxtLoc;
outTxtFile.open(outTxtLoc);
getline(outTxtFile, outMsg);
cout << "\n\n";
cout << "File was decrypted, and reads : " << outMsg << endl;
cout << "\n\n";
while (inTxtFile.good())
{
int dCounter;
for (int dCounter = 0; dCounter < countMax; dCounter++)
{
char swap;
inTxtFile.get(swap);
swap = inMsg[dCount] - key[dCount];
outTxtFile << swap;
}
}
inTxtFile.close();
outTxtFile.close();
cout << "\n Press the ENTER to return to menu" << endl;
cin.ignore(2);
}
|