Question

Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

Computer Science C++ Help, here's the question that needs to be answered (TASK D):

Task D. Decryption

Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext:

decryptCaesar(ciphertext, rshift) == plaintext
decryptVigenere(ciphertext, keyword) == plaintext

Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers.

It should first ask the user to input plaintext, then ask for a right shift for the Caesar cipher and report the ciphertext and its subsequent decryption. After that, it should do the same for the Vigenere cipher.

Example:

$ ./decryption

Enter plaintext: Hello, World!

= Caesar =
Enter shift    : 10
Ciphertext     : Rovvy, Gybvn!
Decrypted      : Hello, World!

= Vigenere =
Enter keyword  : cake
Ciphertext     : Jevpq, Wyvnd!
Decrypted      : Hello, World!

(When reporting decrypted strings, they should be the result of applying decryption functions to the ciphertext, not the original plaintext variable.)

This question piggybacks off of two previous tasks (Tasks B & C). The following shows the questions for both Task B and C (NOTE: These are only shown to provide background info and continue the code using the variables/functions that were used in this code), I'm also including the code I've written for both these tasks, which is correct, only task D needs to be answered.

Question & Code for TASK B:

Task B. Implementing Caesar cipher encryption

Write a program caesar.cpp with functions implementing Caesar cipher encryption:

// A helper function to shift one character by rshift
char shiftChar(char c, int rshift);

// Caesar cipher encryption
string encryptCaesar(string plaintext, int rshift);

The argument rshift is the magnitude of the right shift. For the sake of the lab, you may assume it to be in the range 0 ≤ rshift ≤ 25 (although, implementing it correctly for an arbitrary integer shift is also possible, of course).

Your functions should preserve case, and any non-alphabetic characters should be left unchanged. For example,

encryptCaesar("Way to Go!", 5) == "Bfd yt Lt!"

Feel free to write more additional helper functions when you need them.

The main function should ask the user to input a plaintext sentence, then enter the right shift, and report the ciphertext computed using your encryption function.

Code for Task B:


#include <iostream>
#include <string>

using namespace std;

char shiftChar(char character, int rShift);
string encryptCaesar(string textPlain, int rShift);

int main()
{
   string userInput;
cout << "Enter plaintext: ";
  
getline(cin, userInput);
  
   int encryptionKey;
  
cout << "Enter shift : ";
cin >> encryptionKey;
  
cout << "Ciphertext : " << encryptCaesar(userInput, encryptionKey) << endl;
       /*
           This piece of int main will ask for user input, store the user input, create
           an ecryption key and encrypt it using Caesar.
       */
}

char shiftChar(char character, int rShift) {
if(character >= 'A' && character <= 'Z')
{
character = 'A' + (character - 'A' + rShift + 26) %26;
}
if(character >= 'a' && character <= 'z')
{
character = 'a' + (character-'a' + rShift + 26) %26;
}
return character;
/*
       This piece of code will implement the Caesar shift, it will push characters within
       a certain boundary to the next character in line.
*/
}

string encryptCaesar(string userInput, int rShift) {
string endResult = "";
char character;
for(int i = 0; i < userInput.length(); ++i)
{
endResult += shiftChar(userInput[i], rShift);
}
return endResult;
}

Question for Task C:

Write a program vigenere.cpp. It should contain a function encryptVigenere implementing this cipher:

string encryptVigenere(string plaintext, string keyword);

You may assume that the keyword contains only lowercase alphabetic characters a - z.

The main should implement a testing interface similar to the one in Task B, the user enters the plaintext and the keyword, and the program reports the ciphertext.

Example:

$ ./vigenere

Enter plaintext: Hello, World!
Enter keyword  : cake
Ciphertext     : Jevpq, Wyvnd!

Code for Task C:


#include <iostream>
#include <string>

using namespace std;

string encryptVigenere(string textPlain, string encryptionKey);

int main()
{
   string textPlain;
cout << "Enter plaintext: ";
  
getline(cin, textPlain);
  
string encryptionKey;
  
cout << "Enter keyword: ";
cin >> encryptionKey;
  
cout << "Ciphertext: " << encryptVigenere(textPlain, encryptionKey) << endl;   
  
   /*
       This part of int main function will get user input, it will store our values and
       create an ecryption key to later decipher.
   */
}

string encryptVigenere(string textPlain, string keyword)
{
string endResult = "";

char character;
int ind = 0, encryptionKey;

//Starting our nested if loops inside a for loop.
for(int k = 0; k < textPlain.length(); ++k)
{
character = textPlain[k];
     
if(character >= 'A' && character <= 'Z')
   {
encryptionKey = keyword[ind] - 'a';
ind = (ind + 1) % keyword.length();
character = 'A' + (character - 'A' + encryptionKey + 26) %26;
}
if(character >= 'a' && character <= 'z')
   {
encryptionKey = keyword[ind] - 'a';
ind = (ind + 1) % keyword.length();
character = 'a' + (character - 'a' + encryptionKey + 26) %26;
}
endResult += character;
}
return endResult;
/*
       This wall of text is the code that actually implements the encryption, it takes
       characters within a certain boundary and then pushes them however many ASCII
       characters necessary.
*/
}

Once again, please only answer Task D, and please use the strings/variables/functions that were used in Tasks B & C so that the code lines up. Including comments so that I can follow along with would be greatly appreciated. Thank you for the help!

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

If you like the solution please give it a thumbs up!

Solution :-

C++ code :- I have tried to use most of your existing code and modify it accordingly.

#include <iostream>
#include <string>

using namespace std;

// shift key function used for encryption
char shiftChar(char character, int rShift)
{
if(character >= 'A' && character <= 'Z')
{
character = 'A' + (character - 'A' + rShift + 26) %26;
}
if(character >= 'a' && character <= 'z')
{
character = 'a' + (character-'a' + rShift + 26) %26;
}

return character;

}

// shift key function used for decryption
// instead of adding rshift subtract it for the decryption
char shiftCharDecrypt(char character, int rShift)
{
if(character >= 'A' && character <= 'Z')
{
character = 'A' + (character - 'A' - rShift + 26) %26;
}
if(character >= 'a' && character <= 'z')
{
character = 'a' + (character-'a' - rShift + 26) %26;
}
return character;
}

string encryptCaesar(string userInput, int rShift)
{
string endResult = "";
char character;
for(int i = 0; i < userInput.length(); ++i)
{
endResult += shiftChar(userInput[i], rShift);
}
return endResult;
}


// decryption part is almost same with little change in shift key function
string decryptCaesar(string cipherText, int rShift)
{
string endResult = "";
char character;
for(int i = 0; i < cipherText.length(); ++i)
{
endResult += shiftCharDecrypt(cipherText[i], rShift);
}
return endResult;
}

// Vigenere encryption
string encryptVigenere(string textPlain, string keyword)
{
string endResult = "";

char character;
int ind = 0, encryptionKey;

//Starting our nested if loops inside a for loop.
for(int k = 0; k < textPlain.length(); ++k)
{
character = textPlain[k];
  
if(character >= 'A' && character <= 'Z')
{
encryptionKey = keyword[ind] - 'a';
ind = (ind + 1) % keyword.length();
character = 'A' + (character - 'A' + encryptionKey + 26) %26;
}
if(character >= 'a' && character <= 'z')
{
encryptionKey = keyword[ind] - 'a';
ind = (ind + 1) % keyword.length();
character = 'a' + (character - 'a' + encryptionKey + 26) %26;
}
endResult += character;
}
return endResult;

}

// Vigenere decryption, instead of adding keyword subtract it
// almost same as encryption
string decryptVigenere(string cipherText, string keyword)
{
string endResult = "";

char character;
int ind = 0, decryptionKey;

for(int k = 0; k < cipherText.length(); ++k)
{
character = cipherText[k];
  
if(character >= 'A' && character <= 'Z')
{
decryptionKey = keyword[ind] - 'a';
ind = (ind + 1) % keyword.length();
character = 'A' + (character - 'A' - decryptionKey + 26) %26;
}
if(character >= 'a' && character <= 'z')
{
decryptionKey = keyword[ind] - 'a';
ind = (ind + 1) % keyword.length();
character = 'a' + (character - 'a' - decryptionKey + 26) %26;
}
endResult += character;
}
return endResult;
  
}
int main()
{
string plainText;
cout << "Enter plaintext: ";
  
getline(cin, plainText);

string choice;
cout<<"choose from below to proceed :-\n";
cout<<"caesar : to apply caesar cipher encryption-decryption\n";
cout<<"vigenere : to apply vigenere algo encryption-decryption\n";

cin>>choice;

if(choice == "caesar")
{
int encryptionKey;
  
cout << "Enter shift : ";
cin >> encryptionKey;
  
cout << "Ciphertext : " << encryptCaesar(plainText, encryptionKey) << endl;
cout<<"Decrypted :- "<<decryptCaesar(encryptCaesar(plainText,encryptionKey), encryptionKey) << endl;
}
else
{
string encryptionKey;
  
cout << "Enter keyword: ";
cin >> encryptionKey;
  
cout << "Ciphertext: " << encryptVigenere(plainText, encryptionKey) << endl;   

cout<<"Decrypted :- "<<decryptVigenere(encryptVigenere(plainText,encryptionKey), encryptionKey) << endl;
}
  
  
}

Add a comment
Know the answer?
Add Answer to:
Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....
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
  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

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

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • Needs to be done in C, The function shown can be called to reverse the char....

    Needs to be done in C, The function shown can be called to reverse the char. 7.5 Bit Encryption 7.5.1 Problem Given a single character, apply a simple bitwise encryption algorithm and return the cipher character. 7.5.2 Preconditions You must provide a series of functions which meet the requirements in the table below. You you may include other functions as long as the requested functions execute correctly. Do not include a main function in your source or header files. You...

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

  • Creating a Vigenère Cipher Encryption/Decryption in Assembly Language NASM/YASM With these instructions so far we created...

    Creating a Vigenère Cipher Encryption/Decryption in Assembly Language NASM/YASM With these instructions so far we created some variables for the data and the outputs for them but are very confused on how to integrate the cipher portion so help would be appreciated. As seen in the picture below we are supposed to use a Vigenere cipher which uses key and plaintext variables in nasm/yasm assembly. The key is extended to the size of the text and then for each letter...

  • JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information...

    JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information being exposed, it is becoming more and more important to find ways to protect our sensitive data. In this program we will develop a simple tool that helps users generate strong passwords, encrypt and decrypt data using some cyphering techniques. You will need to create two classes. The first class is your driver for the application and contains the main method. Name this class...

  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n...

    Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...

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