Question

Programming Requirements You will write a program consisting of the following files: cipher.cpp: Contains your main...

Programming Requirements

You will write a program consisting of the following files:

  1. cipher.cpp: Contains your main function, provides all input/output with the user, and is responsible for reading the file into a buffer (i.e., a string).
  2. encrypt.cpp: Contains a function named encrypt, that takes two parameters: (i) a string containing text to encrypt, and an integer for the shift value. The function should declare a string, store the encrypted text in it, and return the string.
  3. decrypt.cpp: Contains a function named decrypt, that takes two parameters: (i) a string containing text to decrypt, and an integer for the shift value. The function should create a string variable, store the decrypted text in it, and return the string.
  4. encrypt.h: Contains the function header for the encrypt function. Do not forget your header guard! This file should be included by cipher.cpp.
  5. decrypt.h: Contains the function header for the decrypt function. Do not forget your header guard! This file should be included by cipher.cpp.

Your program should do the following:

  1. Ask the user if they want to encrypt or decrypt some text.
  2. Ask the user for the shift value.
  3. Ask the user for the file they wish to encrypt/decrypt.
  4. Perform the encryption or decryption of the file based on the user's input in 1.
  5. Print the string returned from your encrypt/decrypt function.

Here are some examples of the full run of the program:

Sample Run 1 (sample1-enc.txt)

ranger0$ ./cipher

Would you like to (e)ncrypt or (d)ecrypt a file? e

Enter the shift value: 8

Enter the path to the file: sample1-enc.txt

The encrypted text is:

Twzmu qxacu lwtwz aqb iumb, kwvamkbmbcz ilqxqakqvo mtqb, aml lw mqcauwl bmuxwz qvkqlqlcvb cb tijwzm mb lwtwzm uiovi itqyci. Cb mvqu il uqvqu dmvqiu, ycqa vwabzcl mfmzkqbibqwv cttiukw tijwzqa vqaq cb itqycqx mf mi kwuuwlw kwvamycib. Lcqa icbm qzczm lwtwz qv zmxzmpmvlmzqb qv dwtcxbibm dmtqb maam kqttcu lwtwzm mc ncoqib vctti xizqibcz. Mfkmxbmcz aqvb wkkimkib kcxqlibib vwv xzwqlmvb, acvb qv kctxi ycq wnnqkqi lmamzcvb uwttqb ivqu ql mab tijwzcu.

Sample Run 2 (sample1-dec.txt)

ranger0$ ./cipher

Would you like to (e)ncrypt or (d)ecrypt a file? d

Enter the shift value: 12

Enter the path to the file: sample1-dec.txt

The decrypted text is:

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.

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

N.B: For Decryption I have subtract the shift value, but you can add it also. I have supply decrypted file name which is created from of encrypted text for demo purpose. You can also modify the program according to your requirement.

Compile method

Create all files indivisually first than compile

c++ cipher.cpp encrypt.cpp decrypt.cpp -o cipher

Run

./cipher

Code

cypher.cpp

#include<iostream>
#include<fstream>
#include <string>
#include "encrypt.h"
#include "decrypt.h"
using namespace std;
int main()
{
char ch;
int shift_val;
string word,fname,ret_s1;
  
ifstream file; //File Pointer for reading

cout<<"Would you like to (e)ncrypt or (d)ecrypt a file?";
cin>>ch;
cout<<"Enter the shift value:";
cin>>shift_val;
cout<<"Enter the path to the file:";
cin>>fname;
file.open(fname); //Open the file in Read Mode
if(ch=='e')
{
cout<<"The Encrypted text:"<<endl;
while(file >> word) // To get you all the lines.
{
ret_s1.resize(word.size());//initilize string
ret_s1=encrypt(word, shift_val);
cout<<ret_s1<<" "; // Prints Encrypted text.
}
cout<<endl;
file.close(); //Close the file
}

if(ch=='d')
{
cout<<"The Decrypted text:"<<endl;
while(file >> word) // To get you all the lines.
{
ret_s1.resize(word.size());
ret_s1=decrypt(word, shift_val);
cout<<ret_s1<<" "; // Prints decrypted text.
}
cout<<endl;
file.close(); //Close the file
}

return 0;
}

encrypt.cpp

#include "encrypt.h"
string encrypt(string s1, int shift_val)
{
string ret_s;
ret_s.resize(s1.size());
int i;
for (i = 0; i < s1.size(); i++)
{
ret_s[i]=s1[i]+shift_val;
}
ret_s[i++]='\0';
return ret_s;
}

decrypt.cpp

#include "decrypt.h"
string decrypt(string s1, int shift_val)
{
string ret_s;
ret_s.resize(s1.size());
int i;
for (i = 0; i < s1.size(); i++)
{
ret_s[i]=s1[i]-shift_val;
}
ret_s[i++]='\0';
return ret_s;
}

encrypt.h

#include <string>
using namespace std;
string encrypt(string s1, int shift_val);

decrypt.h

#include <string>
using namespace std;
string decrypt(string s1, int shift_val);

Screen Shots

iteradmin@C126-C072: - File Edit View Search Terminal Help iteradmin@c126-C072:~$ c++ cipher.cpp encrypt.cpp decrypt.CPP iteradmin@c126-C072:~$ C++ cipher.cpp encrypt.cpp decrypt.cpp -o cipher iteradmin@c126-C072:-$ ./cipher Would you like to (encrypt or decrypt a file?e Enter the shift value: 1 Enter the path to the file: source.txt The Encrypted text: Qsblbti Diboesb cipj JUFS- CCTS iteradmin@c126-C072:~$ ./cipher Would you like to (encrypt or (d)ecrypt a file?d Enter the shift value:1 Enter the path to the file:decrypt.txt The Decrypted text: Prakash Chandra Bhoi ITER, BBSR iteradmin@c126-C072:~$ .

#include<iostream> #include<fstream> #include <string> #include "encrypt.h" #include "decrypt.h" using namespace std; int main() char ch; int shift_val; string word, fname, ret_51; ifstream file; //File Pointer for reading cout<<"Would you like to (e)ncrypt or (d)ecrypt a file?"; cin>>ch; cout<<"Enter the shift value:"; cin>>shift_val; cout<<"Enter the path to the file:"; cin>>fname; file.open(fname); //Open the file in Read Mode if(ch=='e') cout<<"The Encrypted text:"<<endl; while(file >> word) // To get you all the lines. ret_s1.resize(word.size());//initilize string ret_s1=encrypt(word, shift_val); cout<<ret_$1<<" "; // Prints Encrypted text. cout<<endl; file.close(); //Close the file

if(ch=='') cout<<"The Decrypted text:"<<endl; while(file >> word) // To get you all the lines. ret_s1.resize(word.size(); ret_s1=decrypt(word, shift_val); cout<<ret_s1<<" "; // Prints decrypted text. cout<<endl; file.close(); //Close the file return 0; C++

Add a comment
Know the answer?
Add Answer to:
Programming Requirements You will write a program consisting of the following files: cipher.cpp: Contains your main...
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
  • Please help me write this Java program. I had posted this question before, but got an...

    Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You! -------------------------------------------------------------------------------------- Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key). You can use brute force to do a for loop through all the 26 shift values, however, your program should only...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • For this code after case 0, I need it to ask the user if they would...

    For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program. import java.util.InputMismatchException; import java.util.*; import java.io.*;...

  • 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...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Write a javascript program which implements the following two classical cryptosystem which we covered in class:...

    Write a javascript program which implements the following two classical cryptosystem which we covered in class: Affine Cipher Vigenere Cipher Your program should consist of at least five functions: Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as...

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

  • C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or...

    C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or decrypting. 2) Ask the user to enter a sentence to be transformed. 3) Ask the user to enter a sentence that will be used as the encryption or decryption key. 4) The sentences (array of characters) should end with a NULL terminator '\0'. 5) The range of values will be all printable characters on the ASCII chart starting with a SPACE - Value 32,...

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