-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyword.cpp
146 lines (124 loc) · 3.82 KB
/
Keyword.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cstdlib>
using namespace std;
string keywordPicker(){
cout << "Keyword must contain each letter A-Z exactly once, no repetition of letter should occur" << endl;
string keywrd;
cout << "Enter keyword: ";
getline(cin, keywrd);
cin.ignore();
//keyword to uppercase
for(int i = 0; i < keywrd.length(); i++){
keywrd[i] = toupper(keywrd[i]);
}
//checking repetition of letter in keyword
for (int i = 0; i < keywrd.length(); i++){
int pos = i;
for(int j = 0; j < keywrd.length(); j++){
if(pos == j){
continue;
} else if(keywrd[i] == keywrd[j]){
cout << "Letter \"" << keywrd[i] << "\" Repeating in keyword" << endl;
exit(EXIT_FAILURE); // terminating the program
}
} //inner for
} // for
//generating alphabet with keyword
string temp = "";
for(int i = 0; i < keywrd.length(); i++){
temp += keywrd[i];
}
for(int i = 0; i < 26; i++){
temp += (char)(i+65);
}
//removing letters of keyword from alphabet
for (int i = 0; i < temp.length(); i++){
bool found = false;
for(int j = 0; j < keywrd.length(); j++){
if(temp[i] == keywrd[j]){
found = true;
break; // no need to iterate any further
}
} // inner for
if(found == false){
keywrd += temp[i];
}
} // for loop
// cout << keywrd;
return keywrd;
}
void cipherEncryption(string alpha, string keywrd){
string message;
cout << "Enter message: ";
getline(cin, message);
cin.ignore();
//message to uppercase
for(int i = 0; i < message.length(); i++){
message[i] = toupper(message[i]);
}
string encryText = "";
for (int i = 0; i < message.length(); i++){
if(message[i] == (char)32){
encryText += " ";
} else {
int counter = 0;
for (int j = 0; j < alpha.length(); j++){
if(message[i] == alpha[j]){
//character at count location of alphabet is encrypted letter in key
encryText += keywrd[counter];
break;
} else {
counter++;
}
} // inner for
} //if-else
} //for
cout << "Encrypted Text: " << encryText;
}
void cipherDecryption(string alpha, string keywrd){
string message;
cout << "Enter Encrypted Message: ";
getline(cin, message);
cin.ignore();
//message to uppercase
for(int i = 0; i < message.length(); i++){
message[i] = toupper(message[i]);
}
string decryText = "";
for (int i = 0; i < message.length(); i++){
if(message[i] == (char)32){
decryText += " ";
} else {
int counter = 0;
for (int j = 0; j < alpha.length(); j++){
if(message[i] == keywrd[j]){
//character at count location of alphabet is encrypted letter in key
decryText += alpha[counter];
break;
} else {
counter++;
}
} // inner for
} //if-else
} //for
cout << "Decrypted Text: " << decryText;
}
int main()
{
int choice;
cout << "1. Encryption\n2. Decryption\nChoose(1,2): ";
cin >> choice;
cin.ignore();
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string keywrd = keywordPicker();
if(choice == 1){
cout << endl << "---Encryption---" << endl;
cipherEncryption(alpha, keywrd);
} else if (choice == 2){
cout << endl << "---Decryption---" << endl;
cipherDecryption(alpha, keywrd);
} else {
cout << endl << "Wrong choice" << endl;
}
return 0;
}