-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVigenereCipher.cpp
158 lines (132 loc) · 3.61 KB
/
VigenereCipher.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
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
using namespace std;
string message;
string mappedKey;
void messageAndKey(){
string msg;
cout << "Enter message: ";
getline(cin, msg);
cin.ignore();
//message to uppercase
for(int i = 0; i < msg.length(); i++){
msg[i] = toupper(msg[i]);
}
string key;
cout << "Enter key: ";
getline(cin, key);
cin.ignore();
//key to uppercase
for(int i = 0; i < key.length(); i++){
key[i] = toupper(key[i]);
}
//mapping key to message
string keyMap = "";
for (int i = 0,j = 0; i <msg.length();i++){
if(msg[i] ==32){
keyMap += 32;
} else {
if(j<key.length()){
keyMap += key[j];
j++;
} else {
j = 0;
keyMap += key[j];
j++;
}
} //if-else
} //for
// cout << msg << "\n" << keyMap;
message = msg;
mappedKey = keyMap;
}
int tableArr[26][26];
void createVigenereTable(){
for (int i = 0; i < 26; i++){
for (int j = 0; j < 26; j++){
int temp;
if((i+65)+j > 90){
temp = ((i+65)+j) - 26;
//adding ASCII of alphabet letter in table index position
tableArr[i][j] = temp;
} else {
temp = (i+65)+j;
//adding ASCII of alphabet letter in table index position
tableArr[i][j] = temp;
}
} // for j loop
} // for i loop
//testing the table
// for(int i = 0; i <26; i++){
// for(int j = 0; j < 26; j++){
// cout << (char)tableArr[i][j] << " ";
// }
// cout << endl;
// }
}
void cipherEncryption(string message, string mappedKey){
createVigenereTable();
string encryptedText = "";
for(int i = 0; i < message.length(); i++){
if(message[i] == 32 && mappedKey[i] == 32){
encryptedText += " ";
} else {
int x = (int)message[i]-65;
int y = (int)mappedKey[i]-65;
encryptedText += (char)tableArr[x][y];
}
}
cout << "Encrypted Text: " << encryptedText;
}
int itrCount(int key, int msg){
int counter = 0;
string result = "";
//starting from ASCII of letter of Key and ending at letter of message
// to get full 26 letters of alphabet
for(int i = 0; i < 26; i++){
if(key+i > 90){
result += (char)(key+(i-26));
} else {
result += (char)(key+i);
}
} //for
for(int i = 0; i < result.length(); i++){
if(result[i] == msg){
break;
} else {
counter++;
}
}
return counter;
}
void cipherDecryption(string message, string mappedKey){
string decryptedText = "";
for(int i = 0; i < message.length(); i++){
if(message[i] == 32 && mappedKey[i] == 32){
decryptedText += " ";
} else {
int temp = itrCount((int)mappedKey[i], (int)message[i]);
decryptedText += (char)(65+temp);
}
}
cout << "Decrypted Text: " << decryptedText;
}
int main()
{
cout << "Message and key can only be alphabetic" << endl;
int choice;
cout << "1. Encryption\n2. Decryption\nChoose(1,2): ";
cin >> choice;
cin.ignore();
if (choice == 1){
cout << "Encryption" << endl;
messageAndKey();
cipherEncryption(message, mappedKey);
} else if (choice == 2){
cout << "Decryption" << endl;
messageAndKey();
cipherDecryption(message, mappedKey);
} else {
cout << "Wrong Choice" << endl;
}
return 0;
}