thenuke321

Untitled

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