Created
November 27, 2023 19:06
-
-
Save sgf-dma/22d1dc6969c538129504bd0d5463c6dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <Windows.h> | |
using namespace std; | |
vector<string> splitToBlocks(const string& text, size_t blockSize) { | |
vector<string> blocks; | |
for (size_t i = 0; i < text.length(); i += blockSize) { | |
blocks.push_back(text.substr(i, blockSize)); | |
} | |
return blocks; | |
} | |
string encryption(const string& plain_text, int n, int m, const vector<int>& key_sequence) { | |
string padded_plain_text = plain_text; | |
if (padded_plain_text.length() % (n * m) != 0) { | |
int padding_length = (n * m) - (padded_plain_text.length() % (n * m)); | |
padded_plain_text.append(padding_length, ' '); // Дополнительный пробел для дополнения текста | |
} | |
vector<string> blocks = splitToBlocks(padded_plain_text, n * m); | |
vector<char> encrypted_chars; | |
for (const auto& block : blocks) { | |
vector<char> encrypted_block(n * m, ' '); | |
for (size_t i = 0; i < n * m; i++) { | |
int row = i / n; | |
int col = key_sequence[i % n] - 1; | |
encrypted_block[col * m + row] = block[i]; | |
} | |
encrypted_chars.insert(encrypted_chars.end(), encrypted_block.begin(), encrypted_block.end()); | |
} | |
string encrypted_message(encrypted_chars.begin(), encrypted_chars.end()); | |
return encrypted_message; | |
} | |
string decryption(const string& encrypted_message, int n, int m, const vector<int>& key_sequence) { | |
string padded_encrypted_message = encrypted_message; | |
if (padded_encrypted_message.length() % (n * m) != 0) { | |
int padding_length = (n * m) - (padded_encrypted_message.length() % (n * m)); | |
printf("abc\n"); | |
padded_encrypted_message.append(padding_length, ' '); // Дополнительный пробел для дополнения текста | |
} | |
vector<string> blocks = splitToBlocks(padded_encrypted_message, n * m); | |
vector<char> decrypted_chars; | |
for (const auto& block : blocks) { | |
vector<char> decrypted_block(n * m, ' '); | |
vector<int> inverse_key_sequence(n); | |
for (size_t i = 0; i < key_sequence.size(); i++) { | |
inverse_key_sequence[key_sequence[i] - 1] = i; | |
} | |
for (int i = 0; i < m; i++) { | |
for (int j = 0; j < n; j++) { | |
decrypted_block[inverse_key_sequence[j] + n*i] = block[i + m * j]; | |
} | |
} | |
decrypted_chars.insert(decrypted_chars.end(), decrypted_block.begin(), decrypted_block.end()); | |
} | |
string decrypted_message(decrypted_chars.begin(), decrypted_chars.end()); | |
return decrypted_message; | |
} | |
int main() { | |
SetConsoleCP(1251); | |
SetConsoleOutputCP(1251); | |
string plain_text; | |
int n, m; | |
vector<int> key_sequence; | |
int choice; | |
cout << "Выберите функцию (шифрование - 1, дешифрование - 2): "; | |
cin >> choice; | |
cin.ignore(); // очистка буфера ввода | |
if (choice == 1) { | |
cout << "Введите текст для шифрования: "; | |
getline(cin, plain_text); | |
cout << "Введите количество столбцов (n): "; | |
cin >> n; | |
cout << "Введите количество строк (m): "; | |
cin >> m; | |
cout << "Введите последовательность ключа для шифрования (через пробел): "; | |
for (int i = 0; i < n; i++) { | |
int key; | |
cin >> key; | |
key_sequence.push_back(key); | |
} | |
cin.ignore(); // очистка буфера | |
string encrypted_message = encryption(plain_text, n, m, key_sequence); | |
cout << "Зашифрованный текст: " << encrypted_message << endl; | |
} | |
else if (choice == 2) { | |
string encrypted_message; | |
cout << "Введите зашифрованный текст: "; | |
getline(cin, encrypted_message); | |
cout << "Введите количество столбцов (n): "; | |
cin >> n; | |
cout << "Введите количество строк (m): "; | |
cin >> m; | |
cout << "Введите последовательность ключа для дешифрования (через пробел): "; | |
for (int i = 0; i < n; i++) { | |
int key; | |
cin >> key; | |
key_sequence.push_back(key); | |
} | |
cin.ignore(); // очистка буфера | |
string decrypted_message = decryption(encrypted_message, n, m, key_sequence); | |
cout << "Расшифрованный текст: " << decrypted_message << endl; | |
} | |
else { | |
cout << "Неверный выбор." << endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment