Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //written by VLAD
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <time.h>
- #include <locale>
- #include <Windows.h>
- #include <fstream>
- using namespace std;
- char* input(char*& path, int& raw_length, int& msg_length) {
- int length;
- ifstream is;
- is.open(path, ios::binary);
- is.seekg(0, ios::end);
- length = is.tellg();
- raw_length = length;
- char* buffer = new char[length];
- is.seekg(0, ios::beg);
- is.read(buffer, length);
- is.close();
- for (int i = 0; i < length; ++i) {
- if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13)
- msg_length++;
- }
- return buffer;
- }
- void output(char* buffer, const int& raw_length) {
- ofstream out;
- out.open("crypted_msg.txt", ios::binary);
- out.write(buffer, raw_length);
- out.close();
- }
- // шифр цезаря
- void caesar(const int& change_Caesar, char*& encrypt, char*& alphabet, const int& len, const int& lenalp) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (encrypt[i] == alphabet[j]) {
- if (j + change_Caesar >= lenalp)
- help = alphabet[change_Caesar + j - lenalp];
- else
- help = alphabet[j + change_Caesar];
- encrypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Зашифровано шифром цезаря" << endl;
- }
- // шифр атбаш
- void atbash(char*& encrypt, char*& alphabet, const int& len, const int& lenalp) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (encrypt[i] == alphabet[j]) {
- help = alphabet[lenalp - j - 1];
- encrypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Зашифровано шифром атбаша" << endl;
- }
- // шифр виженера
- void weiner(char*& encrypt, char*& alphabet, const int& len, const string& key, const int& lenalp) {
- int* change = new int[len];
- int lenkey = key.length();
- char help;
- int help2;
- for (int i = 0, k = 0; i < len; ++i) {
- if (k + 1 != lenkey) {
- help = key[k];
- k++;
- }
- else {
- help = key[k];
- k = 0;
- }
- for (int j = 0; j < lenalp; ++j) {
- if (help == alphabet[j]) {
- help2 = j + 1;
- change[i] = help2;
- }
- }
- }
- for (int i = 0; i < len; ++i) {
- help2 = change[i];
- for (int j = 0; j < lenalp; ++j) {
- if (encrypt[i] == alphabet[j]) {
- if (j + help2 >= lenalp)
- help = alphabet[help2 + j - lenalp];
- else
- help = alphabet[j + help2];
- encrypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- delete[] change;
- cout << " - " << "Зашифровано шифром виженера" << endl;
- }
- // шифр обратный
- void reverse(char*& encrypt, const int& len) {
- for (int i = 0; i < len / 2; ++i) {
- char rev = encrypt[i];
- encrypt[i] = encrypt[len - i - 1];
- encrypt[len - i - 1] = rev;
- }
- for (int i = 0; i < len; ++i)
- cout << encrypt[i];
- cout << " - " << "Зашифровано шифром обратного порядка" << endl;
- }
- // шифр a1z26
- void a1z26(char*& encrypt, char*& alphabet, const int& len, const int& lenalp, char*& fbuffer, const int& fbuffer_size, char*& sm_buffer, const int& change_Caesar) {
- int sm_count = 0, b_count = 0;
- for (int i = 0; i < (fbuffer_size - 5); ) {
- if ((int)encrypt[b_count] == 32 || (int)encrypt[b_count] == 10 || (int)encrypt[b_count] == 13) {
- fbuffer[i] = encrypt[b_count];
- b_count++;
- i++;
- }
- else {
- for (int j = 0; j < lenalp; ++j) {
- if (sm_buffer[sm_count] == alphabet[j]) {
- if ((j + 1) < 10) {
- fbuffer[i] = '0';
- fbuffer[i + 1] = (j + 1) + '0';
- fbuffer[i + 2] = '-';
- }
- else {
- fbuffer[i] = ((j + 1) / 10) + '0';
- fbuffer[i + 1] = ((j + 1) % 10) + '0';
- fbuffer[i + 2] = '-';
- }
- cout << fbuffer[i] << fbuffer[i + 1] << fbuffer[i+2];
- i += 3;
- break;
- }
- }
- b_count++;
- sm_count++;
- }
- }
- if (change_Caesar < 10) {
- fbuffer[(fbuffer_size - 1) - 1] = '0';
- fbuffer[(fbuffer_size - 1)] = change_Caesar + '0';
- }
- else {
- fbuffer[(fbuffer_size - 1) - 1] = (change_Caesar / 10) + '0';
- fbuffer[(fbuffer_size - 1)] = (change_Caesar % 10) + '0';
- }
- fbuffer[(fbuffer_size - 1) - 2] = '-';
- if (alphabet[1] == 'b') {
- fbuffer[(fbuffer_size - 1) - 3] = '2';
- fbuffer[(fbuffer_size - 1) - 4] = '5';
- }
- else {
- fbuffer[(fbuffer_size - 1) - 3] = '6';
- fbuffer[(fbuffer_size - 1) - 4] = '6';
- }
- cout << " - " << "Зашифровано шифром a1z26" << endl << endl;
- }
- void DEreverse(char*& encrypt, const int& len) {
- for (int i = 0; i < len / 2; ++i) {
- char rev = encrypt[i];
- encrypt[i] = encrypt[len - i - 1];
- encrypt[len - i - 1] = rev;
- }
- for (int i = 0; i < len; ++i)
- cout << encrypt[i];
- cout << " - " << "Расшифровано шифром обратного порядка" << endl;
- }
- void DEweiner(char*& crypt, char*& alphabet, const int& len, const string& key, const int& lenalp) {
- int lenkey = key.length();
- int* change = new int[len];
- char help;
- int help2;
- for (int i = 0, k = 0; i < len; ++i) {
- if (k + 1 != lenkey) {
- help = key[k];
- k++;
- }
- else {
- help = key[k];
- k = 0;
- }
- for (int j = 0; j < lenalp; ++j) {
- if (help == alphabet[j]) {
- help2 = j + 1;
- change[i] = help2;
- }
- }
- }
- for (int i = 0; i < len; ++i) {
- help2 = change[i];
- for (int j = 0; j < lenalp; ++j) {
- if (crypt[i] == alphabet[j]) {
- if (j + (lenalp - help2) >= lenalp)
- help = alphabet[(lenalp - help2) + j - lenalp];
- else
- help = alphabet[j + (lenalp - help2)];
- crypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Расшифровано шифром вижинера" << endl;
- }
- // дешифр атбаш
- void DEatbash(char*& crypt, char*& alphabet, const int& len, const int& lenalp) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (crypt[i] == alphabet[j]) {
- help = alphabet[lenalp - j - 1];
- crypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Расшифровано шифром атбаша" << endl;
- }
- // дешифр цезаря
- void DEcaesar(const int& DEchange_Caesar, char*& crypt, char*& alphabet, int& len, const int& lenalp, char*& buffer, int& raw_length) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (crypt[i] == alphabet[j]) {
- if (j + (lenalp - DEchange_Caesar) >= lenalp)
- help = alphabet[(lenalp - DEchange_Caesar) + j - lenalp];
- else
- help = alphabet[j + (lenalp - DEchange_Caesar)];
- cout << help;
- crypt[i] = help;
- break;
- }
- }
- }
- cout << " - " << "Расшифровано шифром цезаря" << endl << endl;
- int sm_count = 0, b_count = 0;
- len = ((raw_length - 5) - len * 3) + len;
- char* fbuffer = new char[len];
- for (int i = 0; i < raw_length - 5; ) {
- if ((int)buffer[i] == 32 || (int)buffer[i] == 10 || (int)buffer[i] == 13) {
- fbuffer[sm_count] = buffer[i];
- sm_count++;
- i++;
- }
- else {
- fbuffer[sm_count] = crypt[b_count];
- b_count++;
- sm_count++;
- i += 3;
- }
- }
- crypt = fbuffer;
- }
- int setting_up(char first_letter, char*& lang, int& change_Caesar, bool& crypt, int& lenalp, char*& buffer, char*& n_buffer, int& raw_length, int& msg_length) {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "Russian");
- srand(time(NULL));
- char* alphabetENG = new char[26]{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
- char* alphabetRUS = new char[33]{ 'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я' };
- if (int(first_letter) >= 97 && int(first_letter) <= 122) {
- lang = alphabetENG;
- crypt = true;
- lenalp = 26;
- }
- else if (((int)(first_letter) >= -32 && (int)(first_letter) <= -1) || (int)first_letter == -72) {
- lang = alphabetRUS;
- crypt = true;
- lenalp = 33;
- }
- else if (first_letter - '0' >= 0 && first_letter - '0' < 10) {
- crypt = false;
- if (buffer[(raw_length - 1) - 3] == '2') {
- lang = alphabetENG;
- lenalp = 26;
- }
- else {
- lang = alphabetRUS;
- lenalp = 33;
- }
- }
- else {
- cout << "error, wrong text language or crypted msg";
- system("pause");
- }
- if (crypt == true)
- change_Caesar = rand() % 25 + 1;
- else {
- if (buffer[(raw_length - 1) - 1] == '0')
- change_Caesar = buffer[(raw_length - 1)] - '0';
- else
- change_Caesar = ((buffer[(raw_length - 1) - 1] - '0') * 10) + (buffer[(raw_length - 1)] - '0');
- }
- int k = 0;
- char* sm_buffer;
- if (crypt == true) {
- sm_buffer = new char[msg_length];
- for (int i = 0; i < raw_length; ) {
- if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13) {
- sm_buffer[k] = buffer[i];
- ++k;
- ++i;
- }
- else
- i++;
- }
- }
- else {
- msg_length = (msg_length - 5) / 3;
- sm_buffer = new char[msg_length];
- for (int i = 0; i < (raw_length - 5); ) {
- if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13) {
- if (buffer[i] == '0') {
- sm_buffer[k] = lang[(buffer[i + 1] - '0') - 1];
- k++;
- i += 3;
- }
- else {
- sm_buffer[k] = lang[(((buffer[i] - '0') * 10) + (buffer[i + 1] - '0')) - 1];
- k++;
- i += 3;
- }
- }
- else
- i++;
- }
- }
- n_buffer = sm_buffer;
- return 1;
- }
- int main(int argc, char* argv[]) {
- int raw_length, change_Caesar, lenalp, msg_length = 0;
- char* lang;
- char* sm_buffer;
- bool crypt = true;
- char* buffer = input(argv[1], raw_length, msg_length);
- if (setting_up(buffer[0], lang, change_Caesar, crypt, lenalp, buffer, sm_buffer, raw_length, msg_length) == 0)
- return 0;
- string key;
- char* fbuffer;
- int fbuffer_size;
- cout << "Введите ключ: ";
- getline(cin, key);
- if (crypt == true) {
- fbuffer_size = 3 * msg_length + (raw_length - msg_length) + 5;
- fbuffer = new char[fbuffer_size];
- caesar(change_Caesar, sm_buffer, lang, msg_length, lenalp);
- atbash(sm_buffer, lang, msg_length, lenalp);
- weiner(sm_buffer, lang, msg_length, key, lenalp);
- reverse(sm_buffer, msg_length);
- a1z26(buffer, lang, raw_length, lenalp, fbuffer, fbuffer_size, sm_buffer, change_Caesar);
- output(fbuffer, fbuffer_size);
- delete[] fbuffer;
- }
- else {
- DEreverse(sm_buffer, msg_length);
- DEweiner(sm_buffer, lang, msg_length, key, lenalp);
- DEatbash(sm_buffer, lang, msg_length, lenalp);
- DEcaesar(change_Caesar, sm_buffer, lang, msg_length, lenalp, buffer, raw_length);
- output(sm_buffer, msg_length);
- }
- delete[] buffer;
- delete[] sm_buffer;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment