I NEED TO ADD DECRYPTION FUNCTIONS CORRESPONDING TO THE PROGRAM BELOW. PLEASE DO
ID: 3587206 • Letter: I
Question
I NEED TO ADD DECRYPTION FUNCTIONS CORRESPONDING TO THE PROGRAM BELOW. PLEASE DON'T GIVE ME JUST DECRYPTION FUNCTIONS ALONE. IT'S NEED TO BE ADDED TO THE CURRENT PROGRAM ALONE.
THANK YOU.
// PROGRAM
#include <iostream>
using namespace std;
// TASK 1
string encryptCaeser(string str, int rshift) // Caeser chiper
{
int i,j;
string output="";
char c;
for(i=0;i<str.size();i++)
{
if(str[i]>='a' && str[i]<='z')
{
j=(str[i]-'a'+rshift+26)%26;
c=j+97;
output+=c;
}
else if(str[i]>='A' && str[i]<='Z')
{
j=(str[i]-'A'+rshift+26)%26;
c=j+65;
output+=c;
}
else
output+=str[i];
}
return output;
}
//TASK 2
string encryptVigenere(string text, string key)
{
int num[10]; //to store the number equivalent of the key letters
for(int i=0; i<key.size(); i++)
{
num[i] = int(key[i])-97; //getting the number equivalent for key letters
}
int j=0; //for indexing num
for(int i=0; i<(text.size()); i++)
{
if((text[i] <65 || text[i]>90) && (text[i] <97 || text[i]>122))
continue; //for anything other than small and capital alphabets
else
{
//right shifting the text using num
text[i] = ((text[i]-97+num[j%key.size()])%26) +97;
j++; //next num
}
}
return text; //returning the shifted text
}
int main(){
string text;
cout<<"Enter the text: ";
getline(cin,text);
cout<<encryptCaeser(text,23);
//For task 2:
string key;
cout<<" Enter the text: ";
getline(cin,text);
cout<<"Now enter the key: ";
cin>>key;
cout<<encryptVigenere(text,key);
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
// TASK 1
string encryptCaeser(string str, int rshift) // Caeser chiper
{
int i,j;
string output="";
char c;
for(i=0;i<str.size();i++)
{
if(str[i]>='a' && str[i]<='z')
{
j=(str[i]-'a'+rshift+26)%26;
c=j+97;
output+=c;
}
else if(str[i]>='A' && str[i]<='Z')
{
j=(str[i]-'A'+rshift+26)%26;
c=j+65;
output+=c;
}
else
output+=str[i];
}
return output;
}
//decryptinon..function
string decryptVigenere(string text, string key) {
int num[10]; //to store the number equivalent of the key letters
for(int i=0; i<key.size(); i++)
{
num[i] = int(key[i])-97; //getting the number equivalent for key letters
}
int j=0; //for indexing num
for(int i=0; i<(text.size()); i++)
{
if((text[i] <65 || text[i]>90) && (text[i] <97 || text[i]>122))
continue; //for anything other than small and capital alphabets
else
{
//right shifting the text using num
// text[i] = ((text[i]-97+num[j%key.size()])) +97;
//cout<<(int)text[i]<<endl;
text[i] = text[i]-97-(num[j%key.size()])+97;
// cout<<(int)text[i]<<endl;
j++; //next num
}
}
return text;
}
//TASK 2
string encryptVigenere(string text, string key)
{
int num[10]; //to store the number equivalent of the key letters
for(int i=0; i<key.size(); i++)
{
num[i] = int(key[i])-97; //getting the number equivalent for key letters
}
int j=0; //for indexing num
for(int i=0; i<(text.size()); i++)
{
if((text[i] <65 || text[i]>90) && (text[i] <97 || text[i]>122))
continue; //for anything other than small and capital alphabets
else
{
//right shifting the text using num
text[i] = ((text[i]-97+num[j%key.size()])%26) +97;
j++; //next num
}
}
return text; //returning the shifted text
}
int main(){
string text;
cout<<"Enter the text: ";
getline(cin,text);
cout<<encryptCaeser(text,23);
//For task 2:
string key;
cout<<" Enter the text: ";
getline(cin,text);
cout<<"Now enter the key: ";
cin>>key;
string en =encryptVigenere(text,key) ;
cout<<"Encrypted : "<<en<<endl;
cout<<"Decrpted : "<<decryptVigenere(en,key)<<endl;
return 0;
}
output:
Enter the text: hello
ebiil
Enter the text: hello
Now enter the key: abcde
Encrypted : hfnos
Decrpted : hello
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.