-
Notifications
You must be signed in to change notification settings - Fork 0
/
Substitution cipher
60 lines (59 loc) · 1.44 KB
/
Substitution cipher
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
#include <iostream>
using namespace std;
const char REAL_ALPHABETH[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' };
int orderIntheAlphabeth(char letter) {
int order=0;
for (int i = 0; i < strlen(REAL_ALPHABETH); ++i) {
if (toupper(letter) == REAL_ALPHABETH[i]) {
order = i;
}
}
return order+1;
}
bool validateKey(char* key) {
if (strlen(key) != 26) {
cout << "Invalid key. It must be 26 symbols.";
return false;
}else {
for (int i = 0; i < strlen(key); ++i) {
if (!isalpha(key[i])) {
cout << "Only letters are allowed to the key!";
return false;
}
for (int j = 0; j < strlen(key); ++j) {
if (key[i] == key[j] && i != j) {
cout << "No repeating sharacters allowed!";
return false;
}
}
}
}
return true;
}
int main()
{
char* alphabeth = new (nothrow) char[27];
char *crypt= new (nothrow) char [100];
cout << "Enter your cipher: ";
cin.getline(alphabeth, 27);
while (validateKey(alphabeth)==false) {
cout << "Try another!";
cin.clear();
cin.getline(alphabeth, 27);
}
cout << "Enter your text: ";
cin.clear();
cin.getline(crypt, 100);
for (int i = 0; i < strlen(crypt); ++i) {
if (isalpha(crypt[i])) {
int order = orderIntheAlphabeth(crypt[i]);
crypt[i] = alphabeth[order-1];
}
}
for (int i = 0; i < strlen(crypt); ++i) {
cout << crypt[i];
}
delete[] alphabeth;
delete[] crypt;
return 0;
}