CS52 Programming Assignment 1 Solved

45.00 $

Category:

Description

5/5 - (1 vote)

C++ Cryptography, Integrated Development Environments (IDEs), Compilers, and Canvas
Part 1 (50 points) Cryptography: A C++ Shift Cipher:
In part 1 you will write a program to encrypt and decrypt a plaintext (p) string of lower-case letters and digits that contain no spaces or other characters (e.g. @#$,etc.). Your program will accept command line arguments to allow the user to enter in a key value k, a plaintext string of characters just described (−p) or a ciphertext (−C) and exactly one of the options to encrypt (−E) or decrypt (−D). Refer to the example usage on page 2 for how to run the program using the command line arguments and the skeleton code on page 3 for a general outline of the code.
Program Description and Functionality:Provide the following in your source file:
1. Comments at the top of your .cpp file.
2. A main function with command line arguments main(int argc, char* argv[])
3. Use only C++ headers that place all standard-library routines in namespace std. For standard input and output use #include to access std::cout;std::cin and #include for C-style string functions like std::strcmp and std::strlen and if needed #include for helper functions like std::atoi, etc.
4. The use of command line arguments (int argc, char* argv[]) to get the plaintext (-p sometext) or cipher text (-C zvtl0l40), the shift amount −k 7 and the options to encrypt (-E) or decrypt (-D).
5. The program only needs to work for lowercase letters and digits 0-9. Although you can modify the provided array and equations to work for a larger character set:
char alphabet_digits[]
= {’a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’, ’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’,
’0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’};
6. To encrypt a plaintext string use the following formula to compute an index i into the alphabet_digits array:
Ci = E(p) = (p + k) mod 36,
where mod is the modulus operator %, C is the ciphertext, E is the encryption function, p is the index of the plaintext character, k is the key shift, and 36 is the length of the alphabet_digits array.
7. To decrypt a ciphertext string use the following formula to compute an index i into the alphabet_digits array:
pi = D(C) = (C − k) mod 36, (C − k) >= 0 pi = D(C) = (((C − k) mod 36)+36) mod 36, (C − k) < 0 where mod is the modulus operator %, C is the index of the ciphertext character, D is the decryption function, p is the plaintext, k is the key shift, and 36 is the length of the alphabet_digits array. In C & C++, mod, or remainder of division, is the % symbol. For negative C − k numbers refer to the references on page 4 for more details about the modulus operator in C++ with negative numbers. Example usage: >A01.exe -p abc -k 1 -E The ciphertext is : bcd enter any key to exit
>A01.exe -C bcd -k 1 -D The plaintext is : abc enter any key to exit
>A01.exe -p 789 -k 3 -E The ciphertext is : abc enter any key to exit
>A01.exe -C abc -k 3 -D The plaintext is : 789 enter any key to exit
>A01.exe -p sometext -k 7 -E The ciphertext is : zvtl0l40 enter any key to exit
>A01.exe -C zvtl0l40 -k 7 -D The plaintext is : sometext enter any key to exit
>A01.exe -p th1s1sv3ryc00l -k 9 -E The ciphertext is : 2qa1a14c07l99u enter any key to exit
>A01.exe -C 2qa1a14c07l99u -k 9 -D The plaintext is : th1s1sv3ryc00l enter any key to exit
Skeleton code
//
// Name: Your First Name & Last Name
// SSID: Student ID Number
// Assignment #: 1
// Submission Date: 9/20/2020
//
// Description: A program to encrypt and decrypt a message using a shift cipher
// The plaintext message must only contain lowercase alpha and digits 0-9
//
// command line arguments
// -p theplaintextmessage1 – the plaintext (p) message to be encrypted
// -C 2qnyujrw2n62vn11jpna – the cipher (C) text to be decrypted
// -k 9 – the key (k) shift value
// -E – the option to encrypt (E)
// -D – the option to decrypt (D)
//
#include //std::cout, std::cin, etc.
#include //C++ version of stdlib.h
#include //C++ version of string.h
//todo: add other includes you may need
//Command line args reference:
//https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=vs-2019 int main(int argc, char *argv[])
{
// todo: return if not enough input
// todo: declare variables for A01, an example below:
int index_of_plaintext = 0; //argv index of plaintext int index_of_ciphertext = 0; //argv index of ciphertext bool encrypt = false; bool decrypt = false; int k = 0;
// todo: process the command line arguments using a loop
for (int i = 1; i < argc; i++){
// use strcmp to compare command line switches to argv[i]
// ….your code goes here
}
// todo: -E encrypt the plaintext with key value k
if(encrypt){
// use a nested loop to search and replace plaintext with shifted text // ….your code goes here
}
// todo: -D decrypt the ciphertext -C with key value k
if(decrypt){
// use a nested loop to search and replace ciphertext with plaintext // ….your code goes here
}
// todo: display the ciphertext or plaintext
return 0; }//main
Where to do the assignment
You can do this assignment on your own computer, or using the SMC Virtual labs with Citrix. In either case, ensure the code compiles and runs on Windows. Submit one .cpp file named A01.cpp. Do not use any other name or else points will be deducted.
Submitting the Assignment
Include your name, your student id, the assignment number, the submission date, and a program description in comments at the top of your files, and use the CS52 Programming Guide for coding style guidance. Submit the assignment on Canvas (https://online.smc.edu) by uploading your .cpp file to the Assignment 1 entry as an attachment. Do not cut-and-paste your program into a text window. Do not hand in a screenshot of your program’s output. Do not hand in a text file containing the output of your program.
Saving your work
Save your work often on a flash-drive or to the cloud (e.g., GoogleDrive, Microsoft OneDrive, Canvas, etc.). Always save a personal copy of your files (e.g. .cpp, .h, etc.). Do not store files on the virtual lab computers.
References:
Microsoft C++ Language Reference
ISO C++ Standard Library and other refs
’The modulus operator yields the remainder given by the following expression, where e is the first operand and e2 is the second: e1 − (e1/e2) ∗ e2, where both operands are of integral types.’ Microsoft C++ Modulus & Multiplication Operators
Stackexchange thread on Mod

  • A01-6399dt.zip