Advertisement
fabis_sparks

Untitled

Sep 7th, 2021
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>  /*вывод русской раскладки*/
  3. #include <fstream>
  4. #include <ctime>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void randKey(char* key, const char* input);
  10. void console(char* input, char* key, char* result);
  11. int file(char* input, char* key, char* result);
  12. void encryption(const char* input, const char* key, char* result);
  13. void encryptionFile(const char* input, const char* key, char* result, ofstream& fout3);
  14. void decryption(char* input, const char* key, const char* result);
  15. void decryptionFile(char* input, const char* key, const char* result, ofstream& fout4);
  16. void mainMenu(char* input, char* key, char* result);
  17. void randMenuConsole(char* key, const char* input);
  18. void randMenuFile(char* key, const char* input, ifstream& fin2, ofstream& fout2, string path2);
  19. bool notOpen(ifstream& fin);
  20. bool notOpen(ofstream& fout);
  21. void readFile(ifstream& fin, char* input);
  22.  
  23. int main() {
  24.     SetConsoleCP(1251);// установка кодовой страницы win-cp 1251 в поток ввода
  25.     SetConsoleOutputCP(1251); // установка кодовой страницы win-cp 1251 в поток вывода
  26.     setlocale(LC_ALL, "rus");
  27.     srand(unsigned int(time(NULL)));
  28.  
  29.     char* input = new char[100000];
  30.     char* key = new char[100000];
  31.     char* result = new char[100000];
  32.  
  33.     mainMenu(input, key, result);
  34.  
  35.     delete[] input;
  36.     delete[] key;
  37.     delete[] result;
  38. }
  39.  
  40. void mainMenu(char* input, char* key, char* result) {
  41.     cout << "Выберете пункт меню: " << endl;
  42.     cout << "1. Работа с консолью" << endl;
  43.     cout << "2. Работа с файлом" << endl;
  44.     int choice;
  45.     cin >> choice;
  46.  
  47.     switch (choice) {
  48.     case 1:
  49.         console(input, key, result);
  50.         break;
  51.     case 2:
  52.         file(input, key, result);
  53.     }
  54. }
  55.  
  56. void randMenuConsole(char* key, const char* input) {
  57.     cout << "Выберите тип ввода ключа: 1 или 2\n";
  58.     int choice;
  59.     cin >> choice;
  60.     char* shortKey = new char[100000];
  61.     int inputLen = strlen(input);
  62.     int keyLen = strlen(key);
  63.  
  64.     switch (choice) {
  65.     case 1:
  66.         cout << "Введите ключ " << endl;
  67.         cin.ignore();                  /*игнор переноса строки*/
  68.         cin.getline(shortKey, 100000); /*считывает пробелы*/
  69.         if (strlen(shortKey) >= strlen(input)) {
  70.             for (int i = 0; i < inputLen; ++i) {
  71.                 key[i] = shortKey[i];
  72.             }
  73.             key[inputLen] = '\0';
  74.         }
  75.         else {
  76.             for (size_t i = 0; i < strlen(input) / strlen(shortKey); ++i) {
  77.                 for (size_t j = 0; j < strlen(shortKey); ++j) {
  78.                     key[i * strlen(shortKey) + j] = shortKey[j];
  79.                 }
  80.             }
  81.             size_t start = strlen(input) - (strlen(input) % strlen(shortKey));
  82.             for (size_t i = 0; i < strlen(input) % strlen(shortKey); ++i) {
  83.                 key[start + i] = shortKey[i];
  84.             }
  85.             key[start + strlen(input) % strlen(shortKey)] = '\0';
  86.         }
  87.         break;
  88.     case 2:
  89.         randKey(key, input);
  90.         cout << endl;
  91.     }
  92. }
  93.  
  94. void console(char* input, char* key, char* result) {
  95.     cout << "Введите строку:\n";
  96.     cin.ignore();                /*игнор переноса строки*/
  97.     cin.getline(input, 100000);  /*считывает пробелы*/
  98.     randMenuConsole(key, input);
  99.     cout << "Ключ:\n" << key << "\n\n";
  100.     cout << "Зашифрованный текст:\n";
  101.     encryption(input, key, result);
  102.     cout << endl;
  103.     cout << "Расшифрованный текст:\n";
  104.     decryption(input, key, result);
  105.     cout << endl;
  106. }
  107.  
  108. int file(char* input, char* key, char* result) {
  109.        string path;
  110.        cout << "Введите путь к 1-ому файлу" << endl;
  111.        cin >> path;
  112.  
  113.     ifstream fin1(path);
  114.  
  115.     if (notOpen(fin1))
  116.         return -1;
  117.  
  118.     readFile(fin1, input);
  119.  
  120.     fin1.close();
  121.  
  122.         string path2;
  123.         cout << "Введите путь ко 2-ому файлу" << endl;
  124.         cin >> path2;
  125.  
  126.     ifstream fin2;
  127.     ofstream fout2;
  128.  
  129.     randMenuFile(key, input, fin2, fout2, path2);
  130.  
  131.         string path3;
  132.         cout << "Введите путь к 3-ому файлу" << endl;
  133.         cin >> path3;
  134.  
  135.     ofstream fout3(path3);
  136.     notOpen(fout3);
  137.     encryptionFile(input, key, result, fout3);
  138.     fout3.close();
  139.  
  140.         string path4;
  141.         cout << "Введите путь к 4-ому файлу" << endl;
  142.         cin >> path4;
  143.  
  144.     ofstream fout4(path4);
  145.     notOpen(fout4);
  146.     decryptionFile(input, key, result, fout4);
  147.     fout4.close();
  148.  
  149.     return 0;
  150. }
  151.  
  152. bool notOpen(ifstream& fin) {
  153.     if (fin.is_open()) {
  154.         cout << "Все ОК! Файл открыт!\n" << endl;
  155.         return false;
  156.     }
  157.     else {
  158.         cout << "Файл не открыт!\n" << endl;
  159.         return true;
  160.     }
  161. }
  162.  
  163. bool notOpen(ofstream& fout) {
  164.     if (fout.is_open()) {
  165.         cout << "Все ОК! Файл открыт!\n" << endl;
  166.         return false;
  167.     }
  168.     else {
  169.         cout << "Файл не открыт!\n" << endl;
  170.         return true;
  171.     }
  172. }
  173.  
  174. void readFile(ifstream& fin, char* input) {
  175.     char* k = new char[100000];
  176.     fin.getline(input, 100000);
  177.     int inputLen = strlen(input);
  178.  
  179.     input[inputLen] = ' ';
  180.     input[inputLen + 1] = '\0';
  181.  
  182.     while (!fin.eof()) {
  183.  
  184.         fin.getline(k, 100000);
  185.         inputLen = strlen(input);
  186.  
  187.         for (size_t i = 0; i < strlen(k); ++i) {
  188.             input[inputLen + i] = k[i];
  189.         }
  190.  
  191.         input[inputLen + strlen(k)] = ' ';
  192.         input[inputLen + strlen(k) + 1] = '\0';
  193.     }
  194.     delete[] k;
  195. }
  196.  
  197. void randMenuFile(char* key, const char* input, ifstream& fin2, ofstream& fout2, string path2) {
  198.     cout << "Выберите тип ввода ключа: 1 или 2\n";
  199.     int choice;
  200.     cin >> choice;
  201.     char* shortKey = new char[100000];
  202.     int inputLen = strlen(input);
  203.  
  204.     switch (choice) {
  205.     case 1:
  206.         fin2.open(path2);
  207.         readFile(fin2, shortKey);
  208.  
  209.         if (strlen(shortKey) >= strlen(input)) {
  210.             for (int i = 0; i < inputLen; ++i) {
  211.                 key[i] = shortKey[i];
  212.             }
  213.             key[inputLen] = '\0';
  214.         }
  215.         else {
  216.             for (size_t i = 0; i < strlen(input) / strlen(shortKey); ++i) {
  217.                 for (size_t j = 0; j < strlen(shortKey); ++j) {
  218.                     key[i * strlen(shortKey) + j] = shortKey[j];
  219.                 }
  220.             }
  221.             size_t start = strlen(input) - (strlen(input) % strlen(shortKey));
  222.             for (size_t i = 0; i < strlen(input) % strlen(shortKey); ++i) {
  223.                 key[start + i] = shortKey[i];
  224.             }
  225.             key[start + strlen(input) % strlen(shortKey)] = '\0';
  226.         }
  227.         fin2.close();
  228.         break;
  229.     case 2:
  230.         fout2.open(path2);
  231.         randKey(key, input);
  232.         fout2 << key;
  233.         fout2.close();
  234.     }
  235. }
  236.  
  237. void encryption(const char* input, const char* key, char* result) {
  238.  
  239.     for (size_t i = 0; i < strlen(input); ++i) {
  240.         unsigned char symbol = char(input[i] ^ key[i]);
  241.         result[i] = char(input[i] ^ key[i]);
  242.         if (symbol > 31 && symbol < 127 || symbol > 161 && symbol < 255) {
  243.             cout << symbol;
  244.         }
  245.         else {
  246.             cout << " " << int(input[i] ^ key[i]) << " ";
  247.         }
  248.     }
  249.     result[strlen(input)] = '\0';
  250.     cout << endl;
  251. }
  252.  
  253. void encryptionFile(const char* input, const char* key, char* result, ofstream& fout3) {
  254.  
  255.     for (size_t i = 0; i < strlen(input); ++i) {
  256.         unsigned char symbol = char(input[i] ^ key[i]);
  257.         result[i] = char(input[i] ^ key[i]);
  258.         if (symbol > 31 && symbol < 127 || symbol > 161 && symbol < 255) {
  259.             fout3 << symbol;
  260.         }
  261.         else {
  262.             fout3 << int(input[i] ^ key[i]);
  263.         }
  264.     }
  265.     result[strlen(input)] = '\0';
  266. }
  267.  
  268. void decryption(char* input, const char* key, const char* result) {
  269.     for (size_t i = 0; i < strlen(key); ++i) {
  270.         input[i] = char(result[i] ^ key[i]);
  271.         cout << input[i];
  272.     }
  273.     input[strlen(key)] = '\0';
  274.     cout << endl;
  275. }
  276.  
  277. void decryptionFile(char* input, const char* key, const char* result, ofstream& fout4) {
  278.     for (size_t i = 0; i < strlen(key); ++i) {
  279.         input[i] = char(result[i] ^ key[i]);
  280.         fout4 << input[i];
  281.     }
  282.     input[strlen(key)] = '\0';
  283. }
  284.  
  285. void randKey(char* key, const char* input) {
  286.     for (size_t i = 0; i < strlen(input); ++i) {
  287.         key[i] = char(34 + rand() % (255 - 34));
  288.     }
  289.     key[strlen(input)] = '\0';
  290. }
  291.  
  292. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  293. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  294.  
  295. // Советы по началу работы
  296. //   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
  297. //   2. В окне Team Explorer можно подключиться к системе управления версиями.
  298. //   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
  299. //   4. В окне "Список ошибок" можно просматривать ошибки.
  300. //   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
  301. //   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement