// // decrypt1.cpp // CSC 215 // John Rogers // // This program decrypts the input // in a text file selected by the // user and places the output in // a text file selected by the user. // It does this by performing // character input and output. // #include #include #include #include void openFiles(ifstream& inputFile, ofstream& outputFile); char decrypt(char encryptedChar, char key); int main() { ifstream inputFile; ofstream outputFile; char encryptedChar, decryptedChar, key; openFiles(inputFile, outputFile); cout << "Please enter the key character: "; cin >> key; inputFile.get(encryptedChar); while (! inputFile.eof()) { decryptedChar = decrypt(encryptedChar, key); outputFile.put(decryptedChar); inputFile.get(encryptedChar); } inputFile.close(); outputFile.close(); return 0; } // // // This function prompts the user for and opens // two files: one for input and one for output. // The first is connected to an input file stream // object and the second to an output file stream // object, both of which are passed as call-by- // reference parameters. // void openFiles(ifstream& inputFile, ofstream& outputFile) { char inputFileName[50], outputFileName[50]; do { cout << "Please enter the name of the input file: "; cin >> inputFileName; inputFile.open(inputFileName); if (inputFile.fail()) { cout << "Cannot open the file: " << inputFileName << endl; } } while (inputFile.fail()); do { cout << "Please enter the name of the output file: "; cin >> outputFileName; outputFile.open(outputFileName); if (outputFile.fail()) { cout << "Cannot open the file: " << outputFileName << endl; } } while (outputFile.fail()); return; } // // // This function uses an exclusive-or // to decrypt a character. Note that // _all_ characters get decrypted. // char decrypt(char encryptedChar, char key) { char decryptedChar; decryptedChar = encryptedChar ^ key; return decryptedChar; }