Question: Please help me to solve Q3 thanks! using C++ Problem 3: Shift Cipher Write a C++ program which encrypts and decrypts a sequence of input
Please help me to solve Q3 thanks! using C++





Problem 3: Shift Cipher Write a C++ program which encrypts and decrypts a sequence of input characters using an algorithm adapted from the Vignre cipher as described below. We first consider the algorithm to encrypt/decrypt a single character: To encrypt (decrypt) a letter c (within the alphabet A-Z or a-z) with a shift of k positions: 1. Let x be c's position in the alphabet (O based), e.g., position of B is 1 and position of g is 6. 2. For encryption, calculate y = x + k modulo 26; for decryption, calculate y = x - k modulo 26. 3. Let w be the letter corresponding to position y in the alphabet. If c is in uppercase, the encrypted (decrypted) letter is w in lowercase; otherwise, the encrypted (decrypted) letter is w in uppercase. A character which is not within the alphabet A-Z or a-z will remain unchanged under encryption or decryption. Example. Given letter B and k = 3, we have x = 1, y = 1 + 3 mod 26 = 4, and w = E. As B is in uppercase, the encrypted letter is e. Now, to encrypt/decrypt a sequence of characters: The number of positions, k, used to shift a character is determined by a key V of n characters. For example, if V is a 4- character key 'C', 'O', 'M', 'P', and their positions in the alphabet is 2, 14, 12 and 15, respectively. To encrypt a sequence of characters, we shift the first character by +2 positions, the second by +14, the third by +12, the fourth by +15 and repeat the key, i.e., we shift the fifth character by +2, the sixth by +14, until we encrypt all the characters in the input sequence. Example. Consider the input sequence of characters 'H', 'e','1','1','','W', 'o', 'r','1','d' and a 4-character key 'C','O','M','P': character H e 1 I o W o r 1 d k +2 +14 +12 + 15 +2 +14 +12 + 15 +2 +14 X 7 4 11 11 14 22 14 17 11 3 y 9 18 23 0 16 10 0 6 17 13 n w j S a q k a g enrypted character j S A k A G N R Note: For decryption, we will shift by-k instead of k. Input: first line of input s ci C2 C3 ..., where o s is either the character e for encryption, or the character d for decryption o CI C2 C3 ... is a sequence of space separated characters, ended by ! , to be encrypted or decrypted o you may assume that the input number of characters to encrypt or decrypt (including ! ) is no greater than 50. second line of input n vi U2 ... Un, where n is the number of characters in the key, and V1, V2,...,Un are the characters in the key. o you may assume that all characters in the key is in uppercase and the number of characters in the key is at least one and no greater than 10. Output: the encrypted/decrypted message ended by !. There should be no space between two consecutive characters. Requirement: You are provided with a template program 3.cpp . Read the code in the template carefully to see what have been provided for you. You can ONLY use the simple data types char, bool, int, double and arrays. In other words, you are not allowed to use other data types or data structures such as strings or STL containers (e.g., vectors), etc. Sample Test Cases User inputs are shown in blue. 3_1 3 3 ABC ! 3_2 e a BCDe ! 2 X Y XzZbB! 3_3 d X z ZOB! 2 X Y aBcDe! 3_4 e Hello ENGG1 3 4 0 / COMP 2 1 13 ! 4 COMP jSXAQs zvi1340/odod2113! 3_5 df 30 3B 3 I POR 3-QK_X3 DIJK 3 V - WBE 3! 9 CODE IS FUN D313t3d_cod3_is_d3bugg3d_cod3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
