Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! I got a task in C++ I need help with: The program first asks for the encryption key with which to encrypt the text

Hello! I got a task in C++ I need help with:

The program first asks for the encryption key with which to encrypt the text with the prompt: "Enter the Encryption key: " and then reads the entered string as the encryption key.

Next, the program checks the validity of the entered string as an encryption key. The following checks are performed on the string in the following order:

The string must be 26 characters long. If not, an error message is printed: "Error! The Encryption key must contain 26 characters." The character string may only contain English lowercase letters, i.e. the characters 'a' - 'z'. If the string contains something else, an error message is printed: "Error! The Encryption key must contain only lower case characters." The string must contain all English lowercase letters. If not, an error message is printed: "Error! The Encryption key must contain all alphabets a-z." The execution of the program ends with the return value EXIT_FAILURE immediately after printing the error message, i.e. the program can print only one of the error messages in one run.

When the program has read and checked the key, it asks the user for the word that the user wants to encrypt with the prompt: "Enter the text to be encrypted: " and then reads one string as the text to be encrypted.

The character string to be encrypted is checked to ensure that it contains only English lowercase letters. If the string contains something else, an error message is printed: "Error! The text to be encrypted must contain only lower case characters." and program execution is terminated with the return value EXIT_FAILURE.

The program encrypts the letters using a simple replacement method, the replacements for the letters are obtained from the encryption key. The encryption key is interpreted in such a way that its first character corresponds to the a of the text to be encrypted, the second to b and the last, i.e. the 26th character, corresponds to the letter z.

For example, the text encrypted with the encryption key abcdefghijklmnopqrstuvwxyz looks exactly the same as the original, because the letter a is replaced by the letter a, b is replaced by b, etc. With the encryption key abcdefghijklmnopqrstuvwzyx, the encryption would change all x letters to z letters and z letters to x letters without changing the other letters.

There was another question like this but I couldn't get the answer provided to work so I am hoping you'd help me out here. Thank you!

As you can see this does not work:

#include

#include

#include

using namespace std;

bool isValidKey(const string &key) {

if (key.length() != 26) {

cout

return false;

}

for (char c : key) {

if (!islower(c)) {

cout

return false;

}

}

mapchar, int> charCount;

for (char c : key) {

charCount[c]++;

}

for (char c = 'a'; c

if (charCount[c] != 1) {

cout

return false;

}

}

return true;

}

bool isValidText(const string &text) {

for (char c : text) {

if (!islower(c)) {

cout

return false;

}

}

return true;

}

string encrypt(const string &text, const string &key) {

string encryptedText;

for (char c : text) {

encryptedText += key[c - 'a'];

}

return encryptedText;

}

int main() {

string key;

cout ​`oaicite:{"index":0,"invalid_reason":"Malformed citation >"}`​ key;

if (!isValidKey(key)) {

return EXIT_FAILURE;

}

string text;

cout ​`oaicite:{"index":1,"invalid_reason":"Malformed citation >"}`​ text;

if (!isValidText(text)) {

return EXIT_FAILURE;

}

string encryptedText = encrypt(text, key);

cout

return 0;

}

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions