Advertisement
blazinghorizon

Untitled

Oct 18th, 2020 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <unistd.h>
  5. #include <vector>
  6. #include <locale>
  7.  
  8. #define COL 4
  9. #define STR 2
  10.  
  11. using namespace std;
  12.  
  13. bool checkKeys(string key, int limit);
  14. void decrypt(string message, vector<int> key_col, vector<int> key_str);
  15. void crypt(string message, vector<int> key_col, vector<int> key_str);
  16. vector<int> castKey(string stringKey);
  17.  
  18. int main()
  19. {
  20.     /* Установка локали для корректной работы */
  21.     setlocale(LC_ALL, "ru_RU.utf-8");
  22.  
  23.     string message;
  24.     string mode;
  25.     string key_str;
  26.     string key_col;
  27.     string answer;
  28.  
  29.     cout << "Данная программа осуществляет (де)шифрование по методу разнесения-рассечения\n";
  30.     /*cout << "Введите режим работы (дешифровка/шифровка)\n";
  31.     while (true) {
  32.         getline(cin, mode);
  33.         if (mode.compare("дешифровка") == 0 || mode.compare("шифровка") == 0) break;
  34.         else cout << "Повторите ввод режима работы\n";
  35.     }
  36. */
  37.     mode = "шифровка";
  38.     cout << "Введите строку для " << mode.substr(0, mode.length() - 2) << \n";
  39.     while (true) {
  40.             getline(cin, message);
  41.         cout << "Это все? (да/нет)\n";
  42.         getline(cin, answer);
  43.         if (answer.compare("да") == 0) break;
  44.     }/*
  45.  
  46.     cout << "Введите ключ для столбцов\n";
  47.     while (true) {
  48.         getline(cin, key_col);
  49.         if (checkKeys(key_col, COL)) break;
  50.         else cout << "Повторите ввод ключа\n";
  51.     }
  52. */
  53.     key_col = "4132";
  54.     /*
  55.     cout << "Введите ключ для строк\n";
  56.     while (true) {
  57.         getline(cin, key_str);
  58.         if (checkKeys(key_str, STR)) break;
  59.         else cout << "Повторите ввод ключа\n";
  60.     }*/
  61.     key_str = "21";
  62.  
  63.     vector<int> k_col = castKey(key_col);
  64.     vector<int> k_str = castKey(key_str);
  65.  
  66.     if (mode.compare("дешифровка") == 0) decrypt(message, k_col, k_str);
  67.     if (mode.compare("шифровка") == 0) crypt(message, k_col, k_str);
  68.  
  69.     return 0;
  70.  
  71. }
  72.  
  73. void decrypt(string message, vector<int> key_col, vector<int> key_str)
  74. {
  75.     string blocks[STR * COL] = {""};
  76.  
  77.     return;
  78. }
  79.  
  80. void crypt(string message, vector<int> key_col, vector<int> key_str)
  81. {
  82.     string blocks[STR * COL];
  83.     for (unsigned int i = 0; i < STR*COL; i++)
  84.         blocks[i] = "";
  85.  
  86.     for (unsigned int i = 0; i < message.size(); i++) {
  87.         int s = key_col[i % COL];
  88.         int r = key_str[(i / COL) % STR];
  89.  
  90.         int index = COL * (r - 1) + s - 1;
  91.         char check = message[i];
  92.         blocks[index].push_back(message.at(i));
  93.     }
  94.  
  95.     for (unsigned int i = 0; i < STR*COL; i++)
  96.         cout << blocks[i] << endl;
  97.     return;
  98. }
  99.  
  100.  
  101. bool checkKeys(string key, int limit)
  102. {
  103.     if (static_cast<int>(key.length()) != limit) return false;
  104.  
  105.     for (int i = 0; i < limit; i++) {
  106.         if (!isdigit(key[i])) return false;
  107.         int cur = key[i] - '0';
  108.         if (cur <= 0 || cur > limit) return false;
  109.     }
  110.  
  111.     return true;
  112. }
  113.  
  114. vector<int> castKey(string stringKey)
  115. {
  116.     vector<int> key;
  117.     for (unsigned int i = 0; i < stringKey.length(); i++)
  118.         key.push_back(stringKey[i] - '0');
  119.     return key;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement