psychotrip

One-time pad

Sep 20th, 2023
1,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. /*
  2.  * Вариант 9
  3.  * Напишите программу, позволяющую зашифровать и расшифровать сообщения с использованием одноразового блокнота.
  4.  * Входные и выходные данные запишите в файл типа .txt.
  5.  */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <windows.h>
  10.  
  11. using namespace std;
  12.  
  13. // Генерация ключа
  14. string generateKey(int length)
  15. {
  16.     string key;
  17.     for (int i = 0; i < length; i++)
  18.     {
  19.         char randomChar = 'A' + (rand() % 26); // 'A' - чтобы были заглавные буквы ('a' - строчные)
  20.                                                // 26 - столько букв в английском алфавите (если значение больше, то в ключе будут присутствовать служебные символы)
  21.         key += randomChar;
  22.     }
  23.  
  24.     return key;
  25. }
  26.  
  27. // Шифровка сообщения
  28. void encryptMessage(string message, string key, string& encryptedMessage)
  29. {
  30.     for (int i = 0; i < message.length(); i++)
  31.         encryptedMessage += message[i] ^ key[i];
  32. }
  33.  
  34. // Расшифровка сообщения
  35. void decryptMessage(string encryptedMessage, string key, string& decryptedMessage)
  36. {
  37.     for (int i = 0; i < encryptedMessage.length(); i++)
  38.         decryptedMessage += encryptedMessage[i] ^ key[i];
  39. }
  40.  
  41. int main()
  42. {
  43.     SetConsoleOutputCP(CP_UTF8);
  44.     srand(time(NULL));
  45.  
  46.     string message, encryptedMessage, decryptedMessage;
  47.  
  48.     ifstream FileIn("input.txt");
  49.     if (FileIn.is_open())
  50.     {
  51.         getline(FileIn, message);
  52.         FileIn.close();
  53.     }
  54.     else
  55.     {
  56.         cerr << "Невозможно открыть input.txt" << endl;
  57.         return 1;
  58.     }
  59.  
  60.     cout << "Исходное сообщение (файл input.txt): " << message << endl;
  61.  
  62.     string key = generateKey(message.length());
  63.     cout << "Сгенерированный ключ (файл key.txt): " << key << endl;
  64.  
  65.     encryptMessage(message, key, encryptedMessage);
  66.     cout << "Зашифрованное сообщение (файл encrypted.txt): " << encryptedMessage << endl;
  67.  
  68.     decryptMessage(encryptedMessage, key, decryptedMessage);
  69.     cout << "Расшифрованное сообщение (файл output.txt): " << decryptedMessage << endl;
  70.  
  71.     ofstream FileKey("key.txt");
  72.     if (FileKey.is_open())
  73.     {
  74.         FileKey << key;
  75.         FileKey.close();
  76.     }
  77.     else
  78.     {
  79.         cerr << "Невозможно открыть key.txt" << endl;
  80.         return 1;
  81.     }
  82.  
  83.     ofstream FileEn("encrypted.txt");
  84.     if (FileEn.is_open())
  85.     {
  86.         FileEn << encryptedMessage;
  87.         FileEn.close();
  88.     }
  89.     else
  90.     {
  91.         cerr << "Невозможно открыть encrypted.txt" << endl;
  92.         return 1;
  93.     }
  94.  
  95.     ofstream FileOut("output.txt");
  96.     if (FileOut.is_open())
  97.     {
  98.         FileOut << decryptedMessage;
  99.         FileOut.close();
  100.     }
  101.     else
  102.     {
  103.         cerr << "Невозможно открыть output.txt" << endl;
  104.         return 1;
  105.     }
  106.  
  107.     system("PAUSE");
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment