#include <iostream> #include <fstream> #include <string> #include <algorithm> #include…
- User
#include #include #include #include #include
std::string vigenere_encrypt(const std::string& plain_text, const std::string& key) { std::string encrypted_text; int key_length = key.length(); std::string upper_key = key; std::transform(upper_key.begin(), upper_key.end(), upper_key.begin(), ::toupper);
for (size_t i = 0; i < plain_text.length(); ++i) { char c = plain_text[i]; if (std::isalpha(c)) { char key_char = upper_key[i % key_length]; int shift = key_char - 'A'; if (std::islower(c)) { encrypted_text.push_back(((c - 'a' + shift) % 26 + 'a')); } else { encrypted_text.push_back(((c - 'A' + shift) % 26 + 'A')); } } else { encrypted_text.push_back(c); } } return encrypted_text;}
std::string clean_key(const std::string& key) { std::string cleaned_key; for (char c : key) { if (std::isalpha(c)) { cleaned_key.push_back(c); } } return cleaned_key; }
std::string read_file(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) { std::cerr << "Ошибка: файл " << filename << " не может быть открыт.\n"; exit(1); } std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator()); file.close(); return content; }
void write_file(const std::string& filename, const std::string& content) { std::ofstream file(filename); if (!file.is_open()) { std::cerr << "Ошибка: файл " << filename << " не может быть открыт для записи.\n"; exit(1); } file << content; file.close(); }
std::string prepare_text(const std::string& text) { std::string prepared_text; for (char c : text) { if (std::isalpha(c)) { prepared_text.push_back(std::toupper(c)); } } return prepared_text; }
int main() { std::string key = read_file("C:\Users\kiril\OneDrive\Рабочий стол\Мои\1\en\key.txt"); key = clean_key(key);
if (key.empty()) { std::cerr << "Ошибка: ключ не может быть пустым после удаления лишних символов.\n"; return 1; } std::string input_text = read_file("C:\\Users\\kiril\\OneDrive\\Рабочий стол\\Мои\\1\\en\\en_text_input.txt"); std::string plain_text = prepare_text(input_text); std::string encrypted_text = vigenere_encrypt(plain_text, key); std::cout << "Ключ: " << key << "\n"; std::cout << "Зашифрованный текст: " << encrypted_text << "\n"; write_file("en_text_encrypted.txt", encrypted_text); return 0;}
Код не работает как надо, проверь его на ошибки, а так же добавь поддержку русской локали
- Assistant