thenuke321

Untitled

Nov 15th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. // This code was largely done by someone else but I fixed it and made it better.
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     ifstream inFile;
  10.     ofstream outFile;
  11.  
  12.     // Define variables
  13.     string fileName,
  14.         key = "seacrest out";
  15.     // Key
  16.     cout << "Please Enter A Key" << endl;
  17.     getline(cin,key);
  18.     // Input
  19.     cout << "Please enter the name of the file you wish to encrypt: ";
  20.     cin >> fileName;
  21.     cout << endl;
  22.  
  23.     inFile.open(fileName, ios::in | ios::binary);
  24.     string str((istreambuf_iterator<char>(inFile)), istreambuf_iterator<char>()); // Reads a text file into a single string.
  25.     inFile.close();
  26.     cout << "The file has been read into memory as follows:" << endl;  
  27.     cout << str << endl;
  28.     system("pause");
  29.  
  30.     // Encryption
  31.   //  cout << "The file has been encrypted as follows:" << endl;
  32.     for (unsigned x = 0; x < str.size(); x++)           // Steps through the characters of the string.
  33.         str[x] ^= key[x % key.size()];                  // Cycles through a multi-character encryption key, and encrypts the character using an XOR bitwise encryption.
  34.     //cout << str << endl;                                // This code works, but I get system beeps. Something is still wrong.
  35.  
  36.     // Write Encrypted File
  37.     cout << "Please enter the file name to save the encrypted file under: ";
  38.     cin >> fileName;
  39.     cout << endl;
  40.  
  41.     outFile.open(fileName, ios::out | ios::binary);
  42.     outFile.write(str.c_str(), str.size());         // Writes the string to the binary file by first converting it to a C-String using the .c_str member function.
  43.  
  44.     system("pause");
  45.  
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment