/* Program to implement Vigenere Cipher encryption and decryption technique.
*/
#include<iostream>
using namespace std;
string generateKey(string str, string key)
{
int x=str.size();
for(int i=0; ;i++)
{
if(x==i)
i=0;
if(key.size()==str.size())
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key)
{
string cipher_text;
for(int i=0;i<str.size();i++)
{
int x=(str[i]+key[i])%26;
x+='A';
cipher_text.push_back(x);
}
return cipher_text;
}
string originalText(string cipher_text,string key)
{
string orig_text;
for(int i=0;i<cipher_text.size();i++)
{
int x=(cipher_text[i]-key[i]+26)%26;
x+='A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
char str[100],keyword[100];
cout<<"\nEnter the string: ";
cin>>str;
cout<<"Enter the keyword: ";
cin>>keyword;
string key=generateKey(str,keyword);
string cipher_text=cipherText(str,key);
cout<<"\nEncrypted text is: "<<cipher_text<<"\n";
cout<<"Decrypted text is: "<<originalText(cipher_text, key);
cout<<"\n"<<"\n";
return 0;
}
OUTPUT SCREEN:
*/
#include<iostream>
using namespace std;
string generateKey(string str, string key)
{
int x=str.size();
for(int i=0; ;i++)
{
if(x==i)
i=0;
if(key.size()==str.size())
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key)
{
string cipher_text;
for(int i=0;i<str.size();i++)
{
int x=(str[i]+key[i])%26;
x+='A';
cipher_text.push_back(x);
}
return cipher_text;
}
string originalText(string cipher_text,string key)
{
string orig_text;
for(int i=0;i<cipher_text.size();i++)
{
int x=(cipher_text[i]-key[i]+26)%26;
x+='A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
char str[100],keyword[100];
cout<<"\nEnter the string: ";
cin>>str;
cout<<"Enter the keyword: ";
cin>>keyword;
string key=generateKey(str,keyword);
string cipher_text=cipherText(str,key);
cout<<"\nEncrypted text is: "<<cipher_text<<"\n";
cout<<"Decrypted text is: "<<originalText(cipher_text, key);
cout<<"\n"<<"\n";
return 0;
}
OUTPUT SCREEN:
No comments:
Post a Comment