Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <unistd.h>
- #include <vector>
- #include <locale>
- #define COL 4
- #define STR 2
- using namespace std;
- bool checkKeys(string key, int limit);
- void decrypt(string message, vector<int> key_col, vector<int> key_str);
- void crypt(string message, vector<int> key_col, vector<int> key_str);
- vector<int> castKey(string stringKey);
- int main()
- {
- /* Установка локали для корректной работы */
- setlocale(LC_ALL, "ru_RU.utf-8");
- string message;
- string mode;
- string key_str;
- string key_col;
- string answer;
- cout << "Данная программа осуществляет (де)шифрование по методу разнесения-рассечения\n";
- /*cout << "Введите режим работы (дешифровка/шифровка)\n";
- while (true) {
- getline(cin, mode);
- if (mode.compare("дешифровка") == 0 || mode.compare("шифровка") == 0) break;
- else cout << "Повторите ввод режима работы\n";
- }
- */
- mode = "шифровка";
- cout << "Введите строку для " << mode.substr(0, mode.length() - 2) << "и\n";
- while (true) {
- getline(cin, message);
- cout << "Это все? (да/нет)\n";
- getline(cin, answer);
- if (answer.compare("да") == 0) break;
- }/*
- cout << "Введите ключ для столбцов\n";
- while (true) {
- getline(cin, key_col);
- if (checkKeys(key_col, COL)) break;
- else cout << "Повторите ввод ключа\n";
- }
- */
- key_col = "4132";
- /*
- cout << "Введите ключ для строк\n";
- while (true) {
- getline(cin, key_str);
- if (checkKeys(key_str, STR)) break;
- else cout << "Повторите ввод ключа\n";
- }*/
- key_str = "21";
- vector<int> k_col = castKey(key_col);
- vector<int> k_str = castKey(key_str);
- if (mode.compare("дешифровка") == 0) decrypt(message, k_col, k_str);
- if (mode.compare("шифровка") == 0) crypt(message, k_col, k_str);
- return 0;
- }
- void decrypt(string message, vector<int> key_col, vector<int> key_str)
- {
- string blocks[STR * COL] = {""};
- return;
- }
- void crypt(string message, vector<int> key_col, vector<int> key_str)
- {
- string blocks[STR * COL];
- for (unsigned int i = 0; i < STR*COL; i++)
- blocks[i] = "";
- for (unsigned int i = 0; i < message.size(); i++) {
- int s = key_col[i % COL];
- int r = key_str[(i / COL) % STR];
- int index = COL * (r - 1) + s - 1;
- char check = message[i];
- blocks[index].push_back(message.at(i));
- }
- for (unsigned int i = 0; i < STR*COL; i++)
- cout << blocks[i] << endl;
- return;
- }
- bool checkKeys(string key, int limit)
- {
- if (static_cast<int>(key.length()) != limit) return false;
- for (int i = 0; i < limit; i++) {
- if (!isdigit(key[i])) return false;
- int cur = key[i] - '0';
- if (cur <= 0 || cur > limit) return false;
- }
- return true;
- }
- vector<int> castKey(string stringKey)
- {
- vector<int> key;
- for (unsigned int i = 0; i < stringKey.length(); i++)
- key.push_back(stringKey[i] - '0');
- return key;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement