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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
class Substitution // Substitution Cipher class
{
char str[500];
public:
void sgetstring(void); // method to get input string and key
void sencryption(void); // encryption method for Substitution Cipher
void sdecryption(void); // decryption method for Substitution Cipher
};
void Substitution::sgetstring()
{
int size = 500;
cout << "\n Enter the Input String: "; // Input for Substitution Cipher
cin.getline(str, size, '\n');
}
void Substitution::sencryption()
{
int key, i, t1;
char c;
cout << "\n Enter Key: ";
cin >> key;
cout << "\n Encryption : ";
//Encryption
for (i = 0; i<strlen(str); i++)
{
c = str[i];
if (c == -1)
break;
if (isupper(c))
{
t1 = (int)c - (int) 'A';
t1 = (t1 + key) % 26;
t1 = t1 + (int) 'A';
cout << (char)t1;
}
else if (islower(c))
{
t1 = (int)c - (int) 'a';
t1 = (t1 + key) % 26;
t1 = t1 + (int) 'a';
cout << (char)t1;
}
else{
cout << c;
}
}
}
void Substitution::sdecryption()
{
int key, i, t1;
char c;
cout << "\n Decryption : \n";
//decryption
for (key = 0; key<26; key++)
{
for (i = 0; i<strlen(str); i++)
{
c = str[i];
if (c == -1)
break;
if (isupper(c))
{
t1 = (int)c - (int) 'A';
t1 = (t1 - key + 26) % 26;
t1 = t1 + (int) 'A';
cout << (char)t1;
}
else if (islower(c))
{
t1 = (int)c - (int) 'a';
t1 = (t1 - key + 26) % 26;
t1 = t1 + (int) 'a';
cout << (char)t1;
}
else{
cout << c;
}
}
cout << "\n";
}
//ending
}
class Transposition // Transposition Cipher class
{
char arr[22][22], darr[22][22], message[111], emessage[111], retmessage[111], key[55];
char temp[55], temp2[55];
public:
void tgetstring(); // method to get input string and key
void tencryption(); // encryption method for Transposition Cipher
void tdecryption(); // decryption method for Transposition Cipher
void tcipher(int i, int c); // Transposition Cipher module
int tfindMin(); // method to find the minimum values from the given key
void tmakeArray(int, int); // method to get the result encrypted / decrypted array
};
void Transposition::tgetstring()
{
cout << "\n Enter the key\n";
cin.getline(key, 111, '\n');
//gets(key);
cout << "\n Enter message to be ciphered\n";
cin.getline(message, 111, '\n');
//gets(message);
}
void Transposition::tencryption()
{
int i, j, klen, emlen, flag = 0;
int r, c, index, rows, k = 0;
strcpy_s(temp, key);
klen = strlen(key);
k = 0;
for (i = 0;; i++)
{
if (flag == 1)
break;
for (j = 0; key[j] != '\0'; j++)
{
if (message[k] == '\0')
{
flag = 1;
arr[i][j] = '-';
}
else
{
arr[i][j] = message[k++];
}
}
}
r = i;
c = j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
cout << arr[i][j];
}
cout << "\n";
}
cout << "\n Encrypted message is\n";
k = 0;
for (i = 0; i < klen; i++)
{
index = this->tfindMin();
this->tcipher(index, r);
}
emessage[k] = '\0';
//printf("\nEncrypted message is\n");
//for (i = 0; i<strlen(emessage); i++)
//{
// printf("%c", emessage[i]);
//}
cout << "\n\n";
}
void Transposition::tdecryption()
{
int i, j, klen, emlen, flag = 0;
int r, c, index, rows, k;
//deciphering
emlen = strlen(message);
klen = strlen(key);
//emlen is length of encrypted message
strcpy_s(temp, key);
rows = emlen / klen;
//rows is no of row of the array to made from ciphered message
k = 0;
for (i = 0;; i++)
{
if (flag == 1)
break;
for (j = 0; key[j] != '\0'; j++)
{
if (message[k] == '\0')
{
flag = 1;
arr[i][j] = '-';
}
else
{
arr[i][j] = message[k++];
}
}
}
r = i;
c = j;
j = 0;
for (i = 0, k = 1; i<emlen; i++, k++)
{
//printf("\nEmlen=%d",emlen);
temp2[j++] = message[i];
if ((k % rows) == 0)
{
temp2[j] = '\0';
index = this->tfindMin();
this->tmakeArray(index, rows);
j = 0;
}
}
cout << "\n Array Retrieved is\n";
k = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
cout << darr[i][j];
//retrieving message
retmessage[k++] = darr[i][j];
}
cout << "\n";
}
retmessage[k] = '\0';
cout << "\n Message retrieved is\n";
for (i = 0; retmessage[i] != '\0'; i++)
cout << retmessage[i];
}
void Transposition::tcipher(int i, int r)
{
int j, k = 0;
for (j = 0; j < r; j++)
{
{
emessage[k++] = arr[j][i];
cout << emessage[k - 1];
}
}
// emessage[k]='\0';
}
void Transposition::tmakeArray(int col, int row)
{
int i, j;
for (i = 0; i < row; i++)
{
darr[i][col] = temp2[i];
}
}
int Transposition::tfindMin()
{
int i, j, min, index;
min = temp[0];
index = 0;
for (j = 0; temp[j] != '\0'; j++)
{
if (temp[j] < min)
{
min = temp[j];
index = j;
}
}
temp[index] = 123;
return (index);
}
int main(int argc, char** argv) // main method
{
int opa, ope;
char flag;
cout << "\n Select Algorithm: \n";
cout << "\n 1. Substitution :";
cout << "\n 2. Transposition :";
cin >> opa;
fflush(stdin);
if (opa == 1)
{
Substitution s; // creating object of Substitution class
cout << "\n Substitution Cipher \n";
s.sgetstring();
cout << "\n What do you want to perform: \n";
cout << "\n 1. Encryption :";
cout << "\n 2. Decryption :";
cin >> ope;
if (ope == 1)
{
s.sencryption();
}
else
{
s.sdecryption();
}
}
if (opa == 2)
{
Transposition t; // creating object of Transposition class
cout << "\n Transposition Cipher \n";
t.tgetstring();
cout << "\n What do you want to perform: \n";
cout << "\n 1. Encryption :";
cout << "\n 2. Decryption :";
cin >> ope;
if (ope == 1)
{
t.tencryption();
}
else
{
t.tdecryption();
}
}
cout << "\n\n\n Done";
//getch();
cin >> flag;
return (0);
}
|