Advertisement
melnikovmaxim

BORSCHT_Vernam cipher

Dec 16th, 2019
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. //link https://yadi.sk/d/TGDExdSbp1nICQ
  2. #include <QCoreApplication>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9. int symbcount(int y) { //подсчет символов для задания массива
  10.     ifstream one("text.txt"), two("gamma.txt");
  11.     int x = -1;
  12.     if (y == 1 && !one.is_open())
  13.         x = -2;
  14.     else if (y == 2 && !one.is_open())
  15.         x = -2;
  16.     if (x != -2 && y == 1)
  17.         while (!one.eof())
  18.         {
  19.             one.get();
  20.             x++;
  21.         }
  22.     else
  23.         if (x != -2 && y == 2)
  24.             while (!two.eof())
  25.             {
  26.                 two.get();
  27.                 x++;
  28.             }
  29.     one.close();
  30.     two.close();
  31.     return x;
  32. }
  33.  
  34. void getsymb(char** buffer1, char* buffer2, int y1, int y2) { //запись символов из файлов в массивы
  35.     ifstream one;
  36.     one.open("text.txt");
  37.     char c;
  38.     cout << "Text: " << endl << "==================================================" << endl << endl;
  39.     for (int i1 = 0; i1 < y1; i1++)
  40.     {
  41.         c = one.get();
  42.         if (c >= 65 && c <= 90)
  43.             buffer1[i1][0] = c + 32;
  44.         else
  45.             buffer1[i1][0] = c;
  46.         cout << buffer1[i1][0];
  47.     }
  48.     cout << endl << endl << "==================================================" << endl << endl;
  49.     one.close();
  50.  
  51.     ifstream two;
  52.     two.open("gamma.txt");
  53.     cout << "Key: " << endl << "==================================================" << endl << endl;
  54.     for (int i1 = 0; i1 < y2; i1++)
  55.     {
  56.         c = two.get();
  57.         if (c >= 65 && c <= 90)
  58.             buffer2[i1] = c + 32;
  59.         else
  60.             buffer2[i1] = c;
  61.         cout << buffer2[i1];
  62.     }
  63.     cout << endl << endl << "==================================================" << endl << endl;
  64.     two.close();
  65. }
  66.  
  67. void addchar(char** buffer1, char* buffer2, int y1, int y2) { //добавление символов в двумерный массив (символов ключа)
  68.     int j1 = -1;
  69.     for (int k = 0; k < y1; k++)
  70.     {
  71.  
  72.         if (buffer1[k][0] >= 97 && buffer1[k][0] <= 122) {
  73.             j1++;
  74.             if (j1 > y2 - 1)
  75.                 j1 = 0;
  76.             buffer1[k][1] = buffer2[j1];
  77.         }
  78.         else
  79.             buffer1[k][1] = '0';
  80.     }
  81. }
  82.  
  83. void outshifr(char** buffer1, char* buffer2, int y) //вывод зашифрованного текста
  84. {
  85.     ofstream out("C:\\lab6\\shifr.txt");
  86.     cout << "Ciphertext: " << endl << "==================================================" << endl << endl;
  87.     for (int k = 0; k < y; k++) {
  88.         if (buffer1[k][0] >= 97 && buffer1[k][0] <= 122)
  89.             buffer2[k] = buffer1[k][0] + (buffer1[k][1]) % 26;
  90.         else
  91.             buffer2[k] = buffer1[k][0];
  92.         out << buffer2[k];
  93.         cout << buffer2[k];
  94.     }
  95.     cout << endl << endl << "==================================================" << endl << endl;
  96. }
  97.  
  98. void outunshifr(char** buffer1, char* buffer2, int y) { //вывод расшифрованного текста
  99.     char c;
  100.     ofstream out("C:\\lab6\\unshifr.txt");
  101.     cout << "Deciphered text: " << endl << "==================================================" << endl << endl;
  102.     for (int k = 0; k < y; k++) {
  103.         if (buffer2[k] != 32 && buffer2[k] != 10)
  104.             c = buffer2[k] - (buffer1[k][1]) % 26;
  105.         else
  106.             c = buffer2[k];
  107.         out << c;
  108.         cout << c;
  109.     }
  110.     cout << endl << endl << "==================================================" << endl << endl;
  111. }
  112.  
  113.  
  114. int main(int argc, char* argv[])
  115. {
  116.     char** buffer1;
  117.     char* buffer2;
  118.     char* buffer3;
  119.  
  120.     int x = 0, i, j;
  121.     bool chiph = false;
  122.     cout << "Enter the '1' to ENCRYPT text or '2' to DECIPHER" << endl << endl;
  123.     while (1) {
  124.         cout << "Enter here the number: ";
  125.         cin >> x;
  126.         cout << endl << endl;
  127.  
  128.         if (x == 1) {
  129.             i = symbcount(1);
  130.             j = symbcount(2);
  131.             if (i == -2 || j == -2)
  132.                 cout << "One or more files are empty or do not exist" << endl;
  133.             else {
  134.  
  135.                 buffer1 = new char* [i];
  136.                 for (int p = 0; p < i; p++)
  137.                     buffer1[p] = new char[1];
  138.                 buffer2 = new char[j];
  139.                 buffer3 = new char[i];
  140.  
  141.                 getsymb(buffer1, buffer2, i, j);
  142.                 addchar(buffer1, buffer2, i, j);
  143.                 outshifr(buffer1, buffer3, i);
  144.                 chiph = true;
  145.             }
  146.         }
  147.         else if (x == 2 && chiph == true)
  148.             outunshifr(buffer1, buffer3, i);
  149.         else if (x == 2 && chiph == false)
  150.             cout << "First you need to encrypt the files" << endl;
  151.         else
  152.             cout << "If you want to encrypt new data, enter '1'" << endl;
  153.     }
  154.  
  155.     delete buffer1, buffer2, buffer3;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement