Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- //
- //
- //
- string StringEncryptorDecryptor(char encryptionKey, string stringToEncrypt);
- //
- //
- //
- void GetFileInfo(string& fileName, ifstream& inData);
- //
- //
- //
- string ChangeFileExtension(string fileName);
- int main()
- {
- string secretMessage{};
- string decryptMessage{};
- char encryptionKey{};
- string fileName{};
- ifstream inData{};
- ofstream outData{};
- cout << "Choose a single character to encrypt your file. " << endl;
- cin.get(encryptionKey);
- encryptionKey = toupper(encryptionKey);
- cin.ignore(INT_MAX,'\n');
- GetFileInfo(fileName, inData);
- outData.open(fileName.substr(0, fileName.find_last_of('.'))+".xor");
- inData >> secretMessage;
- while(!inData.eof()){
- inData >> secretMessage;
- outData << StringEncryptorDecryptor(encryptionKey, secretMessage) <<" ";
- }
- cout << "Your program has now been encrypted." << endl;
- cout << "The Encrypted filename is: " << ChangeFileExtension(fileName) << endl;
- outData.close();
- inData.close();
- cout <<"\nWould you like to decrypt a file?" << endl;
- cin >> decryptMessage;
- if(decryptMessage == "Yes" ||decryptMessage == "yes"||decryptMessage == "y" ){
- GetFileInfo(fileName, inData);
- cout <<"Your file has been decrypted and is as follows : " << endl;
- inData >> secretMessage;
- while(!inData.eof()){
- cout << StringEncryptorDecryptor(encryptionKey, secretMessage) <<" ";
- inData >> secretMessage;
- }
- }
- else{
- cout <<"Goodbye." << endl;
- }
- return 0;
- }
- string StringEncryptorDecryptor(char encryptionKey, string stringToEncrypt){
- string word = stringToEncrypt;
- for (int i{0};i < stringToEncrypt.size() ; i++){
- word[i] = stringToEncrypt[i] ^ encryptionKey;
- }
- return word;
- };
- void GetFileInfo(string& fileName, 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(string fileName){
- string changedExtension{};
- changedExtension = fileName.substr(0, fileName.find_last_of('.'))+".xor";
- return changedExtension;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement