Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     ifstream keyStream("key.txt");
  11.     vector<int> offsets;
  12.     string temp;
  13.     while (keyStream >> temp){
  14.         int offsetSum = 0;
  15.         for (int i = 0; i < temp.size(); ++i) {
  16.             offsetSum += temp[i];
  17.         }
  18.         offsets.push_back(offsetSum);
  19.     }
  20.  
  21.     ifstream in("input.txt");
  22.     ofstream encrypted("encrypted.txt");
  23.  
  24.  
  25.     map<char, map<char, int>> encryptionsOfChars;
  26.     printf("Encrypted content(encrypted.txt):\n");
  27.     while (getline(in, temp)){
  28.         for (int i = 0; i < temp.size(); ++i) {
  29.             char nChar = (temp[i] + offsets[i % offsets.size()]) % 256;
  30.             encryptionsOfChars[temp[i]][nChar]++;
  31.             temp[i] = nChar;
  32.         }
  33.         encrypted << temp << endl;
  34.         cout << temp << endl;
  35.     }
  36.  
  37.     ifstream encryptedIn("encrypted.txt");
  38.     ofstream decrypted("decrypted.txt");
  39.  
  40.     printf("Decrypted content(decrypted.txt):\n");
  41.     while (getline(encryptedIn, temp)){
  42.         for (int i = 0; i < temp.size(); ++i) {
  43.             temp[i] = (temp[i] - offsets[i % offsets.size()]) % 256;
  44.         }
  45.         decrypted << temp << endl;
  46.         cout << temp << endl;
  47.     }
  48.  
  49.     bool repeat = true;
  50.     while (repeat){
  51.         char symbol;
  52.         printf("symbol to examine: ");
  53.         cin >> symbol;
  54.         for (auto i = encryptionsOfChars[symbol].begin(); i !=encryptionsOfChars[symbol].end() ; ++i) {
  55.             printf("\t%d(%c)\t: %d\n", i -> first,  i ->first, i -> second);
  56.         }
  57.         printf("continue? (0 / 1): ");
  58.         cin >> repeat;
  59.     }
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement