-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composition_Encrypt_Decrypt.c++
126 lines (98 loc) · 3.07 KB
/
Composition_Encrypt_Decrypt.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<bits/stdc++.h>
#define Sakr_ ios_base :: sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0);
#define ll long long
using namespace std;
/**
* by:Ahmed Sakr (sakr_) with AZA ;
* freopen("input.txt","r",stdin);
* freopen("output.txt","w",stdout);
* __builtin_popcountll();
**/
char matrix[5][5];
map<char,pair<int,int>>matrixToQuiclyAcss;
void getMatrixReady() {
char c='a';
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(c=='q') {
matrix[i][j]=c;
matrixToQuiclyAcss[c] = {i, j};
matrixToQuiclyAcss['z'] = {i, j};
}else {
matrix[i][j]=c;
matrixToQuiclyAcss[c] = {i, j};
}
c++;
}
}
}
string CompositionEncrypt(string &text) {
int textlen = text.length();
vector<int> arr;
for(int i=0;i<textlen;i++) {
if(isalpha(text[i])) {
if(isupper(text[i]))
text[i]+=32;
arr.push_back(matrixToQuiclyAcss[text[i]].first);
arr.push_back(matrixToQuiclyAcss[text[i]].second);
}
}
string result="";
int arrlen = arr.size()/2;
for(int i=0;i<arrlen;i++) {
result+= matrix[arr[i]][arr[i+arrlen]];
}
return result;
}
string CompositionDecrypt(string &text) {
int textlen = text.length();
vector<int> arr(textlen*2+5);
for(int i=0;i<textlen;i++) {
if (isupper(text[i]))
text[i] += 32;
arr[i] = matrixToQuiclyAcss[text[i]].first;
arr[i + textlen] = matrixToQuiclyAcss[text[i]].second;
}
string result="";
int arrlen = textlen*2;
for(int i=0;i<arrlen;i+=2) {
result+= matrix[arr[i]][arr[i+1]];
}
return result;
}
void solveit() {
getMatrixReady();
string plainText, cypertext, c, x;
string key;
////////////////simple interface///////////////////////
while (true) {
cout << "<1> (cypertext to plaintext).\n<2> (plaintext to cypertext).\nEnter( 1 or 2):";
cin >> c;
if (c.length() == 1 && c[0] - '0' == 1) {
cin.ignore ();
cout << "enter cypertext :"; getline(cin, cypertext);
////cout << "enter Key :"; cin >> key;
cout << "plain-text is :" << CompositionDecrypt(cypertext) << "\n";
} else if (c.length() == 1 && c[0] - '0' == 2) {
cin.ignore ();
cout << "enter plainText :"; getline(cin, plainText);
////cout << "enter Key :"; cin >> key;
cout << "cyper-text is :" << CompositionEncrypt(plainText) << "\n";
} else {
cout << "wrong - choose one or two only\n";
}
cout << "To continue enter (1) :";
cin >> x;
///system("clear");
if (!(x.length() == 1 && x[0] - '0' == 1)) break;
}
////////////////////////////////////////////////////////
}
int main() {
///Sakr_
int t = 1;
////cin>>t;
while (t--) {
solveit();
}
}