Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help in this in C++ In this assignment, you will write the code for two functions, and demonstrate your functions are correct with drivers.

Need help in this in C++

In this assignment, you will write the code for two functions, and demonstrate your functions are correct with drivers. The first function checks that a password is valid. To be valid, a password must Be at least 8 characters and at most 10 characters Have at least one upper case letter, one lower case letter, one digit and one special character. A special character is one of the following characters: #, $, %, &, !, ?, @. Not contain any character which is not a letter, digit or special character. The second function encrypts an original string of characters into another string of characters. The encryption scheme will be an adapted version of the Caesar cipher. 1. Additional Requirements

a) Functions You are required to implement your program with the following functions. You can implement additional functions, as you see fit, to make your program more modular. isValidPassword: takes as argument a string and returns true if the string is a valid password. Else it returns false. encrypt: takes as arguments a string, an encryption key, encrypts it, and returns the encrypted result, which is a string.

b) Style Make sure you follow the style requirements, especially regarding the comment header for functions, to avoid losing points.

c) Adapted Caesar cipher Caesar cipher is a type of encryption in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with an offset of 3, D would be replaced by A, E would become B, and so on. The offset value is the encryption key. We adapt the scheme to encrypt all characters whose ASCII code is MIN_RANGE (set to 33) to MAX_RANGE (set to 122) inclusive. These include all the alphabet letters, upper case and lower case, and all the digits, but exclude the non printable characters. To encrypt, we convert the ASCII code by adding the offset to the ASCII code. If the converted code is in the MIN_RANGE to MAX_RANGE, the character whose ASCII code is equal to the converted code is the encrypted character. If the converted code is outside the range, we wrap around the value so that it falls within the range. For example, a converted value of 123 would wrap around to 34, a converted value of 124 would wrap around to 35, etc. Offsets can also be negative. The table below illustrates some examples of encrypted characters for various values of the offset. Refer to Appendix B of the textbook for the complete ASCII table. Note that with this scheme, a character that has been encrypted with an offset value = offsetVal can be decrypted by encrypting with an offset value = -offsetVal.

d) Outline of main Your main function will consist of a driver for isValidPassword, and a driver for encrypt. Driver for isValidPassword You are provided with testString, an array of strings. For each string in the array, the program will determine if it is a valid password and print the result. For consistency across the homework submissions, you should copy and paste the following declarations in your submission, and implement the following pseudocode for the driver. // Global const int NUM_STRINGS = 7; // Global named constant: size of testString array const int MIN_LENGTH = 8; // Global named constant: Min length of password const int MAX_LENGTH = 10; // Global named constant: Max length of password // Inside the main function string testString[NUM_STRINGS] = {"12A4&z78", "12A4&z78(", "12A45z78", "12A4$Z78", "a124$z78", "12A4&z7", "12A4&z7890T" };

// Pseudocode of Driver for isValidPassword() Loop over the strings in testString array. For each element of the array, call isValidPassword Display the string and the result (Valid or Not valid) returned by isValidPassword Driver for encrypt The driver will implement nested loops over various values of encryption keys and over the strings in testString array. For each value of the key and for each string, the driver calls encrypt, then prints the result. Then for verification, the function decrypts the result (by encrypting with the negated offset) and displays the result. Normally the decrypted result should be the same as the original string. For consistency across the homework submissions, you should copy and paste the following declarations in your submission and implement the following pesudocode for the driver. // Global const int MIN_RANGE = 33; // Global named constant: Low end of ASCII value range const int MAX_RANGE = 122; // Global named constant: High end of ASCII value range // Inside the main function int key[] = {0, MAX_RANGE-MIN_RANGE+1, 1, 5, MAX_RANGE-MIN_RANGE+6, -1, MAX_RANGE-MIN_RANGE, -5, -MAX_RANGE+MIN_RANGE-6}; // Keys used for encryption int numKeys = sizeof(key)/sizeof(int); // Calculate size of key array // Pseudocode of Driver for encrypt Loop over the keys in the key array Display the key value Loop over the strings str in the testString array Let encrypted be the string returned by encrypt(str, key); Let decrypted be the string returned by encrypt(encrypted, -key) Display str, encrypted, decrypted 2. Suggestions for Implementation You are allowed to use existing library functions in chapter 10 of the textbook. a) isValidPassword To test if a string contains at least one upper case letter, lower case letter or digit, you can use isupper, islower, etc. In addition, to test that a string contains at least one special character, you can define an array of special characters and define a function called isspecial that you can build upon. You may define additional functions, as you see fit. char special[] = {'#', '$', '%', '&', '!', '?', '@'}; // Special characters int numSpecial = sizeof(special)/sizeof(char); // Calculate the size of special array /* This function takes as arguments a character c, an array of special characters, along with its size, and returns true if c is equal to an element of the array, false otherwise */ bool isspecial(char c, char spec[], int siz); With this implementation approach, the isValidPassword prototype would look like /* This function takes as arguments a string, an array of special characters, along with its size, and returns true if the string is a valid password, false otherwise */ bool isValidPassword(string pwd, char spec[], int siz); The driver would look like // Driver for isValidPassword() for (int i = 0; i < NUM_STRINGS; i ++) cout << setw(14) << testString[i] << (isValidPassword(testString[i], special, numSpecial)? " is valid": " is not valid") << endl; b) encrypt function You can implement the following function called convert which encrypts a single character, and build upon it to encrypt a string. /* This function takes as arguments a character c and an offset and encrypts the character c according to the adapted Caesar cipher */ char convert(char c, int offset) { int offsettedAscii = (static_cast(c)+ offset); // This holds the converted ASCII code int adjustedAscii; // This holds the converted ASCII, after adjustment (wrap around), if any if (offsettedAscii > MAX_RANGE) adjustedAscii = (offsettedAscii - MAX_RANGE - 1) % (MAX_RANGE - MIN_RANGE + 1) + MIN_RANGE ; else if (offsettedAscii < MIN_RANGE) adjustedAscii = MAX_RANGE - (MIN_RANGE - offsettedAscii - 1) % (MAX_RANGE - MIN_RANGE + 1); else adjustedAscii = offsettedAscii; return static_cast (adjustedAscii); } // Driver for encrypt string encrypted; // Holds the encrypted string for (int i = 0; i < numKeys; i++) { cout << " Key is " << key[i] << endl; for (int j = 0; j < NUM_STRINGS; j++) { encrypted = encrypt(testString[j], key[i]); cout << "Orig: " << setw(14) << testString[j] << " Encrypted: " << setw(14) << encrypted << " Decrypted: " << setw(14) << encrypt(encrypted, key[i]) << endl; } }

3. Expected Output This is an example of output if your implementation is correct.

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

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions