a message has been encrypted and put in the file \"secret.txt\". The original me
ID: 3770638 • Letter: A
Question
a message has been encrypted and put in the file "secret.txt". The original message was inputted word by word. to encrypt the n th word in a file, each small letter in the word was "rotated up" by n plus the length of the word, with "wraparound".
write a c++ code program which decrypts the secret message and output the plain text result. you should input the encrypted message ("secret.txt")word by word, undo the encryption, output result. .
ex: tli xbpjr jzwev mve ufxapo yfob dro xmlk qbt. A qxgs wb jxu zsfv cwpdano fg ikoo, wpo u spmmjoh stone gq yqtvj uxp jo wkh hayn.
should be : the quick brown fox jumped over the lazy dog. A bird in the hand gathers no moss, but a rolling Stone is worth two in the bush.
In the illustration above, the first encrypted word is tli. The t need no decryption. The l should be rotated down by 1+3=4 ( we are in the first word. and the word length is 3.
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string buff;
string words[100];
ifstream myfile("secret.txt");
int i = 0;
int word_count = 0, new_sentence = 1;
if(!myfile)
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while(!myfile.eof())
{
getline(myfile,buff,' ');
cout<<buff<<" ";
words[i] = buff;
//cout<<"2."<<a<<" "<<words[a]<<" ";
i++;
}
cout<<endl;
word_count = i--;
for(i = 0; i<word_count;i++){
buff = words[i];
int l = buff.size();
int rotateBy, offset = 0;
char ch;
for(std::string::iterator it = buff.begin(); it != buff.end(); ++it) {
rotateBy = (i%26)+l+1;
if (*it != ',' && *it != '.' && new_sentence == 0){
offset = ((*it)-rotateBy);
// cout << offset;
if (offset < 97)
offset += 26;
else if (offset > 122)
offset -= 26;
ch = offset;
*it = ch;
//new_sentence = 0;
}
else if (*it == '.')
new_sentence = 1;
else
new_sentence = 0;
}
cout<<buff<<" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.