write a program that will read the contents of a file (message.txt) which contai
ID: 3776699 • Letter: W
Question
write a program that will read the contents of a file (message.txt) which contains a message that the user wishes to encrypt. The encrypted message will be saved to a file called (encrypted_message.txt). A simple encryption algorithm replaces a character from the original message with another character that is a fixed number of positions in the ASCII chart. For example, if each character is replaced by the character that is two characters to the right, then the letter ‘a’ is replaced by the letter ‘c’, the letter ‘b’ is replaced by the letter ‘d’, and so on.
Your program should allow the user to choose the “key” for encryption, i.e. if the user enters 4 then each character is replaced by a character that is 4 characters to the right, if the user enters -4 then each character is replaced by 4 characters to the left. Use an ASCII character chart to make sure that you are replacing the characters properly.
Your program should also create a file called “key.txt” that contains the key value used to encrypt the message.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#define MAX 2000
using namespace std;
void enchrypt(char *s,int key);
int main()
{
ifstream inFile;
ofstream outFile1,outFile2;
int key,c;
char ch;
//msg for decrypt
char str[MAX],buf[MAX];
inFile.open("Message.txt");
outFile1.open("key.txt");
outFile2.open("encrypted_message.txt");
//check if opening file was successful
if(!inFile || !outFile1 || !outFile2)
{
cout<<"Not able to open file ... ";
return -1;
}
cout<<"Enter the keyy for enchryption: ";
cin>>key;
outFile1<<key;
int i = 0;
while(!inFile.eof())
{
inFile.getline(str,MAX);
enchrypt(str,key);
outFile2<<str;
outFile2<<endl;
}
inFile.close();
outFile1.close();
outFile2.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.