Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. //
  8. //
  9. //
  10. string StringEncryptorDecryptor(char encryptionKey, string stringToEncrypt);
  11.  
  12. //
  13. //
  14. //
  15. void GetFileInfo(string& fileName, ifstream& inData);
  16.  
  17. //
  18. //
  19. //
  20. string ChangeFileExtension(string fileName);
  21.  
  22. int main()
  23. {
  24. string secretMessage{};
  25. string decryptMessage{};
  26. char encryptionKey{};
  27. string fileName{};
  28. ifstream inData{};
  29. ofstream outData{};
  30.  
  31. cout << "Choose a single character to encrypt your file. " << endl;
  32. cin.get(encryptionKey);
  33. encryptionKey = toupper(encryptionKey);
  34. cin.ignore(INT_MAX,'\n');
  35.  
  36.  
  37. GetFileInfo(fileName, inData);
  38.  
  39. outData.open(fileName.substr(0, fileName.find_last_of('.'))+".xor");
  40.  
  41. inData >> secretMessage;
  42. while(!inData.eof()){
  43. inData >> secretMessage;
  44. outData << StringEncryptorDecryptor(encryptionKey, secretMessage) <<" ";
  45. }
  46.  
  47. cout << "Your program has now been encrypted." << endl;
  48. cout << "The Encrypted filename is: " << ChangeFileExtension(fileName) << endl;
  49.  
  50. outData.close();
  51. inData.close();
  52.  
  53. cout <<"\nWould you like to decrypt a file?" << endl;
  54. cin >> decryptMessage;
  55.  
  56. if(decryptMessage == "Yes" ||decryptMessage == "yes"||decryptMessage == "y" ){
  57. GetFileInfo(fileName, inData);
  58. cout <<"Your file has been decrypted and is as follows : " << endl;
  59. inData >> secretMessage;
  60. while(!inData.eof()){
  61. cout << StringEncryptorDecryptor(encryptionKey, secretMessage) <<" ";
  62. inData >> secretMessage;
  63. }
  64. }
  65. else{
  66. cout <<"Goodbye." << endl;
  67. }
  68.  
  69. return 0;
  70. }
  71.  
  72. string StringEncryptorDecryptor(char encryptionKey, string stringToEncrypt){
  73. string word = stringToEncrypt;
  74. for (int i{0};i < stringToEncrypt.size() ; i++){
  75. word[i] = stringToEncrypt[i] ^ encryptionKey;
  76. }
  77. return word;
  78. };
  79.  
  80. void GetFileInfo(string& fileName, ifstream& inData){
  81. do {
  82. // Prompt user for file name and open file
  83. cout << "Enter file containing input values: ";
  84.  
  85. cin >> fileName;
  86. inData.open(fileName.c_str());
  87. if (!inData){
  88. cout << "Invalid file!\n\n";
  89. }
  90. } while (!inData); // Loop until valid file name provided
  91. };
  92.  
  93. string ChangeFileExtension(string fileName){
  94. string changedExtension{};
  95. changedExtension = fileName.substr(0, fileName.find_last_of('.'))+".xor";
  96.  
  97. return changedExtension;
  98. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement