Ok I have this code that I need help with... here are the instructions...When yo
ID: 3647911 • Letter: O
Question
Ok I have this code that I need help with... here are the instructions...When you think you have a working program, use Notepad to create a file with plain text in it. You should enter some different length lines containing a variety of characters. Your file should have at least 10 lines. You should try using short and long encryption keys. Using your program, encrypt the file, then decrypt the encrypted file. Take a screen shot of your decrypted output and paste it into a Word document. Also copy the contents of your original file and the encrypted file into the Word document. Clearly label the contents of the Word document. Then copy your source code into your document. Make sure that you have used proper coding style and commenting conventions!here is the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void mapitE(string,char[]);
void mapitD(string,char[]);
void encryptdecrypt(const string,const char[],int,string&);
int main()
{string ende[]={"encrypt","decrypt"},buffer,key,newbuffer="";
char filename[80],enmap[128],demap[128],again;
int i,maplength;
ofstream out;
ifstream in;
int choice;
do
{
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
while(choice<1||choice>2)
{cout<<"invalid choice ";
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
}
cout<<"Enter name of your file to "<<ende[choice-1]<<": ";
cin>>filename;
if (filename=="input")
in.open("input.txt");
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
cout<<"Enter name of your output file: ";
cin>>filename;
if (filename=="output")
out.open("output.txt");
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
if(key.length()>128)
{cout<<"key too long ";
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
}
maplength=key.length();
if(choice==1)
mapitE(key,enmap);
else
mapitD(key,demap);
getline(in,buffer);
while(in)
{
if(choice==1)
encryptdecrypt(buffer,enmap,maplength,newbuffer);
else
encryptdecrypt(buffer,demap,maplength,newbuffer);
out<<newbuffer;
newbuffer.erase(0);
getline(in,buffer);
}
out.close();
in.close();
in.clear();
out.clear();
newbuffer.erase(0);
cout<<"do it again(y/n)? ";
cin>>again;
}while(toupper(again)=='Y');
return 0;
}
void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)
{int i=0;
char t,code;
for(i=0;i<buffer.length();i++)
{ //cout<<buffer[i]<<" "<<i<<endl;
if(isalpha(buffer[i]))
{if(islower(buffer[i]))
code='a';
else
code='A';
t=buffer[i]-code;
//cout<<code<<" "<<buffer[i]<<" "<<t<<" "<<i<<endl;
t=t+map[i%len];
//t=t&;
t=t+code;
newbuffer.push_back(t);
}
else
newbuffer.push_back(buffer[i]);
}
newbuffer.push_back(' ');
}
void mapitE(string key,char map[])
{
int i;
for(i=0;i<key.length();i++)
map[i]=key[i]-'a';
}
void mapitD(string key,char map[])
{
int i;
for(i=0;i<key.length();i++)
map[i]=26-(key[i]-'a');
}
How do I get the decrypted output?
Explanation / Answer
You have to decrypt the encrypted file and save to Read only memory.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.