Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <Windows.h>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     SetConsoleCP(1251);
  11.     SetConsoleOutputCP(1251);
  12.     setlocale(LC_ALL, "rus");
  13.     ifstream fin;
  14.     ifstream finS;
  15.     ofstream fout;
  16.     char fileIn[] = "In.txt";
  17.     char fileOut[] = "Out.txt";
  18.     char fileKeys[] = "Keys.txt";
  19.     char fileDecoded[] = "Decoded.txt";
  20.     unsigned char arrin[5000];
  21.     int sum = 0;
  22.     unsigned int keys[5000] = {};
  23.     int ind = 0;
  24.     int size = 0;
  25.     fin.open(fileKeys, ios::binary);
  26.     if (fin.is_open())
  27.     {
  28.         /*cout << "File is opened!\n\n";*/
  29.         while (!fin.eof())
  30.         {
  31.             fin >> arrin;
  32.             for (int i = 0; arrin[i] != '\0'; i++)
  33.             {
  34.                 sum += arrin[i];
  35.             }
  36.             keys[ind] = sum % 256;
  37.             sum = 0;
  38.             ind++;
  39.         }
  40.         cout << endl;
  41.     }
  42.     else
  43.     {
  44.         cout << "Reading file error!\n\n";
  45.         system("pause");
  46.         return 0;
  47.     }
  48.     fin.close();
  49.  
  50.     unsigned char ciphered = 'g';
  51.     fin.open(fileIn, ios::binary);
  52.     fout.open(fileOut, ios::binary);
  53.     if (fout.is_open())
  54.     {
  55.         /*cout << "\"Out\" is opened!\n";*/
  56.     }
  57.     else
  58.     {
  59.         cout << "File opening error!\n\n";
  60.         system("pause");
  61.         return 0;
  62.     }
  63.     if (fin.is_open())
  64.     {
  65.         /*cout << "\"In\" is opened!\n\n";*/
  66.     }
  67.     else
  68.     {
  69.         cout << "File opening error!\n\n";
  70.         system("pause");
  71.         return 0;
  72.     }
  73.     int i = 0;
  74.     while (true)
  75.     {
  76.         fin >> noskipws >> ciphered;
  77.         if (fin.eof())
  78.             break;
  79.         ciphered = (unsigned char)(ciphered ^ keys[i % (ind + 1)]);
  80.         fout << ciphered;
  81.         size++;
  82.         i++;
  83.     }
  84.     i = 0;
  85.     fout.close();
  86.     fin.close();
  87.     /*  fin.open(fileOut);
  88.         fout.open(fileDecoded);
  89.         while (true)
  90.         {
  91.             fin >> noskipws >> ciphered;
  92.             if(fin.eof())
  93.                 break;
  94.             if(ciphered == '\r' || ciphered == ' ' || ciphered == '\n')
  95.             {
  96.                 fout << noskipws << ciphered;
  97.             }
  98.             else
  99.             {
  100.                 ciphered = (unsigned char)(ciphered ^ keys[i % (ind + 1)]);
  101.                 fout << ciphered;
  102.                 i++;
  103.             }
  104.         }
  105.         fin.close();
  106.         fout.close();*/
  107.     int stat[256];
  108.     finS.open(fileOut, ios::binary);
  109.     if (finS.is_open())
  110.     {
  111.         /*cout << "\"Out\" is opened!\n";*/
  112.     }
  113.     else
  114.     {
  115.         cout << "File opening error!\n\n";
  116.         system("pause");
  117.         return 0;
  118.     }
  119.     for (int i = 0; i < 256; i++)
  120.     {
  121.         stat[i] = 0;
  122.     }
  123.     unsigned char s_symb;
  124.     cout << "Введите исследуемый символ.\n";
  125.     cin >> s_symb;
  126.     cout << endl << endl;
  127.     fin.open(fileIn, ios::binary);
  128.     fin.seekg(0, ios_base::beg);
  129.     fin.close();
  130.     fin.open(fileIn, ios::binary);
  131.     ciphered = NULL;
  132.     unsigned char chin;
  133.     while (true)
  134.     {
  135.         fin >> chin;
  136.  
  137.         if (fin.eof())
  138.             break;
  139.         finS >> ciphered;
  140.         if (chin == s_symb)
  141.         {
  142.             stat[ciphered]++;
  143.         }
  144.     }
  145.     for (int i = 0; i < 256; i++)
  146.     {
  147.         cout << left << setw(4) << stat[i] << "";
  148.         if ((i + 1) % 16 == 0) cout << endl;
  149.     }
  150.     fin.close();
  151.     finS.close();
  152.     fin.open(fileOut);
  153.     fin.seekg(0, ios_base::beg);
  154.     fin.close();
  155.     fin.open(fileOut);
  156.     fout.open(fileDecoded);
  157.     i = 0;
  158.     while (i < size)
  159.     {
  160.         fin >> noskipws >> ciphered;
  161.         ciphered = (unsigned char)(ciphered ^ keys[i % (ind + 1)]);
  162.         fout << ciphered;
  163.         i++;
  164.     }
  165.     fin.close();
  166.     fout.close();
  167.     system("pause");
  168.     return 0;
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement