Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. // Message Encryption program
  2. // Kristophe F. Dizon
  3. // Febraury 22, 2020
  4. // Program takes user's input for desired "key" to be used for encrypting message from
  5. // an input file. Program will then return a file with containing the encrypted
  6. // message.
  7.  
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14. // Encrypt a string of words
  15. // Pre: encryptionKey contains a alphabetic char and stringToEncrypt contains a string.
  16. // Post: Return an encrypted version of the original string passed in.
  17. string StringEncryptorDecryptor(/*IN*/char encryptionKey, /*IN*/string stringToEncrypt);
  18.  
  19. // Prompt user for file name and return ifstream
  20. // Pre: inData declared
  21. // Post: inData contains valid ifstream for data file
  22. void GetFileInfo(/* IN/OUT */string& fileName, /* IN/OUT */ifstream& inData);
  23.  
  24. // Creates a new file with a changed file extension
  25. // Pre: fileName contains a valid string name of a given file
  26. // Post: Returns a string that changes the extension of a file
  27. string ChangeFileExtension(/* IN */string fileName);
  28.  
  29. // Prompt user for a valid character to be used for encyption key
  30. // Pre: encryptionKey is a valid char variable
  31. // Post: intializes encryptionKey with a valid char.
  32. void GetCharacter(/* IN/OUT */char& encryptionKey);
  33.  
  34. int main()
  35. {
  36. string secretMessage{}; // Place to store inData from file stream
  37. string decryptMessage{}; // Place to store user input for decrypting a file
  38. char encryptionKey{}; // Place to store user selected encryption key
  39. string fileName{}; // Holds user input of file name
  40. ifstream inData{}; // Place to hold a valid input file stream
  41. ofstream outData{}; // Place to hold a valid output file stream
  42.  
  43. // Takes users input for intializing encryptionKey and loops while input is not alphabetic.
  44. GetCharacter(encryptionKey);
  45.  
  46. // Loops until a valid file and inData contains valid input stream
  47. GetFileInfo(fileName, inData);
  48.  
  49. // Open an output file with original the file name but with the extension changed
  50. outData.open(ChangeFileExtension(fileName));
  51.  
  52. // Loop until fail state (end of file)
  53. while(!inData.eof()){
  54. // Take data from inData stream and store in secretMessage
  55. getline(inData,secretMessage);
  56. // Encrypt secretMessage and store into outData (output file)
  57. outData << StringEncryptorDecryptor(encryptionKey, secretMessage);
  58. }
  59.  
  60. // Alerting user file has been ecrypted and a new file has been made
  61. cout << "Your file has now been encrypted." << endl;
  62. cout << "The Encrypted filename is: " << ChangeFileExtension(fileName) << endl;
  63.  
  64. // Closing ifstream/ofstream
  65. outData.close();
  66. inData.close();
  67.  
  68. // Asking users to decrypt file and storing choice into decryptMessage
  69. cout <<"\nWould you like to decrypt a file?(yes/no)" << endl;
  70. cin >> decryptMessage;
  71. // Handle cases for decryptMessage
  72. if(decryptMessage == "Yes" ||decryptMessage == "yes"||decryptMessage == "y" ){
  73. // Loops until a valid file and inData contains valid input stream
  74. GetFileInfo(fileName, inData);
  75. cout <<"Your file has been decrypted and is as follows : " << endl;
  76. // Loop until fail state (end of file)
  77. while(!inData.eof()){
  78. // Take data from inData stream and store in secretMessage
  79. getline(inData,secretMessage);
  80. // Decrypt secretMessage and print out to screen
  81. cout << StringEncryptorDecryptor(encryptionKey, secretMessage);
  82. }
  83. }
  84. // if decryptMessage is no, end program
  85. else{
  86. cout <<"Goodbye." << endl;
  87. }
  88.  
  89. return 0;
  90. }
  91.  
  92. string StringEncryptorDecryptor(/*IN*/char encryptionKey, /*IN*/string stringToEncrypt){
  93. string word = stringToEncrypt;
  94. for (int i{0};i < stringToEncrypt.size() ; i++){
  95. word[i] = stringToEncrypt[i] ^ encryptionKey;
  96. }
  97. return word;
  98. };
  99.  
  100. void GetFileInfo(/* IN/OUT */string& fileName, /* IN/OUT */ifstream& inData){
  101. do {
  102. // Prompt user for file name and open file
  103. cout << "Enter file containing input values: ";
  104.  
  105. cin >> fileName;
  106. inData.open(fileName.c_str());
  107. if (!inData){
  108. cout << "Invalid file!\n\n";
  109. }
  110. } while (!inData); // Loop until valid file name provided
  111. };
  112.  
  113. string ChangeFileExtension(/* IN */string fileName){
  114. string changedExtension{};
  115. changedExtension = fileName.substr(0, fileName.find_last_of('.'))+"11.txt";
  116.  
  117. return changedExtension;
  118. };
  119.  
  120. void GetCharacter(/* IN/OUT */char& encryptionKey){
  121. do{
  122. cout << "Choose a single character to encrypt your file. " << endl;
  123. cin.get(encryptionKey);
  124. cin.ignore(INT_MAX,'\n');
  125. if(!isalpha(encryptionKey))
  126. cout << "Your input was not a character (alphabetic)."<<endl;
  127. }while(!isalpha(encryptionKey));
  128. encryptionKey = toupper(encryptionKey);
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement