Advertisement
didiwot

plafair

Oct 14th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.99 KB | None | 0 0
  1. #include <windows.h>
  2. #include<iostream>
  3. #include <locale.h>
  4. #include<string>
  5. using namespace std;
  6.  
  7. // *~~~~~~~~ variable declaration ~~~~~~~~~~* //
  8. char grid_ru[4][8];
  9. char grid_en[5][5]; // 5x5  matrix to encipher or decipher
  10. char keyword[26]; // cypher key
  11. char msg[100]; // message
  12. int mark[130], len, r, c; // necessary variables
  13.  
  14. // *~~~~~~~~~ Function declaration ~~~~~~~~~~~* //
  15. void createGrid_en();
  16. void showGrid();
  17. void encipher_en();
  18. void decipher_en();
  19. void encipher_ru();
  20. void decipher_ru();
  21. void createGrid_ru();
  22. void initialize();
  23. void menu()
  24. {
  25.     int n;
  26.     setlocale(LC_ALL, "Russian");
  27.     system("CLS");
  28.     string lang;
  29.     cout << "Select language/Âûáåðèòå ÿçûê(en/ru): ";
  30.     cin >> lang;
  31.     if (lang == "en") {
  32.         setlocale(LC_ALL, "English");
  33.         SetConsoleCP(866);
  34.         SetConsoleOutputCP(866);
  35.         system("CLS");
  36.         string op[] = { "1. Encipher","2. Decipher","3. Exit" };
  37.         cout << op[0] << endl << op[1] << endl << op[2] << endl << "Enter choice: ";
  38.         cin >> n;
  39.         if (n == 1) encipher_en();
  40.         else if (n == 2) decipher_en();
  41.         else exit(1);
  42.     }
  43.     else if (lang == "ru") {
  44.     system("chcp 1251");   
  45.     SetConsoleCP(1251);
  46.     SetConsoleOutputCP(1251);
  47.         system("CLS");
  48.         string op[] = { "1. Çàøòôðîâàòü","2. Ðàñøèôðîâàòü","3. Âûõîä" };
  49.         cout << op[0] << endl << op[1] << endl << op[2] << endl << "×òî âû õîòèòå ñäåëàòü: ";
  50.         cin >> n;
  51.         if (n == 1) encipher_ru();
  52.         else if (n == 2) decipher_ru();
  53.         else exit(1);
  54.     }
  55.     else cout << "Íåâîçìîæíî èäåíôèöèðîâàòü ÿçûê.";
  56. }
  57. void initialize()
  58. {
  59.     memset(mark, 0, sizeof(mark)); //çàïîëíèòü âñ¸ mark 0
  60.     system("CLS"); //î÷èñòêà
  61. }
  62. int main()
  63. {
  64.     menu();
  65.     return 0;  
  66. }
  67. void decipher_en()
  68. {
  69.     initialize();
  70.     createGrid_en();
  71.     cout << "Cypher text: ";
  72.     char cypText[150]; // cypher text
  73.     cin >> cypText;
  74.     getchar(); // flush buffer
  75.     int l = strlen(cypText); //take length
  76.  
  77.     /*
  78.     for(int i=0;i<l;i+=2)
  79.         cout<<cypText[i]<<cypText[i+1]<<" ";
  80.     cout<<endl;
  81.     */
  82.  
  83.     cout << "Decipher text: ";
  84.     //decipher starts
  85.     int P, Q, R, S, f1, f2, i, j, k;
  86.     char x, y;
  87.     for (i = 0; i < l; i += 2)
  88.     {
  89.         x = cypText[i];
  90.         y = cypText[i + 1];
  91.         f1 = f2 = 0;
  92.         for (j = 0; j < 5; j++)
  93.         {
  94.             for (k = 0; k < 5; k++)
  95.             {
  96.                 if (x == grid_en[j][k])
  97.                 {
  98.                     P = j;
  99.                     Q = k;
  100.                     f1 = 1;
  101.                 }
  102.                 if (y == grid_en[j][k])
  103.                 {
  104.                     R = j;
  105.                     S = k;
  106.                     f2 = 1;
  107.                 }
  108.                 if (f1 && f2) break;
  109.             }
  110.             if (f1 && f2) break;
  111.         }
  112.         if (P == R) //same row
  113.         {
  114.             if (Q == 0) cout << grid_en[P][4];
  115.             else cout << grid_en[P][Q - 1];
  116.             if (S == 0) cout << grid_en[R][4];
  117.             else cout << grid_en[R][S - 1];
  118.         }
  119.         else if (Q == S) // same column
  120.         {
  121.             if (P == 0) cout << grid_en[4][Q];
  122.             else cout << grid_en[P - 1][Q];
  123.             if (R == 0) cout << grid_en[4][S];
  124.             else cout << grid_en[R - 1][S];
  125.         }
  126.         else //opposite corner
  127.         {
  128.             cout << grid_en[P][S] << grid_en[R][Q];
  129.         }
  130.     }
  131.     cout << endl << endl;
  132.     system("PAUSE");
  133.     menu();
  134. }
  135. void encipher_en()
  136. {
  137.     initialize();
  138.     createGrid_en();
  139.     cout << "Message to cypher: ";
  140.     gets(msg);
  141.     int i,k;
  142.     int l = strlen(msg); // msg length
  143.     char reqText[150]; //generate required text to encipher
  144.     int in = 0, j = 0;
  145.     for (i = 0; i < l; i++)
  146.     {
  147.         j = i + 1;
  148.         if (msg[i] == ' ') //ignore all space from string
  149.         {
  150.             i++;
  151.             j++;
  152.         }
  153.         if (msg[j] == ' ') j++; //ignore space
  154.         if (toupper(msg[i]) == 'J') msg[i] = 'I'; // ignore J
  155.         if (toupper(msg[i]) == toupper(msg[j])) // if duplicate add 'X' after the first letter
  156.         {
  157.             reqText[in] = toupper(msg[i]);
  158.             reqText[in + 1] = 'X';
  159.             in++;
  160.         }
  161.         else
  162.         {
  163.             reqText[in] = toupper(msg[i]);
  164.         }
  165.         in++;
  166.     }
  167.     if (in % 2 != 0) reqText[in] = 'X'; // if one character left, add 'X' after it
  168.  
  169.     /*
  170.     //show generated text in pair
  171.     for(int i=0;i<in;i+=2)
  172.         cout<<reqText[i]<<reqText[i+1]<<" ";
  173.     cout<<endl;
  174.     */
  175.  
  176.     cout << "Cypher text: ";
  177.     //encipher starts
  178.     int P, Q, R, S, f1, f2;
  179.     char x, y;
  180.     for (i = 0; i < in; i += 2)
  181.     {
  182.         x = reqText[i];
  183.         y = reqText[i + 1];
  184.         f1 = f2 = 0;
  185.         for (j = 0; j < 5; j++)
  186.         {
  187.             for (k = 0; k < 5; k++)
  188.             {
  189.                 if (x == grid_en[j][k])
  190.                 {
  191.                     P = j;
  192.                     Q = k;
  193.                     f1 = 1;
  194.                 }
  195.                 if (y == grid_en[j][k])
  196.                 {
  197.                     R = j;
  198.                     S = k;
  199.                     f2 = 1;
  200.                 }
  201.                 if (f1 && f2) break;
  202.             }
  203.             if (f1 && f2) break;
  204.         }
  205.         if (P == R) //same row
  206.         {
  207.             if (Q == 4) cout << grid_en[P][0];
  208.             else cout << grid_en[P][Q + 1];
  209.             if (S == 4) cout << grid_en[R][0];
  210.             else cout << grid_en[R][S + 1];
  211.         }
  212.         else if (Q == S) // same column
  213.         {
  214.             if (P == 4) cout << grid_en[0][Q];
  215.             else cout << grid_en[P + 1][Q];
  216.             if (R == 4) cout << grid_en[0][S];
  217.             else cout << grid_en[R + 1][S];
  218.         }
  219.         else //opposite corner
  220.         {
  221.             cout << grid_en[P][S] << grid_en[R][Q];
  222.         }
  223.     }
  224.     cout << endl << endl;
  225.     system("PAUSE");
  226.     menu();
  227. }
  228. void createGrid_en()
  229. {
  230.     cout << "Keyword: ";
  231.     cin >> keyword; // key of the cypher
  232.     getchar();
  233.     len = strlen(keyword);  // size of input string O(n) :3
  234.     mark['J'] = 1; // ignore J
  235.     r = 0, c = 0; //initialize row and column
  236.     int i;
  237.  
  238.     // first populate the keyword
  239.     for (i = 0; i < len; i++)
  240.     {
  241.         if (!mark[toupper(keyword[i])]) // ignore duplicates
  242.         {
  243.             mark[toupper(keyword[i])] = 1;
  244.             grid_en[r][c++] = toupper(keyword[i]);
  245.             if (c % 5 == 0) //increase row column
  246.             {
  247.                 c = 0;
  248.                 r++;
  249.             }
  250.         }
  251.     }
  252.  
  253.     // complete rest of the matrix from unused characters starting from A
  254.     for (i = 'A'; i <= 'Z'; i++)
  255.     {
  256.         if (mark[i] == 0) // taking values that are not in the matrix already
  257.         {
  258.             grid_en[r][c++] = i;
  259.             mark[i] = 1;
  260.             if (c % 5 == 0)
  261.             {
  262.                 if (r == 4 && c == 5) break; //matrix populated
  263.  
  264.                 // else increase row column
  265.                 r++;
  266.                 c = 0;
  267.             }
  268.         }
  269.     }
  270. }
  271. void showGrid()
  272. {
  273.     cout << "5x5 Matrix" << endl;
  274.     //show grid
  275.     for (int i = 0; i < 5; i++)
  276.     {
  277.         for (int j = 0; j < 5; j++)
  278.         {
  279.             cout << grid_en[i][j] << " ";
  280.         }
  281.         cout << endl;
  282.     }
  283. }
  284.  
  285.  
  286. void decipher_ru()
  287. {
  288.     initialize();
  289.     createGrid_ru();
  290.     cout << "Cypher text: ";
  291.     char cypText[150]; // cypher text
  292.     cin >> cypText;
  293.     getchar(); // flush buffer
  294.     int l = strlen(cypText); //take length
  295.  
  296.     /*
  297.     for(int i=0;i<l;i+=2)
  298.         cout<<cypText[i]<<cypText[i+1]<<" ";
  299.     cout<<endl;
  300.     */
  301.  
  302.     cout << "Decipher text: ";
  303.     //decipher starts
  304.     int P, Q, R, S, f1, f2,j,i,k;
  305.     char x, y;
  306.     for (i = 0; i < l; i += 2)
  307.     {
  308.         x = cypText[i];
  309.         y = cypText[i + 1];
  310.         f1 = f2 = 0;
  311.         for (j = 0; j < 4; j++)
  312.         {
  313.             for (k = 0; k < 8; k++)
  314.             {
  315.                 if (x == grid_ru[j][k])
  316.                 {
  317.                     P = j;
  318.                     Q = k;
  319.                     f1 = 1;
  320.                 }
  321.                 if (y == grid_ru[j][k])
  322.                 {
  323.                     R = j;
  324.                     S = k;
  325.                     f2 = 1;
  326.                 }
  327.                 if (f1 && f2) break;
  328.             }
  329.             if (f1 && f2) break;
  330.         }
  331.         if (P == R) //same row
  332.         {
  333.             if (Q == 0) cout << grid_ru[P][7];
  334.             else cout << grid_ru[P][Q - 1];
  335.             if (S == 0) cout << grid_ru[R][7];
  336.             else cout << grid_ru[R][S - 1];
  337.         }
  338.         else if (Q == S) // same column
  339.         {
  340.             if (P == 0) cout << grid_ru[3][Q];
  341.             else cout << grid_ru[P - 1][Q];
  342.             if (R == 0) cout << grid_ru[3][S];
  343.             else cout << grid_ru[R - 1][S];
  344.         }
  345.         else //opposite corner
  346.         {
  347.             cout << grid_ru[P][S] << grid_ru[R][Q];
  348.         }
  349.     }
  350.     cout << endl << endl;
  351.     system("PAUSE");
  352.     menu();
  353. }
  354. void encipher_ru()
  355. {
  356.     initialize();
  357.     createGrid_ru();
  358.     cout << "Ñîîáùåíèå äëÿ øèôðîâêè: ";
  359.     gets(msg);
  360.     int l = strlen(msg); // msg length
  361.     char reqText[150]; //generate required text to encipher
  362.     int in = 0, j = 0,i,k;
  363.     for (i = 0; i < l; i++)
  364.     {
  365.         j = i + 1;
  366.         if (msg[i] == ' ') //ignore all space from string
  367.         {
  368.             i++;
  369.             j++;
  370.         }
  371.         if (msg[j] == ' ') j++; //ignore space
  372.         if (toupper(msg[i]) == '¨') msg[i] = 'Å'; // ignore J
  373.         if (toupper(msg[i]) == toupper(msg[j])) // if duplicate add 'X' after the first letter
  374.         {
  375.             reqText[in] = toupper(msg[i]);
  376.             reqText[in + 1] = 'Õ';
  377.             in++;
  378.         }
  379.         else
  380.         {
  381.             reqText[in] = toupper(msg[i]);
  382.         }
  383.         in++;
  384.     }
  385.     if (in % 2 != 0) reqText[in] = 'Õ'; // if one character left, add 'X' after it
  386.  
  387.     /*
  388.     //show generated text in pair
  389.     for(int i=0;i<in;i+=2)
  390.         cout<<reqText[i]<<reqText[i+1]<<" ";
  391.     cout<<endl;
  392.     */
  393.  
  394.     cout << "Cypher text: ";
  395.     //encipher starts
  396.     int P, Q, R, S, f1, f2;
  397.     char x, y;
  398.     for (i = 0; i < in; i += 2)
  399.     {
  400.         x = reqText[i];
  401.         y = reqText[i + 1];
  402.         f1 = f2 = 0;
  403.         for (j = 0; j < 4; j++)
  404.         {
  405.             for (k = 0; k < 8; k++)
  406.             {
  407.                 if (x == grid_ru[j][k])
  408.                 {
  409.                     P = j;
  410.                     Q = k;
  411.                     f1 = 1;
  412.                 }
  413.                 if (y == grid_ru[j][k])
  414.                 {
  415.                     R = j;
  416.                     S = k;
  417.                     f2 = 1;
  418.                 }
  419.                 if (f1 && f2) break;
  420.             }
  421.             if (f1 && f2) break;
  422.         }
  423.         if (P == R) //same row
  424.         {
  425.             if (Q == 7) cout << grid_ru[P][0];
  426.             else cout << grid_ru[P][Q + 1];
  427.             if (S == 7) cout << grid_ru[R][0];
  428.             else cout << grid_ru[R][S + 1];
  429.         }
  430.         else if (Q == S) // same column
  431.         {
  432.             if (P == 3) cout << grid_ru[0][Q];
  433.             else cout << grid_ru[P + 1][Q];
  434.             if (R == 3) cout << grid_ru[0][S];
  435.             else cout << grid_ru[R + 1][S];
  436.         }
  437.         else //opposite corner
  438.         {
  439.             cout << grid_ru[P][S] << grid_ru[R][Q];
  440.         }
  441.     }
  442.     cout << endl << endl;
  443.     system("PAUSE");
  444.     menu();
  445. }
  446. void createGrid_ru()
  447. {
  448.     cout << "Êëþ÷: ";
  449.     cin >> keyword; // key of the cypher
  450.     getchar();
  451.     len = strlen(keyword);  // size of input string O(n) :3
  452.     mark['¨'] = 1; // ignore J
  453.     r = 0, c = 0;
  454.     int i; //initialize row and column
  455.  
  456.     // first populate the keyword
  457.     for (i = 0; i < len; i++)
  458.     {
  459.         if (!mark[toupper(keyword[i])]) // ignore duplicates
  460.         {
  461.             mark[toupper(keyword[i])] = 1;
  462.             grid_ru[r][c++] = toupper(keyword[i]);
  463.             if (c % 8 == 0) //increase row column
  464.             {
  465.                 c = 0;
  466.                 r++;
  467.             }
  468.         }
  469.     }
  470.  
  471.     // complete rest of the matrix from unused characters starting from A
  472.     for (i = 'À'; i <= 'ß'; i++)
  473.     {
  474.         if (mark[i] == 0) // taking values that are not in the matrix already
  475.         {
  476.             grid_ru[r][c++] = i;
  477.             mark[i] = 1;
  478.             if (c % 8 == 0)
  479.             {
  480.                 if (r == 3 && c == 8) break; //matrix populated
  481.  
  482.                 // else increase row column
  483.                 r++;
  484.                 c = 0;
  485.             }
  486.         }
  487.     }
  488. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement