Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- void Setup();
- void e_inter(), d_inter(), up_input(string &input);
- void __attribute__((constructor)) Setup();
- string Encrypt(string mess, string key);
- string Decrypt(string e_mess, string key);
- string KeyGen(short key_len);
- string get_file();
- char option;
- int Exit_app();
- string alfa;
- ///********
- int main()
- {
- cout << "1.encrypt" << endl;
- cout << "2.decrypt" << endl;
- cout << "3.exit" << endl;
- cout << "alege(1/2/3):";
- option = getch();
- system("cls");
- switch (option)
- {
- case '1':
- e_inter();
- break;
- case '2':
- d_inter();
- break;
- case '3':
- Exit_app();
- break;
- default:
- return main();
- }
- }
- void Setup()
- {
- srand(time(NULL));
- system("title V-ENCRYPTER");
- HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(h, 10);
- for (char c = 'A'; c <= 'Z'; c++)
- alfa += c;
- }
- int Exit_app()
- {
- cout << "Exit";
- for (int i = 0; i < 4; i++)
- {
- Sleep(1000);
- cout << '.';
- }
- return EXIT_SUCCESS;
- }
- ///
- string Encrypt(string mess, string key)
- {
- int i, j, poz, shift;
- string e_mess;
- const int len = mess.size(), key_len = key.size();
- for (i = 0, j = 0; i < len; i++, j++)
- {
- if (mess[i] == 32)
- {
- j--;
- e_mess += 32;
- continue;
- }
- poz = (int)mess[i] - 65;
- shift = (int)key[j % key_len] - 65;
- e_mess += alfa[(shift + poz) % 26];
- }
- return e_mess;
- }
- string Decrypt(string e_mess, string key)
- {
- string text;
- const int len = e_mess.size(), key_len = key.size();
- int i, j, poz, shift;
- for (i = 0, j = 0; i < len; i++, j++)
- {
- if (e_mess[i] == 32)
- {
- j--;
- text += 32;
- continue;
- }
- poz = (int)e_mess[i] - 65;
- shift = (int)key[j % key_len] - 65;
- text += alfa[(poz - shift + 26) % 26];
- }
- return text;
- }
- string get_file()
- {
- system("cls");
- string text = "";
- cout << "Asigurate ca fisieru e in folder cu aplicatia!" << endl;
- string file_name; ///***********
- cout << "Numele fisierului e:";
- cin.ignore();
- getline(cin, file_name);
- fstream fin;
- fin.close();
- fin.open("FILE.in", ios::in);
- if (!fin.is_open())
- {
- const time_t mili_t = 3000;
- cout << "Eroare!!!";
- Beep((mili_t / 6), mili_t);
- Sleep(mili_t / 3);
- return get_file();
- }
- else
- {
- string line;
- while (getline(fin, line))
- text.append(line);
- }
- fin.close();
- return text;
- }
- void up_input(string& input)
- {
- unsigned i;
- for (i = 0; i < input.size(); i++)
- input[i] = toupper(input[i]);
- }
- string KeyGen(short key_len)
- {
- string key;
- if (!key_len)
- key_len = rand() % 10;
- for (short i = 0; i < key_len; i++)
- key += alfa[rand() % alfa.size()];
- return key;
- }
- void e_inter()
- {
- bool ok;
- system("cls");
- char key_type;
- short key_len;
- string key, text;
- cout << "1.Encrypt:" << endl;
- cout << "2.Encrypt file:" << endl;
- do
- {
- option = getch();
- } while (key_type < '1' && key_type > '2');
- system("cls");
- cout << "(cheia e cuvant)" << endl;
- cout << "1.Introdu cheia!" << endl;
- cout << "2.Genereaza cheie random!\n";
- do
- {
- key_type = getch();
- } while (key_type < '1' && key_type > '2');
- system("cls");
- if (key_type == '2')
- {
- cout << "Lungimea cheii(apasa 0 daca vrei sa fie random):";
- do
- {
- cin >> key_len;
- } while (key_len < 0);
- do
- {
- key = KeyGen(key_len);
- cout << "Cheia generata e \"" << key << "\" esti de acord?(0-nu 1-da):";
- cin >> ok;
- } while (!ok);
- }
- else
- {
- cout << "Cheie:";
- cin >> key;
- }
- cout << endl;
- if (option == '1')
- {
- cout << "Text:\n";
- cin.ignore();
- getline(cin, text);
- }
- else
- {
- text = get_file();
- }
- up_input(text);
- up_input(key);
- cout << "\nTextul criptat e:" << endl;
- cout << Encrypt(text, key) << endl << endl;
- back_interface:
- {
- char opt;
- cout << "1.menu" << endl;
- cout << "2.inapoi" << endl;
- cout << "3.exit" << endl;
- cout << "alege:";
- opt = getch();
- system("cls");
- switch (opt)
- {
- case '1':
- main();
- break;
- case '2':
- e_inter();
- break;
- case '3':
- Exit_app();
- break;
- default:
- goto back_interface;
- }
- }
- }
- void d_inter()
- {
- string key, e_text;
- cout << "1.decrypt" << endl;
- cout << "2.decrypt file" << endl;
- do
- {
- option = getch();
- } while (option < '1' && option > '2');
- system("cls");
- cout << "cheie:";
- cin >> key;
- cin.ignore();
- if (option == '1')
- {
- cout << "Text encriptat:";
- getline(cin, e_text);
- }
- else
- {
- e_text = get_file();
- }
- up_input(key);
- up_input(e_text);
- cout << "Text decriptat:" << endl;
- cout << Decrypt(e_text, key) << endl;
- back_interface:
- {
- char opt;
- cout << "1.menu" << endl;
- cout << "2.inapoi" << endl;
- cout << "3.exit" << endl;
- cout << "alege:";
- opt = getch();
- system("cls");
- switch (opt)
- {
- case '1':
- main();
- break;
- case '2':
- e_inter();
- break;
- case '3':
- Exit_app();
- break;
- default:
- goto back_interface;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement