Could someone write the decryption(NOT DESCRIPTION) for function below? Thank yo
ID: 3587398 • Letter: C
Question
Could someone write the decryption(NOT DESCRIPTION) for function below? Thank you.
// CODE STARTS HERE
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;
}
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
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
}
//cout<<text[i]<<endl;
}
return text;
}
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;
}
int main()
{
string text="abcde";//string text
string key = "abcde";//key
string encrypt = encryptVigenere(text,key);//caling encryption function
cout<<"Encrypted string is:"<<encrypt<<endl;
cout<<"Decrypted string is:"<<decryptVigenere(encrypt,key)<<endl;
}
output:-
Encrypted string is:acegi
Decrypted string is:abcde
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.