artemgf

Мяу

Dec 10th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include "funcs.h"
  2.  
  3. int main()
  4. {
  5.     //SetConsoleCP(1251); SetConsoleOutputCP(1251);
  6.     system("chcp 1251 > nul");
  7.     //setlocale(LC_ALL, "Russian");
  8.    
  9.     unsigned char line1;
  10.     //char line;
  11.     unsigned char l;
  12.     unsigned int sum = 0;
  13.     int i = 0;
  14.     int A[256][256];  
  15.     for (int i = 0; i < 256; i++)
  16.     {
  17.         for (int j = 0; j < 256; j++)
  18.         {
  19.             A[i][j] = 0;
  20.         }
  21.     }
  22.                                                      //   (    чтение    )
  23.     ifstream fin("key1.txt", ios::in | ios::binary);
  24.     if (!fin)                                        // cotrol
  25.     {
  26.         cout << "Error of file opening." << endl;
  27.         return 1;
  28.     }
  29.     l = '.';
  30.     bool fl = true;
  31.     int kol = 0; // длина массива ключей
  32.     while (fin.get((char&)line1))         /// кол-во слов
  33.     {
  34.         //line1 = fin.get();
  35.         if((l != ' ') && ((line1 == ' ') || (line1 == ' ')))
  36.             {
  37.                 kol++;
  38.             }
  39.         l = line1; 
  40.     }
  41.     //if ((line1 != ' ') && (line1 != '\n')) { kol++; }
  42.     fl = false;
  43.     unsigned char* key = new unsigned char[kol];
  44.     fin.clear();
  45.     fin.seekg(0);
  46.     unsigned char t;
  47.     while (fin.get((char&)line1))         /// массив ключей
  48.     {
  49.         //line1 = fin.get();
  50.         if ((line1 == ' ') || (line1 == '\n'))
  51.         {
  52.             fl = true;
  53.         }
  54.         else
  55.         {
  56.             if (fl)
  57.             {    t = sum % 256;
  58.                 key[i] = t;
  59.                 sum = 0;
  60.                 i++;
  61.             }
  62.             sum = sum + line1;
  63.             fl = false;
  64.         }
  65.     }
  66.     //sum = sum - line1;
  67.     //key[i] = sum % 256;
  68.                        // массив заполнен юху-у-у-у!
  69.  
  70.     fin.close();
  71.      
  72.     ifstream text("text.txt", ios::in | std::ios::binary);
  73.     if (!text.is_open())                     // cotrol
  74.     {
  75.         cout << "Error of file opening." << endl;
  76.         return 1;
  77.     }
  78.     ofstream crypt("crypt.txt", ios::out | std:: ios::binary);
  79.     if (!crypt.is_open())                     // cotrol
  80.     {
  81.         cout << "Error of file opening." << endl;
  82.         return 1;
  83.     }
  84.     i = 0;                                     //   запись кодирование
  85.     unsigned char cEOF = (unsigned char)(EOF);
  86.     while (text.get((char&)line1))
  87.     {
  88.         //line1 = text.get();
  89.         cout << line1;
  90.         /*if (line1 == cEOF)
  91.         {
  92.             break;
  93.         }*/
  94.         l = (unsigned char)((int)(line1) ^ key[i]);
  95.         A[( unsigned char)(line1)][(unsigned char)(line1) ^ key[i]]++;
  96.         crypt << l;
  97.         i++;
  98.         if (i == kol-1) { i = 0; }
  99.     }
  100.    
  101.    
  102.     crypt.close();
  103.     text.close();
  104.     ////// декодирование
  105.     ifstream code("crypt.txt", ios::in | std::ios::binary);
  106.     if (!code.is_open())                     // cotrol
  107.     {
  108.         cout << "Error of file opening code." << endl;
  109.         return 1;
  110.     }
  111.     ofstream decode("decode.txt", ios::out | std::ios::binary);
  112.     if (!decode.is_open())                     // cotrol
  113.     {
  114.         cout << "Error of file opening decode." << endl;
  115.         return 1;
  116.     }
  117.     cout << endl;
  118.     cout << endl;
  119.     i = 0;                                     //   запись декодирование
  120.    
  121.     while (code.get((char&)(line1)))
  122.     {
  123.         l = (unsigned char)((int)(line1) ^ key[i]);
  124.         //cout << l;
  125.         decode << l;
  126.         i++;
  127.         if (i == kol - 1) { i = 0; }
  128.     }
  129.     cout<<endl;
  130.     decode.close();
  131.     code.close();
  132.  
  133.     // statistic
  134.     st(A);
  135.     return 0;
  136. }
Add Comment
Please, Sign In to add comment