Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include<iostream>
- #include <locale.h>
- #include<string>
- using namespace std;
- // *~~~~~~~~ variable declaration ~~~~~~~~~~* //
- char grid_ru[4][8];
- char grid_en[5][5]; // 5x5 matrix to encipher or decipher
- char keyword[26]; // cypher key
- char msg[100]; // message
- int mark[130], len, r, c; // necessary variables
- // *~~~~~~~~~ Function declaration ~~~~~~~~~~~* //
- void createGrid_en();
- void showGrid();
- void encipher_en();
- void decipher_en();
- void encipher_ru();
- void decipher_ru();
- void createGrid_ru();
- void initialize();
- void menu()
- {
- int n;
- setlocale(LC_ALL, "Russian");
- system("CLS");
- string lang;
- cout << "Select language/Âûáåðèòå ÿçûê(en/ru): ";
- cin >> lang;
- if (lang == "en") {
- setlocale(LC_ALL, "English");
- SetConsoleCP(866);
- SetConsoleOutputCP(866);
- system("CLS");
- string op[] = { "1. Encipher","2. Decipher","3. Exit" };
- cout << op[0] << endl << op[1] << endl << op[2] << endl << "Enter choice: ";
- cin >> n;
- if (n == 1) encipher_en();
- else if (n == 2) decipher_en();
- else exit(1);
- }
- else if (lang == "ru") {
- system("chcp 1251");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- system("CLS");
- string op[] = { "1. Çàøòôðîâàòü","2. Ðàñøèôðîâàòü","3. Âûõîä" };
- cout << op[0] << endl << op[1] << endl << op[2] << endl << "×òî âû õîòèòå ñäåëàòü: ";
- cin >> n;
- if (n == 1) encipher_ru();
- else if (n == 2) decipher_ru();
- else exit(1);
- }
- else cout << "Íåâîçìîæíî èäåíôèöèðîâàòü ÿçûê.";
- }
- void initialize()
- {
- memset(mark, 0, sizeof(mark)); //çàïîëíèòü âñ¸ mark 0
- system("CLS"); //î÷èñòêà
- }
- int main()
- {
- menu();
- return 0;
- }
- void decipher_en()
- {
- initialize();
- createGrid_en();
- cout << "Cypher text: ";
- char cypText[150]; // cypher text
- cin >> cypText;
- getchar(); // flush buffer
- int l = strlen(cypText); //take length
- /*
- for(int i=0;i<l;i+=2)
- cout<<cypText[i]<<cypText[i+1]<<" ";
- cout<<endl;
- */
- cout << "Decipher text: ";
- //decipher starts
- int P, Q, R, S, f1, f2, i, j, k;
- char x, y;
- for (i = 0; i < l; i += 2)
- {
- x = cypText[i];
- y = cypText[i + 1];
- f1 = f2 = 0;
- for (j = 0; j < 5; j++)
- {
- for (k = 0; k < 5; k++)
- {
- if (x == grid_en[j][k])
- {
- P = j;
- Q = k;
- f1 = 1;
- }
- if (y == grid_en[j][k])
- {
- R = j;
- S = k;
- f2 = 1;
- }
- if (f1 && f2) break;
- }
- if (f1 && f2) break;
- }
- if (P == R) //same row
- {
- if (Q == 0) cout << grid_en[P][4];
- else cout << grid_en[P][Q - 1];
- if (S == 0) cout << grid_en[R][4];
- else cout << grid_en[R][S - 1];
- }
- else if (Q == S) // same column
- {
- if (P == 0) cout << grid_en[4][Q];
- else cout << grid_en[P - 1][Q];
- if (R == 0) cout << grid_en[4][S];
- else cout << grid_en[R - 1][S];
- }
- else //opposite corner
- {
- cout << grid_en[P][S] << grid_en[R][Q];
- }
- }
- cout << endl << endl;
- system("PAUSE");
- menu();
- }
- void encipher_en()
- {
- initialize();
- createGrid_en();
- cout << "Message to cypher: ";
- gets(msg);
- int i,k;
- int l = strlen(msg); // msg length
- char reqText[150]; //generate required text to encipher
- int in = 0, j = 0;
- for (i = 0; i < l; i++)
- {
- j = i + 1;
- if (msg[i] == ' ') //ignore all space from string
- {
- i++;
- j++;
- }
- if (msg[j] == ' ') j++; //ignore space
- if (toupper(msg[i]) == 'J') msg[i] = 'I'; // ignore J
- if (toupper(msg[i]) == toupper(msg[j])) // if duplicate add 'X' after the first letter
- {
- reqText[in] = toupper(msg[i]);
- reqText[in + 1] = 'X';
- in++;
- }
- else
- {
- reqText[in] = toupper(msg[i]);
- }
- in++;
- }
- if (in % 2 != 0) reqText[in] = 'X'; // if one character left, add 'X' after it
- /*
- //show generated text in pair
- for(int i=0;i<in;i+=2)
- cout<<reqText[i]<<reqText[i+1]<<" ";
- cout<<endl;
- */
- cout << "Cypher text: ";
- //encipher starts
- int P, Q, R, S, f1, f2;
- char x, y;
- for (i = 0; i < in; i += 2)
- {
- x = reqText[i];
- y = reqText[i + 1];
- f1 = f2 = 0;
- for (j = 0; j < 5; j++)
- {
- for (k = 0; k < 5; k++)
- {
- if (x == grid_en[j][k])
- {
- P = j;
- Q = k;
- f1 = 1;
- }
- if (y == grid_en[j][k])
- {
- R = j;
- S = k;
- f2 = 1;
- }
- if (f1 && f2) break;
- }
- if (f1 && f2) break;
- }
- if (P == R) //same row
- {
- if (Q == 4) cout << grid_en[P][0];
- else cout << grid_en[P][Q + 1];
- if (S == 4) cout << grid_en[R][0];
- else cout << grid_en[R][S + 1];
- }
- else if (Q == S) // same column
- {
- if (P == 4) cout << grid_en[0][Q];
- else cout << grid_en[P + 1][Q];
- if (R == 4) cout << grid_en[0][S];
- else cout << grid_en[R + 1][S];
- }
- else //opposite corner
- {
- cout << grid_en[P][S] << grid_en[R][Q];
- }
- }
- cout << endl << endl;
- system("PAUSE");
- menu();
- }
- void createGrid_en()
- {
- cout << "Keyword: ";
- cin >> keyword; // key of the cypher
- getchar();
- len = strlen(keyword); // size of input string O(n) :3
- mark['J'] = 1; // ignore J
- r = 0, c = 0; //initialize row and column
- int i;
- // first populate the keyword
- for (i = 0; i < len; i++)
- {
- if (!mark[toupper(keyword[i])]) // ignore duplicates
- {
- mark[toupper(keyword[i])] = 1;
- grid_en[r][c++] = toupper(keyword[i]);
- if (c % 5 == 0) //increase row column
- {
- c = 0;
- r++;
- }
- }
- }
- // complete rest of the matrix from unused characters starting from A
- for (i = 'A'; i <= 'Z'; i++)
- {
- if (mark[i] == 0) // taking values that are not in the matrix already
- {
- grid_en[r][c++] = i;
- mark[i] = 1;
- if (c % 5 == 0)
- {
- if (r == 4 && c == 5) break; //matrix populated
- // else increase row column
- r++;
- c = 0;
- }
- }
- }
- }
- void showGrid()
- {
- cout << "5x5 Matrix" << endl;
- //show grid
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- cout << grid_en[i][j] << " ";
- }
- cout << endl;
- }
- }
- void decipher_ru()
- {
- initialize();
- createGrid_ru();
- cout << "Cypher text: ";
- char cypText[150]; // cypher text
- cin >> cypText;
- getchar(); // flush buffer
- int l = strlen(cypText); //take length
- /*
- for(int i=0;i<l;i+=2)
- cout<<cypText[i]<<cypText[i+1]<<" ";
- cout<<endl;
- */
- cout << "Decipher text: ";
- //decipher starts
- int P, Q, R, S, f1, f2,j,i,k;
- char x, y;
- for (i = 0; i < l; i += 2)
- {
- x = cypText[i];
- y = cypText[i + 1];
- f1 = f2 = 0;
- for (j = 0; j < 4; j++)
- {
- for (k = 0; k < 8; k++)
- {
- if (x == grid_ru[j][k])
- {
- P = j;
- Q = k;
- f1 = 1;
- }
- if (y == grid_ru[j][k])
- {
- R = j;
- S = k;
- f2 = 1;
- }
- if (f1 && f2) break;
- }
- if (f1 && f2) break;
- }
- if (P == R) //same row
- {
- if (Q == 0) cout << grid_ru[P][7];
- else cout << grid_ru[P][Q - 1];
- if (S == 0) cout << grid_ru[R][7];
- else cout << grid_ru[R][S - 1];
- }
- else if (Q == S) // same column
- {
- if (P == 0) cout << grid_ru[3][Q];
- else cout << grid_ru[P - 1][Q];
- if (R == 0) cout << grid_ru[3][S];
- else cout << grid_ru[R - 1][S];
- }
- else //opposite corner
- {
- cout << grid_ru[P][S] << grid_ru[R][Q];
- }
- }
- cout << endl << endl;
- system("PAUSE");
- menu();
- }
- void encipher_ru()
- {
- initialize();
- createGrid_ru();
- cout << "Ñîîáùåíèå äëÿ øèôðîâêè: ";
- gets(msg);
- int l = strlen(msg); // msg length
- char reqText[150]; //generate required text to encipher
- int in = 0, j = 0,i,k;
- for (i = 0; i < l; i++)
- {
- j = i + 1;
- if (msg[i] == ' ') //ignore all space from string
- {
- i++;
- j++;
- }
- if (msg[j] == ' ') j++; //ignore space
- if (toupper(msg[i]) == '¨') msg[i] = 'Å'; // ignore J
- if (toupper(msg[i]) == toupper(msg[j])) // if duplicate add 'X' after the first letter
- {
- reqText[in] = toupper(msg[i]);
- reqText[in + 1] = 'Õ';
- in++;
- }
- else
- {
- reqText[in] = toupper(msg[i]);
- }
- in++;
- }
- if (in % 2 != 0) reqText[in] = 'Õ'; // if one character left, add 'X' after it
- /*
- //show generated text in pair
- for(int i=0;i<in;i+=2)
- cout<<reqText[i]<<reqText[i+1]<<" ";
- cout<<endl;
- */
- cout << "Cypher text: ";
- //encipher starts
- int P, Q, R, S, f1, f2;
- char x, y;
- for (i = 0; i < in; i += 2)
- {
- x = reqText[i];
- y = reqText[i + 1];
- f1 = f2 = 0;
- for (j = 0; j < 4; j++)
- {
- for (k = 0; k < 8; k++)
- {
- if (x == grid_ru[j][k])
- {
- P = j;
- Q = k;
- f1 = 1;
- }
- if (y == grid_ru[j][k])
- {
- R = j;
- S = k;
- f2 = 1;
- }
- if (f1 && f2) break;
- }
- if (f1 && f2) break;
- }
- if (P == R) //same row
- {
- if (Q == 7) cout << grid_ru[P][0];
- else cout << grid_ru[P][Q + 1];
- if (S == 7) cout << grid_ru[R][0];
- else cout << grid_ru[R][S + 1];
- }
- else if (Q == S) // same column
- {
- if (P == 3) cout << grid_ru[0][Q];
- else cout << grid_ru[P + 1][Q];
- if (R == 3) cout << grid_ru[0][S];
- else cout << grid_ru[R + 1][S];
- }
- else //opposite corner
- {
- cout << grid_ru[P][S] << grid_ru[R][Q];
- }
- }
- cout << endl << endl;
- system("PAUSE");
- menu();
- }
- void createGrid_ru()
- {
- cout << "Êëþ÷: ";
- cin >> keyword; // key of the cypher
- getchar();
- len = strlen(keyword); // size of input string O(n) :3
- mark['¨'] = 1; // ignore J
- r = 0, c = 0;
- int i; //initialize row and column
- // first populate the keyword
- for (i = 0; i < len; i++)
- {
- if (!mark[toupper(keyword[i])]) // ignore duplicates
- {
- mark[toupper(keyword[i])] = 1;
- grid_ru[r][c++] = toupper(keyword[i]);
- if (c % 8 == 0) //increase row column
- {
- c = 0;
- r++;
- }
- }
- }
- // complete rest of the matrix from unused characters starting from A
- for (i = 'À'; i <= 'ß'; i++)
- {
- if (mark[i] == 0) // taking values that are not in the matrix already
- {
- grid_ru[r][c++] = i;
- mark[i] = 1;
- if (c % 8 == 0)
- {
- if (r == 3 && c == 8) break; //matrix populated
- // else increase row column
- r++;
- c = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement