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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include <iostream>
#include <regex>
#include <string>
#include <sstream>
using namespace std;
string getNumberLocation(string keyword, string kywrdNumListStr) {
int kywrdNumList[keyword.length()];
//cout << sizeof(kywrdNumList);
for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
// using ASCII code '0' is 48, '1' 49 and so on until '9' as 57
kywrdNumList[i] = kywrdNumListStr[i] - 48;
// cout << kywrdNumList[i] << endl;
}
string numLoc = "";
for (int i = 1; i < keyword.length() + 1; i++) {
for (int j = 0; j < keyword.length(); j++) {
if (kywrdNumList[j] == i){
numLoc += to_string(j);
}
}
}
return numLoc;
} // getting number location function
string keywordNumAssign(string keyword){
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int kywrdNumList[keyword.length()];
int init = 0;
for (int i = 0; i < alpha.length(); i ++){
for (int j = 0; j < keyword.length(); j++) {
if (alpha[i] == keyword[j]){
init++;
kywrdNumList[j] = init;
// cout << kywrdNumList[j];
}
} // inner for
} // for
string str = "";
for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
// cout << kywrdNumList[i] << " ";
str += to_string(kywrdNumList[i]);
}
// string str = to_string(kywrdNumList);
// cout << str;
return str;
}
void cipherEncryption(){
string msg;
cout << "Enter Plain Text: ";
getline(cin, msg);
string keyword;
cout << "Enter Keyword: ";
getline(cin, keyword);
// plain text to uppercase
for (int i = 0; i < msg.length(); i++){
msg[i] = toupper(msg[i]);
}
// replacting whitespace in plain text
msg = regex_replace(msg,regex("\\s+"), "");
// keyword to uppercase
for (int i = 0; i < keyword.length(); i++){
keyword[i] = toupper(keyword[i]);
}
for (int i = 0; i < keyword.length(); i++){
cout << keyword[i] << " ";
}
cout << endl;
// assigning numbers to keywords
string kywrdNumListStr = keywordNumAssign(keyword);
int kywrdNumList[keyword.length()];
//cout << sizeof(kywrdNumList);
for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
// using ASCII code '0' is 48, '1' 49 and so on until '9' as 57
kywrdNumList[i] = kywrdNumListStr[i] - 48;
// cout << kywrdNumList[i] << endl;
}
for (int i: kywrdNumList){
cout << i << " ";
}
// in case characters don't fit the entire grid perfectly.
int extraLetters = msg.length() % keyword.length();
// cout << endl << extraLetters << endl;
int dummyCharacters = keyword.length() - extraLetters;
// cout << endl << dummyCharacters << endl;
if (extraLetters != 0){
for (int i = 0; i < dummyCharacters; i++) {
msg += ".";
}
}
int numOfRows = msg.length() / keyword.length();
// Converting message into a grid
char arr[numOfRows][keyword.length()];
int z = 0;
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < keyword.length(); j++) {
arr[i][j] = msg[z];
z++;
}
}
cout << endl;
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < keyword.length(); j++) {
cout << arr[i][j] << " ";
} // inner for loop
cout << endl;
} // for loop
// cipher text generation
string cipherText = "";
// getting locations of numbers
string numLoc = getNumberLocation(keyword, kywrdNumListStr);
cout << numLoc << endl;
cout << endl;
for (int i = 0, k = 0; i < numOfRows; i++, k++) {
int d;
if (k == keyword.length()){
break;
} else {
// d = Character.getNumericValue(numLoc.charAt(k));
d = numLoc[k];
d = d- '0';
}
for (int j = 0; j < numOfRows; j++) {
cipherText += arr[j][d];
}
}
cout << cipherText;
}
void cipherDecryption(){
string msg;
cout << "Enter Cipher Text: ";
getline(cin, msg);
string keyword;
cout << "Enter Keyword: ";
getline(cin, keyword);
// keyword to uppercase
for (int i = 0; i < keyword.length(); i++){
keyword[i] = toupper(keyword[i]);
}
// assigning numbers to keywords
string kywrdNumListStr = keywordNumAssign(keyword);
int kywrdNumList[keyword.length()];
//cout << sizeof(kywrdNumList);
for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
// using ASCII code '0' is 48, '1' 49 and so on until '9' as 57
kywrdNumList[i] = kywrdNumListStr[i] - 48;
// cout << kywrdNumList[i] << endl;
}
int numOfRows = msg.length() / keyword.length();
// Converting message into a grid
char arr[numOfRows][keyword.length()];
// getting locations of numbers
string numLoc = getNumberLocation(keyword, kywrdNumListStr);
for (int i = 0, k = 0; i < msg.length(); i++, k++) {
int d;
if (k == keyword.length()){
k = 0;
} else {
d = numLoc[k];
d = d- '0';
}
for (int j = 0; j < numOfRows; j++, i++) {
arr[j][d] = msg[i];
// cout << arr[j][d];
}
i--;
}
cout << endl;
string plainText = "";
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < keyword.length(); j++) {
plainText += arr[i][j];
} // inner for loop
} // for loop
cout << "Plain Text: " << plainText << endl;
}
int main()
{
cout << "Columnar Transposition Cipher" << endl;
cout << "1. Encryption\n2. Decryption\nChoose(1,2): ";
int choice;
cin >> choice;
cin.ignore();
if (choice == 1){
cout << endl << "Encryption" << endl;
cipherEncryption();
} else if (choice == 2){
cout << endl << "Decryption" << endl;
cipherDecryption();
} else {
cout << endl << "Wrong Choice" << endl;
}
return 0;
}
|