Есть несколько ошибок и опечаток в вашем коде. Давайте пройдемся по ним и исправим.
cpp Copy code
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <climits>
std::string vigenere_decrypt(const std::string& ciphertext, const std::string& key) {
std::string decrypted_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 < ciphertext.length(); ++i) {
char char_upper = std::toupper(ciphertext[i]);
if (std::isalpha(char_upper)) {
char key_char = upper_key[i % key_length];
int key_shift = key_char - 'A';
char decrypted_char = ((char_upper - 'A' - key_shift + 26) % 26 + 'A'); // Исправлено
decrypted_text.push_back(decrypted_char);
} else {
decrypted_text.push_back(ciphertext[i]);
}
}
return decrypted_text;
}
double calculate_index_of_coincidence(const std::string& text) {
std::map<char, int> frequencies;
for (char c : text) {
frequencies[std::toupper(c)]++;
}
double n = text.length();
double ic = 0.0;
for (const auto& pair : frequencies) {
ic += pair.second * (pair.second - 1);
}
ic /= (n * (n - 1));
return ic;
}
std::map<int, double> find_key_length(const std::string& ciphertext, int max_key_length = 20) {
std::map<int, double> likely_key_lengths;
for (int key_len = 1; key_len <= max_key_length; ++key_len) {
double ic_sum = 0.0;
for (int i = 0; i < key_len; ++i) {
std::string subtext;
for (size_t j = i; j < ciphertext.length(); j += key_len) {
subtext.push_back(ciphertext[j]);
}
ic_sum += calculate_index_of_coincidence(subtext);
}
double average_ic = ic_sum / key_len;
likely_key_lengths[key_len] = average_ic;
}
return likely_key_lengths;
}
int find_shift(const std::string& subtext, const std::map<char, double>& frequencies) {
std::map<char, int> text_frequencies;
for (char c : subtext) {
text_frequencies[std::toupper(c)]++;
}
int total = 0;
for (const auto& pair : text_frequencies) {
total += pair.second;
}
int min_diff = INT_MAX;
int best_shift = 0;
for (int shift = 0; shift < 26; ++shift) {
int diff = 0;
for (const auto& pair : frequencies) {
char shifted_char = ((pair.first - 'A' + shift) % 26 + 'A'); // Исправлено
double expected_freq = pair.second * total;
double actual_freq = text_frequencies[shifted_char];
diff += std::abs(expected_freq - actual_freq);
}
if (diff < min_diff) {
min_diff = diff;
best_shift = shift;
}
}
return best_shift;
}
std::string find_key(const std::string& ciphertext, int key_length, const std::map<char, double>& frequencies) {
std::string key;
for (int i = 0; i < key_length; ++i) {
std::string subtext;
for (size_t j = i; j < ciphertext.length(); j += key_length) {
subtext.push_back(ciphertext[j]);
}
int shift = find_shift(subtext, frequencies);
key.push_back(shift + 'A');
}
return 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<char>(file)), std::istreambuf_iterator<char>());
file.close();
return content;
}
int main() {
std::string input_text = read_file("C:\\Users\\kiril\\source\\repos\\ConsoleApplication1\\ConsoleApplication1\\en_text_encrypted.txt");
std::transform(input_text.begin(), input_text.end(), input_text.begin(), ::toupper);
input_text.erase(std::remove_if(input_text.begin(), input_text.end(), [](char c) { return !std::isalpha(c); }), input_text.end());
std::cout << "Шифротекст:\n" << input_text << "\n";
auto possible_key_lengths = find_key_length(input_text);
double ideal_ic = 0.068;
std::vector<std::pair<int, double>> sorted_key_lengths(possible_key_lengths.begin(), possible_key_lengths.end());
std::sort(sorted_key_lengths.begin(), sorted_key_lengths.end(), [ideal_ic](const std::pair<int, double>& a, const std::pair<int, double>& b) {
return std::abs(a.second - ideal_ic) < std::abs(b.second - ideal_ic);
});
std::map<char, double> english_frequencies = {
{'A', 0.082}, {'B', 0.015}, {'C', 0.028}, {'D', 0.043}, {'E', 0.13}, {'F', 0.022},
{'G', 0.02}, {'H', 0.061}, {'I', 0.07}, {'J', 0.0015}, {'K', 0.0077}, {'L', 0.04},
{'M', 0.024}, {'N', 0.067}, {'O', 0.075}, {'P', 0.019}, {'Q', 0.00095}, {'R', 0.06},
{'S', 0.063}, {'T', 0.091}, {'U', 0.028}, {'V', 0.0098}, {'W', 0.024}, {'X', 0.0015},
{'Y', 0.02}, {'Z', 0.00074}
};
for (size_t i = 0; i < 8 && i < sorted_key_lengths.size(); ++i) {
int key_length = sorted_key_lengths[i].first;
std::string key = find_key(input_text, key_length, english_frequencies);
std::cout << "Длина ключа: " << key_length << "\n";
std::cout << "Найденный ключ: " << key << "\n";
std::string decrypted_text = vigenere_decrypt(input_text, key);
std::cout << "Расшифрованный текст:\n" << decrypted_text << "\n\n";
}
return 0;
}
Теперь этот код должен работать корректно для расшифровки текста, зашифрованного методом Виженера. Убедитесь, что файл, который вы пытаетесь прочитать, существует и доступен.