#include<iostream>
#include<string>
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()
{
int x;
cout<<"Enter your choice"<<endl;
cout<<"1. Mono-alphabetic\n2. Poly-alphabetic\n";
cin>>x;
switch(x)
{
case 1:
cout<<"Mono-alphabetic cipher"<<endl;
cout<<"Enter your choice \n 1.Encryption\n 2.Decryption\n";
int b;
cin>>b;
switch(b)
{
case 1:
int k,i;
char plainText[100];
cout<<"Encryption\n";
cout<<"Enter Plain text"<<endl;
cin>>plainText;
cout<<"Enter Encryption key"<<endl;
cin>>k;
for(i=0;(i<100 && plainText[i]!='\0');i++)
plainText[i]=plainText[i]+k;
cout<<"\n Encrypted text:"<<plainText<<endl;
break;
case 2:
int d;
char encrypedText[100];
cout<<"Decryption\n";
cout<<"Enter Encrypted text"<<endl;
cin>>encrypedText;
cout<<"Enter Decryption key"<<endl;
cin>>d;
for(i=0;(i<100 && encrypedText[i]!='\0');i++)
encrypedText[i]=encrypedText[i]-d;
cout<<"\n Decrypted text:"<<encrypedText<<endl;
break;
default:
cout<<"WRONG Choice Entered!!!\n";
break;
}
case 2:
{
cout<<"Poly-alphabetic"<<endl;
string str ="PLANTTHATBOMBTHERE";
string keyword="AMAZON";
string key =generateKey(str,key);
string cipher_text = cipherText(str,key);
cout<<"Ciphertext:"<<cipher_text<<endl;
cout<<"Original text:"<<originalText(cipher_text, key);
break;
break;
}
}
}
This code is for substitution cipher and the poly-alphabetic cipher part is not running which is showing std::logic_error error and what(): basic_string::_M_construct null not valid error please help the first part is running ok without any error but the polyalphabetic cipher part does not show any compilation error it only shows run time error