Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Message Encryption program
- // Kristophe F. Dizon
- // Febraury 22, 2020
- // Program takes user's input for desired "key" to be used for encrypting message from
- // an input file. Program will then return a file with containing the encrypted
- // message.
- #include <iostream>
- #include <fstream>
- using namespace std;
- // Encrypt a string of words
- // Pre: encryptionKey contains a alphabetic char and stringToEncrypt contains a string.
- // Post: Return an encrypted version of the original string passed in.
- string StringEncryptorDecryptor(/*IN*/char encryptionKey, /*IN*/string stringToEncrypt);
- // Prompt user for file name and return ifstream
- // Pre: inData declared
- // Post: inData contains valid ifstream for data file
- void GetFileInfo(/* IN/OUT */string& fileName, /* IN/OUT */ifstream& inData);
- // Creates a new file with a changed file extension
- // Pre: fileName contains a valid string name of a given file
- // Post: Returns a string that changes the extension of a file
- string ChangeFileExtension(/* IN */string fileName);
- // Prompt user for a valid character to be used for encyption key
- // Pre: encryptionKey is a valid char variable
- // Post: intializes encryptionKey with a valid char.
- void GetCharacter(/* IN/OUT */char& encryptionKey);
- int main()
- {
- string secretMessage{}; // Place to store inData from file stream
- string decryptMessage{}; // Place to store user input for decrypting a file
- char encryptionKey{}; // Place to store user selected encryption key
- string fileName{}; // Holds user input of file name
- ifstream inData{}; // Place to hold a valid input file stream
- ofstream outData{}; // Place to hold a valid output file stream
- // Takes users input for intializing encryptionKey and loops while input is not alphabetic.
- GetCharacter(encryptionKey);
- // Loops until a valid file and inData contains valid input stream
- GetFileInfo(fileName, inData);
- // Open an output file with original the file name but with the extension changed
- outData.open(ChangeFileExtension(fileName));
- // Loop until fail state (end of file)
- while(!inData.eof()){
- // Take data from inData stream and store in secretMessage
- getline(inData,secretMessage);
- // Encrypt secretMessage and store into outData (output file)
- outData << StringEncryptorDecryptor(encryptionKey, secretMessage);
- }
- // Alerting user file has been ecrypted and a new file has been made
- cout << "Your file has now been encrypted." << endl;
- cout << "The Encrypted filename is: " << ChangeFileExtension(fileName) << endl;
- // Closing ifstream/ofstream
- outData.close();
- inData.close();
- // Asking users to decrypt file and storing choice into decryptMessage
- cout <<"\nWould you like to decrypt a file?(yes/no)" << endl;
- cin >> decryptMessage;
- // Handle cases for decryptMessage
- if(decryptMessage == "Yes" ||decryptMessage == "yes"||decryptMessage == "y" ){
- // Loops until a valid file and inData contains valid input stream
- GetFileInfo(fileName, inData);
- cout <<"Your file has been decrypted and is as follows : " << endl;
- // Loop until fail state (end of file)
- while(!inData.eof()){
- // Take data from inData stream and store in secretMessage
- getline(inData,secretMessage);
- // Decrypt secretMessage and print out to screen
- cout << StringEncryptorDecryptor(encryptionKey, secretMessage);
- }
- }
- // if decryptMessage is no, end program
- else{
- cout <<"Goodbye." << endl;
- }
- return 0;
- }
- string StringEncryptorDecryptor(/*IN*/char encryptionKey, /*IN*/string stringToEncrypt){
- string word = stringToEncrypt;
- for (int i{0};i < stringToEncrypt.size() ; i++){
- word[i] = stringToEncrypt[i] ^ encryptionKey;
- }
- return word;
- };
- void GetFileInfo(/* IN/OUT */string& fileName, /* IN/OUT */ifstream& inData){
- do {
- // Prompt user for file name and open file
- cout << "Enter file containing input values: ";
- cin >> fileName;
- inData.open(fileName.c_str());
- if (!inData){
- cout << "Invalid file!\n\n";
- }
- } while (!inData); // Loop until valid file name provided
- };
- string ChangeFileExtension(/* IN */string fileName){
- string changedExtension{};
- changedExtension = fileName.substr(0, fileName.find_last_of('.'))+"11.txt";
- return changedExtension;
- };
- void GetCharacter(/* IN/OUT */char& encryptionKey){
- do{
- cout << "Choose a single character to encrypt your file. " << endl;
- cin.get(encryptionKey);
- cin.ignore(INT_MAX,'\n');
- if(!isalpha(encryptionKey))
- cout << "Your input was not a character (alphabetic)."<<endl;
- }while(!isalpha(encryptionKey));
- encryptionKey = toupper(encryptionKey);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement