Question

CaesarCipher Introduction Hiding the meaning of messages by putting them in some kind of code is...

CaesarCipher


Introduction

Hiding the meaning of messages by putting them in some kind of code is something we have all done, from the “coding rings” and secret symbols of childhood to the top secrecy of military and commercial establishments’ secret data. In fact, some of the earliest uses of computers were for coding messages and for breaking the enemy’s coded messages. Newer coding methods are among the most interesting research areas in computer science today.

The Caesar Cipher

One of the simplest examples of a substitution cipher is the Caesar Cipher, which is said to have been used by Julius Caesar to communicate with his army. Caesar is considered to be one of the first persons to have ever employed encryption for the sake of securing messages. Caesar decided that shifting each letter in the message would be his standard algorithm, and so he informed all of his generals of his decision, and was then able to send them secured messages. Using the Caesar Shift (3 to the right), the message,

RETURNTOROME  would be encrypted as UHWXUQWRURPH

In this example, 'R'  'U', 'E'  'H', and so on. Now, even if the enemy did intercept the message, it would be useless, since only Caesar’s generals could read it.


The Assignment

Write a program to implement the Caesar Cipher

Problem

Write a c++ program that will ask the user to enter the name of a file. Your program should then ask the user to enter 'encrypt' or 'decrypt'.

If the user enters anything else your program should print an error message and exit.

If they entered 'encrypt' your program should open the file and encrypt it by shifting each character (including white space) 3 values to the right.

If they entered 'decrypt' your program should open the file and decrypt it by shifting each character (including white space) 3 values to the left.

If the file fails to open your program should print an error message and exit.

The decrypted or encrypted message should be displayed on the screen and also written to an output file named “message”.

Sample Solution Output

Enter File Name: This_is_a_bad_file_name
Enter encrypt or decrypt: decrypt
Error: File did NOT open.
____________________________________________________________________________________________________________

Enter File Name: plainText.txt
Enter encrypt or decrypt: openFile
Error: Bad Command.

____________________________________________________________________________________________________________

Enter File Name: plainText.txt
Enter encrypt or decrypt: encrypt
Vhg#xw#shuvslfldwlv#xqgh#rpqlv#lvwh#qdwxv#huuru#vlw#yroxswdwhp#dffxvdqwlxp#groruhptxh#odxgdqwlxp/#
wrwdp#uhp#dshuldp/#hdtxh#lsvd#txdh#de#loor#lqyhqwruh#yhulwdwlv#hw#txdvl#dufklwhfwr#ehdwdh#ylwdh#
glfwd#vxqw#h{solfder1#Qhpr#hqlp#lsvdp#yroxswdwhp#txld#yroxswdv#vlw#dvshuqdwxu#dxw#rglw#dxw#ixjlw/#
vhg#txld#frqvhtxxqwxu#pdjql#groruhv#hrv#txl#udwlrqh#yroxswdwhp#vhtxl#qhvflxqw1#Qhtxh#sruur#txlvtxdp#
hvw/#txl#groruhp#lsvxp#txld#groru#vlw#dphw/#frqvhfwhwxu/#dglslvfl#yholw/#vhg#txld#qrq#qxptxdp#hlxv#
prgl#whpsrud#lqflgxqw#xw#oderuh#hw#groruh#pdjqdp#doltxdp#txdhudw#yroxswdwhp1#Xw#hqlp#dg#plqlpd#
yhqldp/#txlv#qrvwuxp#h{huflwdwlrqhp#xoodp#frusrulv#vxvflslw#oderulrvdp/#qlvl#xw#doltxlg#h{#hd#
frpprgl#frqvhtxdwxuB#

Txlv#dxwhp#yho#hxp#lxuh#uhsuhkhqghulw#txl#lq#hd#yroxswdwh#yholw#hvvh#txdp#qlklo#prohvwldh#frqvhtxdwxu/#
yho#looxp#txl#groruhp#hxp#ixjldw#txr#yroxswdv#qxood#sduldwxuB

____________________________________________________________________________________________________________

OUTPUT FILE: message

Vhg#xw#shuvslfldwlv#xqgh#rpqlv#lvwh#qdwxv#huuru#vlw#yroxswdwhp#dffxvdqwlxp#groruhptxh#odxgdqwlxp/#
wrwdp#uhp#dshuldp/#hdtxh#lsvd#txdh#de#loor#lqyhqwruh#yhulwdwlv#hw#txdvl#dufklwhfwr#ehdwdh#ylwdh#
glfwd#vxqw#h{solfder1#Qhpr#hqlp#lsvdp#yroxswdwhp#txld#yroxswdv#vlw#dvshuqdwxu#dxw#rglw#dxw#ixjlw/#
vhg#txld#frqvhtxxqwxu#pdjql#groruhv#hrv#txl#udwlrqh#yroxswdwhp#vhtxl#qhvflxqw1#Qhtxh#sruur#txlvtxdp#
hvw/#txl#groruhp#lsvxp#txld#groru#vlw#dphw/#frqvhfwhwxu/#dglslvfl#yholw/#vhg#txld#qrq#qxptxdp#hlxv#
prgl#whpsrud#lqflgxqw#xw#oderuh#hw#groruh#pdjqdp#doltxdp#txdhudw#yroxswdwhp1#Xw#hqlp#dg#plqlpd#
yhqldp/#txlv#qrvwuxp#h{huflwdwlrqhp#xoodp#frusrulv#vxvflslw#oderulrvdp/#qlvl#xw#doltxlg#h{#hd#
frpprgl#frqvhtxdwxuB#

Txlv#dxwhp#yho#hxp#lxuh#uhsuhkhqghulw#txl#lq#hd#yroxswdwh#yholw#hvvh#txdp#qlklo#prohvwldh#frqvhtxdwxu/#
yho#looxp#txl#groruhp#hxp#ixjldw#txr#yroxswdv#qxood#sduldwxuB

____________________________________________________________________________________________________________

Enter File Name: encrypted.txt
Enter encrypt or decrypt: decrypt
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, 
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, 
sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam 
est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius 
modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima 
veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea 
commodi consequatur? 

Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, 
vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

____________________________________________________________________________________________________________

OUTPUT FILE: message

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, 
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, 
sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam 
est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius 
modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima 
veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea 
commodi consequatur? 

Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, 
vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?



0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char fname[100];
cout<<"Enter File Name: "; // take input file name;
cin>>fname;
char command[100];
cout<<"Enter encrypt or decrypt: "; // input the command
cin>>command;
string line,ans_line;
ifstream infile(fname);
ofstream outfile("output"); // open output file
  
if(infile.good()) // if input file exist
{
if(strcmp(command,"encrypt")==0) // if command was encrypt
{
while(getline(infile,line)) // for each line in input file
{
// cout<<line<<endl;
for(int i=0;i<line.size();i++) // for each character of a line
{
line[i]= line[i]+3; // shift right
}
outfile<<line<<endl; // print the line in the output file
}
  
}
else if(strcmp(command,"encrypt")==0)
{
while(getline(infile,line))// for each line in input file
{
// cout<<line<<endl;
for(int i=0;i<line.size();i++) // for each character in line shift left
{
line[i]= line[i]-3;  
}
outfile<<line<<endl; // print the line in output file
}
}
else
{
cout<<"Error: Bad Command."<<endl; // if any other command show error
}
}
else
cout<<"Error: File did NOT open."<<endl; /// show error if file is not opened
  
}

Thank you ?

Add a comment
Know the answer?
Add Answer to:
CaesarCipher Introduction Hiding the meaning of messages by putting them in some kind of code is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore...

    Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem...

  • python program do not use dictionary, list only Complete the program found in assignment4.py. You may...

    python program do not use dictionary, list only Complete the program found in assignment4.py. You may not change any provided code. You may only complete the sections labeled: #YOUR CODE HERE Write a program that does the following. Reads the contents of Text from the include file, input.txt Create a dictionary of key-value pairs called index.txt Key: This represents the individual word Value: This is a list of the line number from Text where Key appeared Example: If the word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT