Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h> /*вывод русской раскладки*/
- #include <fstream>
- #include <ctime>
- #include <string>
- using namespace std;
- void randKey(char* key, const char* input);
- void console(char* input, char* key, char* result);
- int file(char* input, char* key, char* result);
- void encryption(const char* input, const char* key, char* result);
- void encryptionFile(const char* input, const char* key, char* result, ofstream& fout3);
- void decryption(char* input, const char* key, const char* result);
- void decryptionFile(char* input, const char* key, const char* result, ofstream& fout4);
- void mainMenu(char* input, char* key, char* result);
- void randMenuConsole(char* key, const char* input);
- void randMenuFile(char* key, const char* input, ifstream& fin2, ofstream& fout2, string path2);
- bool notOpen(ifstream& fin);
- bool notOpen(ofstream& fout);
- void readFile(ifstream& fin, char* input);
- int main() {
- SetConsoleCP(1251);// установка кодовой страницы win-cp 1251 в поток ввода
- SetConsoleOutputCP(1251); // установка кодовой страницы win-cp 1251 в поток вывода
- setlocale(LC_ALL, "rus");
- srand(unsigned int(time(NULL)));
- char* input = new char[100000];
- char* key = new char[100000];
- char* result = new char[100000];
- mainMenu(input, key, result);
- delete[] input;
- delete[] key;
- delete[] result;
- }
- void mainMenu(char* input, char* key, char* result) {
- cout << "Выберете пункт меню: " << endl;
- cout << "1. Работа с консолью" << endl;
- cout << "2. Работа с файлом" << endl;
- int choice;
- cin >> choice;
- switch (choice) {
- case 1:
- console(input, key, result);
- break;
- case 2:
- file(input, key, result);
- }
- }
- void randMenuConsole(char* key, const char* input) {
- cout << "Выберите тип ввода ключа: 1 или 2\n";
- int choice;
- cin >> choice;
- char* shortKey = new char[100000];
- int inputLen = strlen(input);
- int keyLen = strlen(key);
- switch (choice) {
- case 1:
- cout << "Введите ключ " << endl;
- cin.ignore(); /*игнор переноса строки*/
- cin.getline(shortKey, 100000); /*считывает пробелы*/
- if (strlen(shortKey) >= strlen(input)) {
- for (int i = 0; i < inputLen; ++i) {
- key[i] = shortKey[i];
- }
- key[inputLen] = '\0';
- }
- else {
- for (size_t i = 0; i < strlen(input) / strlen(shortKey); ++i) {
- for (size_t j = 0; j < strlen(shortKey); ++j) {
- key[i * strlen(shortKey) + j] = shortKey[j];
- }
- }
- size_t start = strlen(input) - (strlen(input) % strlen(shortKey));
- for (size_t i = 0; i < strlen(input) % strlen(shortKey); ++i) {
- key[start + i] = shortKey[i];
- }
- key[start + strlen(input) % strlen(shortKey)] = '\0';
- }
- break;
- case 2:
- randKey(key, input);
- cout << endl;
- }
- }
- void console(char* input, char* key, char* result) {
- cout << "Введите строку:\n";
- cin.ignore(); /*игнор переноса строки*/
- cin.getline(input, 100000); /*считывает пробелы*/
- randMenuConsole(key, input);
- cout << "Ключ:\n" << key << "\n\n";
- cout << "Зашифрованный текст:\n";
- encryption(input, key, result);
- cout << endl;
- cout << "Расшифрованный текст:\n";
- decryption(input, key, result);
- cout << endl;
- }
- int file(char* input, char* key, char* result) {
- string path;
- cout << "Введите путь к 1-ому файлу" << endl;
- cin >> path;
- ifstream fin1(path);
- if (notOpen(fin1))
- return -1;
- readFile(fin1, input);
- fin1.close();
- string path2;
- cout << "Введите путь ко 2-ому файлу" << endl;
- cin >> path2;
- ifstream fin2;
- ofstream fout2;
- randMenuFile(key, input, fin2, fout2, path2);
- string path3;
- cout << "Введите путь к 3-ому файлу" << endl;
- cin >> path3;
- ofstream fout3(path3);
- notOpen(fout3);
- encryptionFile(input, key, result, fout3);
- fout3.close();
- string path4;
- cout << "Введите путь к 4-ому файлу" << endl;
- cin >> path4;
- ofstream fout4(path4);
- notOpen(fout4);
- decryptionFile(input, key, result, fout4);
- fout4.close();
- return 0;
- }
- bool notOpen(ifstream& fin) {
- if (fin.is_open()) {
- cout << "Все ОК! Файл открыт!\n" << endl;
- return false;
- }
- else {
- cout << "Файл не открыт!\n" << endl;
- return true;
- }
- }
- bool notOpen(ofstream& fout) {
- if (fout.is_open()) {
- cout << "Все ОК! Файл открыт!\n" << endl;
- return false;
- }
- else {
- cout << "Файл не открыт!\n" << endl;
- return true;
- }
- }
- void readFile(ifstream& fin, char* input) {
- char* k = new char[100000];
- fin.getline(input, 100000);
- int inputLen = strlen(input);
- input[inputLen] = ' ';
- input[inputLen + 1] = '\0';
- while (!fin.eof()) {
- fin.getline(k, 100000);
- inputLen = strlen(input);
- for (size_t i = 0; i < strlen(k); ++i) {
- input[inputLen + i] = k[i];
- }
- input[inputLen + strlen(k)] = ' ';
- input[inputLen + strlen(k) + 1] = '\0';
- }
- delete[] k;
- }
- void randMenuFile(char* key, const char* input, ifstream& fin2, ofstream& fout2, string path2) {
- cout << "Выберите тип ввода ключа: 1 или 2\n";
- int choice;
- cin >> choice;
- char* shortKey = new char[100000];
- int inputLen = strlen(input);
- switch (choice) {
- case 1:
- fin2.open(path2);
- readFile(fin2, shortKey);
- if (strlen(shortKey) >= strlen(input)) {
- for (int i = 0; i < inputLen; ++i) {
- key[i] = shortKey[i];
- }
- key[inputLen] = '\0';
- }
- else {
- for (size_t i = 0; i < strlen(input) / strlen(shortKey); ++i) {
- for (size_t j = 0; j < strlen(shortKey); ++j) {
- key[i * strlen(shortKey) + j] = shortKey[j];
- }
- }
- size_t start = strlen(input) - (strlen(input) % strlen(shortKey));
- for (size_t i = 0; i < strlen(input) % strlen(shortKey); ++i) {
- key[start + i] = shortKey[i];
- }
- key[start + strlen(input) % strlen(shortKey)] = '\0';
- }
- fin2.close();
- break;
- case 2:
- fout2.open(path2);
- randKey(key, input);
- fout2 << key;
- fout2.close();
- }
- }
- void encryption(const char* input, const char* key, char* result) {
- for (size_t i = 0; i < strlen(input); ++i) {
- unsigned char symbol = char(input[i] ^ key[i]);
- result[i] = char(input[i] ^ key[i]);
- if (symbol > 31 && symbol < 127 || symbol > 161 && symbol < 255) {
- cout << symbol;
- }
- else {
- cout << " " << int(input[i] ^ key[i]) << " ";
- }
- }
- result[strlen(input)] = '\0';
- cout << endl;
- }
- void encryptionFile(const char* input, const char* key, char* result, ofstream& fout3) {
- for (size_t i = 0; i < strlen(input); ++i) {
- unsigned char symbol = char(input[i] ^ key[i]);
- result[i] = char(input[i] ^ key[i]);
- if (symbol > 31 && symbol < 127 || symbol > 161 && symbol < 255) {
- fout3 << symbol;
- }
- else {
- fout3 << int(input[i] ^ key[i]);
- }
- }
- result[strlen(input)] = '\0';
- }
- void decryption(char* input, const char* key, const char* result) {
- for (size_t i = 0; i < strlen(key); ++i) {
- input[i] = char(result[i] ^ key[i]);
- cout << input[i];
- }
- input[strlen(key)] = '\0';
- cout << endl;
- }
- void decryptionFile(char* input, const char* key, const char* result, ofstream& fout4) {
- for (size_t i = 0; i < strlen(key); ++i) {
- input[i] = char(result[i] ^ key[i]);
- fout4 << input[i];
- }
- input[strlen(key)] = '\0';
- }
- void randKey(char* key, const char* input) {
- for (size_t i = 0; i < strlen(input); ++i) {
- key[i] = char(34 + rand() % (255 - 34));
- }
- key[strlen(input)] = '\0';
- }
- // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
- // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
- // Советы по началу работы
- // 1. В окне обозревателя решений можно добавлять файлы и управлять ими.
- // 2. В окне Team Explorer можно подключиться к системе управления версиями.
- // 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
- // 4. В окне "Список ошибок" можно просматривать ошибки.
- // 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
- // 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement