Caeser cipher is a type of substitution ciphers in which letters in a message (c
ID: 3771898 • Letter: C
Question
Caeser cipher is a type of substitution ciphers in which letters in a message (called plaintext) are replaced with other letters to generate a ciphertext. The choice of replacement characters is a function of a key (or a password). The key should be a number between 1 and 26. In encryption (converting plaintext to ciphertext), the letter is replaced by another that is key letters after it, whereas, in decryption (converting ciphertext to plaintext) the letter is replaced by another that is key letters before. The letter after 'Z' is considered to be 'A'. For example, if the key is 2: Encrypting plaintext Apple ciphertext: CRRNG. Decrypting ciphertext: CRRNG plaintext: APPLE. Your program should do the following: Ask the user whether he/she wants to do encryption or decryption. Validate that the user inputs either 'e' for encryption or 'd' for decryption. Ask the user for a message that only has alphabet letters with no spaces. Check the message and remove any non-alphabet letter while displaying a warning, and make all the alphabet letters uppercase. Ask the user for a key between 1 and 26 (inclusive). Validate that the key is within this range. Compute and display the ciphertext if the user chose encryption or the plaintext if the user chose decryption. Your program should display messages identical to the examples below given the same input. Hello! Please press e for encryption and d for decryption e Please enter your Message. The Message should have only Alphabet letters, all spaces and non-alphabet characters will be deleted. I love you! Non Alphabet Character Dropped: Non Alphabet Character Dropped: Non Alphabet Character Dropped:! Please input a key between 1 and 26: 7 The ciphertext is:PSVCLFVBExplanation / Answer
#include<fstream.h>
#include<conio.h>
#include<string.h>
class encoder
{char ch,ch1,filename[25], key;
public:
encoder(int);
void encrypt()
{cout<<">>>"; //for show
ch1=ch^key;
cout<<""; //for show
}
void decrypt()
{char a,b,c,d,e; // temporary variables
cout<<">>>"; //for show
a=ch|key;
b=ch^key;
c=a|(~b);
d=b&c;
e=c&(~d);
ch1=~e;
cout<<""; //for show
}
};
encoder::encoder(int x)
{ cout<<" Enter the file name: ";
cin.getline(filename,25); // collecting file name
int l=strlen(filename);
if(filename[l-4]!='.')
strcat(filename,".txt");
fstream file;
file.open(filename,ios::in|ios::out|ios::nocreate); //opening file
if(!file)
cout<<" File not found!!";
else
{
cout<<"Enter KEY:";
key=getche(); //reading encryption key
cout<<endl;
int p,g; // debug
while(1)
{ if(file.eof()!=0) //eof detection
break;
file>>ch; //reading from file
p=file.tellp(); // debug
g=file.tellg(); // debug
cout<<endl<<"g="<<g<<" p="<<p; // debug
file.seekp(-1,ios::cur); //correction term
//file.seekg(-,ios::cur); alternate correction term
if(x==1)
encrypt();
else
decrypt();
file<<ch1; //writing to file
cout<<" ch="<<ch<<" ch1="<<ch1; // debug
}
cout<<" Operation Sucessful!!";
file.close();
}
}
int choice()
{ // User Menu
char x='e';
clrscr();
cout<<"-------------------- ";
cout<<" Encoder 1.22 ";
cout<<"-------------------- ";
cout<<"Encrypt->e Decrypt->d ";
x=getch();
clrscr();
cout<<"-------------------- ";
cout<<" Encoder 1.22 ";
cout<<"-------------------- ";
if(x=='e'||x=='E')
{cout<<" Encryption Mode -------------------- ";
encoder e(1);
}
else
{cout<<" Decryption Mode -------------------- ";
encoder e(0);
}
return 0;
}
int main()
{char y='y';
do
{choice();
cout<<" Continue ?"; // to repeat or not to repeat
y=getche();
}while(y=='y'||y=='Y');
return 0;
}