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
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string inmsg, key, enkey, cipher;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int msglen, keylen, mIndex[100], kIndex[100], cIndex[100];
void getmsg()
{
int i, x, j;
cout<<"Enter the message: ";
getline(cin, inmsg);
msglen=inmsg.length();
for(j=0;j<msglen;j++)
{
inmsg[j] = toupper(inmsg[j]);
}
for(i=0;i<msglen;i++)
{
x=0;
while(inmsg[i] != alpha[x])
{
x++;
}
mIndex[i] = x;
}
}
void getkey()
{
int i, j, x;
cout<<"Enter the key: ";
cin>>key;
cin.ignore();
keylen=key.length();
enkey=inmsg;
for(i=0; i<msglen; i++)
{
enkey[i]=key[i%keylen];
}
for(int k=0;k<msglen;k++)
{
enkey[k] = toupper(enkey[k]);
}
for(j=0;j<msglen;j++)
{
x=0;
while(enkey[j] != alpha[x])
{
x++;
}
kIndex[j] = x;
}
}
void encrypt()
{
getmsg();
getkey();
cipher=inmsg;
int x;
/*** Do Cipher ***/
cout<<"\nCipher: \n";
for(int i=0;i<msglen;i++)
{
cIndex[i]=(mIndex[i]+kIndex[i])%26;
x = cIndex[i];
cipher[i] = alpha[x];
}
cout<<cipher;
}
void decrypt()
{
getmsg();
getkey();
cipher=inmsg;
int x;
/*** Do Decipher ***/
cout<<"\nDeciphered: \n";
for(int i=0;i<msglen;i++)
{
cIndex[i]=(mIndex[i]-kIndex[i])%26;
x = cIndex[i];
cipher[i] = alpha[x];
}
cout<<cipher;
}
int main()
{
int choice;
cout<<"\tVigenere Cipher\n";
cout<<"1. Encrypt"<<endl;
cout<<"2. Decrypt"<<endl;
cout<<"Enter choice: ";
cin>>choice;
cin.ignore();
switch(choice)
{
case 1:
encrypt();
break;
case 2:
decrypt();
break;
}
}
|