Неверное преобразование чисел в русские буквы С++
Пишу реализацию RSA. Столкнулся с проблемой: не может расшифровать русские буквы. Пробовал разные варианты:
setlocale(LC_ALL, "Rus");SetConsoleOutputCP(CP_UTF8);SetConsoleCP(CP_UTF8);
Они не работают.
Конкретно проблема в функции decrypt. Английский текст и символы работают корректно.
string encrypt(const std::string& message, const std::vector<mp::cpp_int>& public_key) {
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
mp::cpp_int n = public_key[0];
mp::cpp_int e = public_key[1];
std::ofstream outfile("encrypted_text.txt"); // Открываем файл для записи
if (!outfile.is_open()) {
std::cerr << "Не получилось записать данные в файл." << std::endl;
return "";
}
std::string encrypted_message;
for (std::size_t i = 0; i < message.size(); ++i) {
mp::cpp_int num = powm(static_cast<mp::cpp_int>(message[i]), e, n);
outfile << num;
// Если это не последний символ, добавляем разделитель
if (i != message.size() - 1) {
outfile << " ";
}
}
outfile.close(); // Закрываем файл
return "";
}
string decryptFromFile(const std::vector<mp::cpp_int>& private_key) { // main
mp::cpp_int n = private_key[0];
mp::cpp_int d = private_key[1];
std::ifstream infile("encrypted_text.txt"); // Открываем файл для чтения
if (!infile.is_open()) {
std::cerr << "Не получилось открыть файл для чтения." << std::endl;
return ""; // Можно обработать ошибку по-другому, если нужно
}
std::vector<mp::cpp_int> encrypted_blocks;
mp::cpp_int num;
while (infile >> num) {
encrypted_blocks.push_back(num);
}
infile.close(); // Закрываем файл
std::string decrypted_message;
for (std::size_t i = 0; i < encrypted_blocks.size(); ++i) {
decrypted_message += static_cast<char>(powm(encrypted_blocks[i], d, n).convert_to<int>());
// Если это не последний блок, добавляем пробел
if (i != encrypted_blocks.size() - 1) {
decrypted_message += "";
}
}
cout << decrypted_message;
return "";
}
string cout_encrypted_text() {
ifstream infile("encrypted_text.txt");
if (!infile.is_open()) {
cerr << "Unable to open the file for reading." << endl;
return "";
}
std::string line;
while (std::getline(infile, line)) {
std::istringstream iss(line);
int number;
while (iss >> number) {
std::cout << static_cast<unsigned char>(number) << "";
}
}
infile.close(); // Закрываем файл
return "";
}