Necto

file crypt

Sep 1st, 2022
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.88 KB | None | 0 0
  1. //written by VLAD
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>  
  5. #include <time.h>
  6. #include <locale>
  7. #include <Windows.h>
  8. #include <fstream>
  9. using namespace std;
  10. char* input(char*& path, int& raw_length, int& msg_length) {
  11.     int length;
  12.     ifstream is;
  13.     is.open(path, ios::binary);
  14.     is.seekg(0, ios::end);
  15.     length = is.tellg();
  16.     raw_length = length;
  17.     char* buffer = new char[length];
  18.     is.seekg(0, ios::beg);
  19.     is.read(buffer, length);
  20.     is.close();
  21.     for (int i = 0; i < length; ++i) {
  22.         if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13)
  23.             msg_length++;
  24.     }
  25.     return buffer;
  26. }
  27. void output(char* buffer, const int& raw_length) {
  28.     ofstream out;
  29.     out.open("crypted_msg.txt", ios::binary);
  30.     out.write(buffer, raw_length);
  31.     out.close();
  32. }
  33. // шифр цезаря
  34. void caesar(const int& change_Caesar, char*& encrypt, char*& alphabet, const int& len, const int& lenalp) {
  35.     char help;
  36.     for (int i = 0; i < len; ++i) {
  37.         for (int j = 0; j < lenalp; ++j) {
  38.             if (encrypt[i] == alphabet[j]) {
  39.                 if (j + change_Caesar >= lenalp)
  40.                     help = alphabet[change_Caesar + j - lenalp];
  41.                 else
  42.                     help = alphabet[j + change_Caesar];
  43.                 encrypt[i] = help;
  44.                 cout << help;
  45.                 break;
  46.             }
  47.         }
  48.     }
  49.     cout << " - " << "Зашифровано шифром цезаря" << endl;
  50. }
  51. // шифр атбаш
  52. void atbash(char*& encrypt, char*& alphabet, const int& len, const int& lenalp) {
  53.     char help;
  54.     for (int i = 0; i < len; ++i) {
  55.         for (int j = 0; j < lenalp; ++j) {
  56.             if (encrypt[i] == alphabet[j]) {
  57.                 help = alphabet[lenalp - j - 1];
  58.                 encrypt[i] = help;
  59.                 cout << help;
  60.                 break;
  61.             }
  62.         }
  63.     }
  64.     cout << " - " << "Зашифровано шифром атбаша" << endl;
  65. }
  66. // шифр виженера
  67. void weiner(char*& encrypt, char*& alphabet, const int& len, const string& key, const int& lenalp) {
  68.     int* change = new int[len];
  69.     int lenkey = key.length();
  70.     char help;
  71.     int help2;
  72.     for (int i = 0, k = 0; i < len; ++i) {
  73.         if (k + 1 != lenkey) {
  74.             help = key[k];
  75.             k++;
  76.         }
  77.         else {
  78.             help = key[k];
  79.             k = 0;
  80.         }
  81.         for (int j = 0; j < lenalp; ++j) {
  82.             if (help == alphabet[j]) {
  83.                 help2 = j + 1;
  84.                 change[i] = help2;
  85.             }
  86.         }
  87.     }
  88.     for (int i = 0; i < len; ++i) {
  89.         help2 = change[i];
  90.         for (int j = 0; j < lenalp; ++j) {
  91.             if (encrypt[i] == alphabet[j]) {
  92.                 if (j + help2 >= lenalp)
  93.                     help = alphabet[help2 + j - lenalp];
  94.                 else
  95.                     help = alphabet[j + help2];
  96.                 encrypt[i] = help;
  97.                 cout << help;
  98.                 break;
  99.             }
  100.         }
  101.     }
  102.     delete[] change;
  103.     cout << " - " << "Зашифровано шифром виженера" << endl;
  104. }
  105. // шифр обратный
  106. void reverse(char*& encrypt, const int& len) {
  107.     for (int i = 0; i < len / 2; ++i) {
  108.         char rev = encrypt[i];
  109.         encrypt[i] = encrypt[len - i - 1];
  110.         encrypt[len - i - 1] = rev;
  111.     }
  112.     for (int i = 0; i < len; ++i)
  113.         cout << encrypt[i];
  114.     cout << " - " << "Зашифровано шифром обратного порядка" << endl;
  115. }
  116. // шифр a1z26
  117. 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) {
  118.     int sm_count = 0, b_count = 0;
  119.     for (int i = 0; i < (fbuffer_size - 5); ) {
  120.         if ((int)encrypt[b_count] == 32 || (int)encrypt[b_count] == 10 || (int)encrypt[b_count] == 13) {
  121.             fbuffer[i] = encrypt[b_count];
  122.             b_count++;
  123.             i++;
  124.         }
  125.         else {
  126.  
  127.             for (int j = 0; j < lenalp; ++j) {
  128.                 if (sm_buffer[sm_count] == alphabet[j]) {
  129.                     if ((j + 1) < 10) {
  130.                         fbuffer[i] = '0';
  131.                         fbuffer[i + 1] = (j + 1) + '0';
  132.                         fbuffer[i + 2] = '-';
  133.                     }
  134.                     else {
  135.                         fbuffer[i] = ((j + 1) / 10) + '0';
  136.                         fbuffer[i + 1] = ((j + 1) % 10) + '0';
  137.                         fbuffer[i + 2] = '-';
  138.                     }
  139.                     cout << fbuffer[i] << fbuffer[i + 1] << fbuffer[i+2];
  140.                     i += 3;
  141.                     break;
  142.                 }
  143.             }
  144.             b_count++;
  145.             sm_count++;
  146.         }
  147.     }
  148.     if (change_Caesar < 10) {
  149.         fbuffer[(fbuffer_size - 1) - 1] = '0';
  150.         fbuffer[(fbuffer_size - 1)] = change_Caesar + '0';
  151.     }
  152.     else {
  153.         fbuffer[(fbuffer_size - 1) - 1] = (change_Caesar / 10) + '0';
  154.         fbuffer[(fbuffer_size - 1)] = (change_Caesar % 10) + '0';
  155.     }
  156.     fbuffer[(fbuffer_size - 1) - 2] = '-';
  157.     if (alphabet[1] == 'b') {
  158.         fbuffer[(fbuffer_size - 1) - 3] = '2';
  159.         fbuffer[(fbuffer_size - 1) - 4] = '5';
  160.     }
  161.     else {
  162.         fbuffer[(fbuffer_size - 1) - 3] = '6';
  163.         fbuffer[(fbuffer_size - 1) - 4] = '6';
  164.     }
  165.  
  166.     cout << " - " << "Зашифровано шифром a1z26" << endl << endl;
  167. }
  168. void DEreverse(char*& encrypt, const int& len) {
  169.     for (int i = 0; i < len / 2; ++i) {
  170.         char rev = encrypt[i];
  171.         encrypt[i] = encrypt[len - i - 1];
  172.         encrypt[len - i - 1] = rev;
  173.     }
  174.     for (int i = 0; i < len; ++i)
  175.         cout << encrypt[i];
  176.     cout << " - " << "Расшифровано шифром обратного порядка" << endl;
  177. }
  178. void DEweiner(char*& crypt, char*& alphabet, const int& len, const string& key, const int& lenalp) {
  179.     int lenkey = key.length();
  180.     int* change = new int[len];
  181.     char help;
  182.     int help2;
  183.     for (int i = 0, k = 0; i < len; ++i) {
  184.         if (k + 1 != lenkey) {
  185.             help = key[k];
  186.             k++;
  187.         }
  188.         else {
  189.             help = key[k];
  190.             k = 0;
  191.         }
  192.         for (int j = 0; j < lenalp; ++j) {
  193.             if (help == alphabet[j]) {
  194.                 help2 = j + 1;
  195.                 change[i] = help2;
  196.             }
  197.         }
  198.     }
  199.     for (int i = 0; i < len; ++i) {
  200.         help2 = change[i];
  201.         for (int j = 0; j < lenalp; ++j) {
  202.             if (crypt[i] == alphabet[j]) {
  203.                 if (j + (lenalp - help2) >= lenalp)
  204.                     help = alphabet[(lenalp - help2) + j - lenalp];
  205.                 else
  206.                     help = alphabet[j + (lenalp - help2)];
  207.                 crypt[i] = help;
  208.                 cout << help;
  209.                 break;
  210.             }
  211.         }
  212.     }
  213.     cout << " - " << "Расшифровано шифром вижинера" << endl;
  214. }
  215. // дешифр атбаш
  216. void DEatbash(char*& crypt, char*& alphabet, const int& len, const int& lenalp) {
  217.     char help;
  218.     for (int i = 0; i < len; ++i) {
  219.         for (int j = 0; j < lenalp; ++j) {
  220.             if (crypt[i] == alphabet[j]) {
  221.                 help = alphabet[lenalp - j - 1];
  222.                 crypt[i] = help;
  223.                 cout << help;
  224.                 break;
  225.             }
  226.         }
  227.     }
  228.     cout << " - " << "Расшифровано шифром атбаша" << endl;
  229. }
  230. // дешифр цезаря
  231. void DEcaesar(const int& DEchange_Caesar, char*& crypt, char*& alphabet, int& len, const int& lenalp, char*& buffer, int& raw_length) {
  232.     char help;
  233.     for (int i = 0; i < len; ++i) {
  234.         for (int j = 0; j < lenalp; ++j) {
  235.             if (crypt[i] == alphabet[j]) {
  236.                 if (j + (lenalp - DEchange_Caesar) >= lenalp)
  237.                     help = alphabet[(lenalp - DEchange_Caesar) + j - lenalp];
  238.                 else
  239.                     help = alphabet[j + (lenalp - DEchange_Caesar)];
  240.                 cout << help;
  241.                 crypt[i] = help;
  242.                 break;
  243.             }
  244.         }
  245.     }
  246.     cout << " - " << "Расшифровано шифром цезаря" << endl << endl;
  247.     int sm_count = 0, b_count = 0;
  248.     len = ((raw_length - 5) - len * 3) + len;
  249.     char* fbuffer = new char[len];
  250.     for (int i = 0; i < raw_length - 5; ) {
  251.         if ((int)buffer[i] == 32 || (int)buffer[i] == 10 || (int)buffer[i] == 13) {
  252.             fbuffer[sm_count] = buffer[i];
  253.             sm_count++;
  254.             i++;
  255.         }
  256.         else {
  257.             fbuffer[sm_count] = crypt[b_count];
  258.             b_count++;
  259.             sm_count++;
  260.             i += 3;
  261.         }
  262.     }
  263.     crypt = fbuffer;
  264. }
  265. 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) {
  266.     SetConsoleCP(1251);
  267.     SetConsoleOutputCP(1251);
  268.     setlocale(LC_ALL, "Russian");
  269.     srand(time(NULL));
  270.     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' };
  271.     char* alphabetRUS = new char[33]{ 'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я' };
  272.  
  273.     if (int(first_letter) >= 97 && int(first_letter) <= 122) {
  274.         lang = alphabetENG;
  275.         crypt = true;
  276.         lenalp = 26;
  277.     }
  278.     else if (((int)(first_letter) >= -32 && (int)(first_letter) <= -1) || (int)first_letter == -72) {
  279.         lang = alphabetRUS;
  280.         crypt = true;
  281.         lenalp = 33;
  282.     }
  283.     else if (first_letter - '0' >= 0 && first_letter - '0' < 10) {
  284.         crypt = false;
  285.         if (buffer[(raw_length - 1) - 3] == '2') {
  286.             lang = alphabetENG;
  287.             lenalp = 26;
  288.         }
  289.         else {
  290.             lang = alphabetRUS;
  291.             lenalp = 33;
  292.         }
  293.  
  294.     }
  295.     else {
  296.         cout << "error, wrong text language or crypted msg";
  297.         system("pause");
  298.     }
  299.     if (crypt == true)
  300.         change_Caesar = rand() % 25 + 1;
  301.     else {
  302.         if (buffer[(raw_length - 1) - 1] == '0')
  303.             change_Caesar = buffer[(raw_length - 1)] - '0';
  304.         else
  305.             change_Caesar = ((buffer[(raw_length - 1) - 1] - '0') * 10) + (buffer[(raw_length - 1)] - '0');
  306.     }
  307.     int k = 0;
  308.     char* sm_buffer;
  309.     if (crypt == true) {
  310.         sm_buffer = new char[msg_length];
  311.         for (int i = 0; i < raw_length; ) {
  312.             if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13) {
  313.                 sm_buffer[k] = buffer[i];
  314.                 ++k;
  315.                 ++i;
  316.             }
  317.             else
  318.                 i++;
  319.         }
  320.     }
  321.     else {
  322.         msg_length = (msg_length - 5) / 3;
  323.         sm_buffer = new char[msg_length];
  324.         for (int i = 0; i < (raw_length - 5); ) {
  325.             if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13) {
  326.                 if (buffer[i] == '0') {
  327.                     sm_buffer[k] = lang[(buffer[i + 1] - '0') - 1];
  328.                     k++;
  329.                     i += 3;
  330.                 }
  331.                 else {
  332.                     sm_buffer[k] = lang[(((buffer[i] - '0') * 10) + (buffer[i + 1] - '0')) - 1];
  333.                     k++;
  334.                     i += 3;
  335.                 }
  336.             }
  337.             else
  338.                 i++;
  339.  
  340.         }
  341.     }
  342.     n_buffer = sm_buffer;
  343.     return 1;
  344. }
  345. int main(int argc, char* argv[]) {
  346.     int raw_length, change_Caesar, lenalp, msg_length = 0;
  347.     char* lang;
  348.     char* sm_buffer;
  349.     bool crypt = true;
  350.     char* buffer = input(argv[1], raw_length, msg_length);
  351.     if (setting_up(buffer[0], lang, change_Caesar, crypt, lenalp, buffer, sm_buffer, raw_length, msg_length) == 0)
  352.         return 0;
  353.         string key;
  354.     char* fbuffer;
  355.     int fbuffer_size;
  356.     cout << "Введите ключ: ";
  357.     getline(cin, key);
  358.     if (crypt == true) {
  359.         fbuffer_size = 3 * msg_length + (raw_length - msg_length) + 5;
  360.         fbuffer = new char[fbuffer_size];
  361.         caesar(change_Caesar, sm_buffer, lang, msg_length, lenalp);
  362.         atbash(sm_buffer, lang, msg_length, lenalp);
  363.         weiner(sm_buffer, lang, msg_length, key, lenalp);
  364.         reverse(sm_buffer, msg_length);
  365.         a1z26(buffer, lang, raw_length, lenalp, fbuffer, fbuffer_size, sm_buffer, change_Caesar);
  366.         output(fbuffer, fbuffer_size);
  367.         delete[] fbuffer;
  368.     }
  369.     else {
  370.         DEreverse(sm_buffer, msg_length);
  371.         DEweiner(sm_buffer, lang, msg_length, key, lenalp);
  372.         DEatbash(sm_buffer, lang, msg_length, lenalp);
  373.         DEcaesar(change_Caesar, sm_buffer, lang, msg_length, lenalp, buffer, raw_length);
  374.         output(sm_buffer, msg_length);
  375.     }
  376.     delete[] buffer;
  377.     delete[] sm_buffer;
  378.     system("pause");
  379.     return 0;
  380. }
Add Comment
Please, Sign In to add comment