Question: Lab 04: Cryptography - Caesar Cipher Lab by: Brady Jacobs In this lab, you will create a program to encrypt a secret message using a

Lab 04: Cryptography - Caesar Cipher

Lab by: Brady Jacobs

In this lab, you will create a program to encrypt a secret message using a special variation of Caesar cipher

Program Specification:

Implement your program using the template code from Canvas under Lab04. Make sure to extract the code before transferring it to VS Code.

Your program should prompt users for a key to encode the message. This key is a non-negative integer. If a user enters input that is not an integer, the program should print an error message and exit to avoid throwing an exception. If a user enters a negative integer, print an error message and loop until the user enters valid input.

Your user will then prompt the user for an input message to encode.

Do not assume that k will be less than or equal to 26. Your program should work for all non-negative integer values. Do not worry if a user tries to enter an abnormally large number that would overflow an int in Java. We wont be trying to break your code.

For instance, if k is 27, A should not become [ even though A + 27 is [ in ASCII. Instead, A should become B since B is 27 places away from A since Z wraps back around to A.

Your program should preserve the case: capitalized letters should stay capitalized, and lowercase letters should remain lowercase.

Your program should preserve punctuation and whitespace, meaning non-alphabetical characters should be unchanged.

Your program should output the Caeser ciphertext and then perform the substring swap and print the final ciphertext.

Your program should loop to ask the user for new plaintext to encrypt with the same key after the first iteration of the loop.

Accessing the Key

Prompt the user to enter the key they would like to encrypt with. Make sure you print the error statements and loop adequately based on the input from the user.

Caesar Cipher

It is pretty easy for us to understand that A + 1 = B, but how can we communicate that same idea in Java code?

Lets start by ignoring the key for now and just try to take in a message to be encrypted from the Scanner object and shift each character by one and print that as the Caesar ciphertext. The main idea here is to learn how to loop through a string and access each character and apply the desired shift of 1.

After this, lets apply the last part to our special encryption process: take the second half of the cipher and move it to the front using substring in Java.

Putting it All Together

If you have done all the previous steps, you will have a good understanding of where to go next. The last part is putting together the Caesar Cipher algorithm and adding a loop to ask for further messages to be encrypted.

Remember to have clear comments, a clearly defined header, and well-formatted code.

Make sure your output matches how the test case output is formatted.

TA Tips:

Best to use the modulo (i.e., remainder) operator, %, to handle the wraparound from Z to A. Recall that ASCII maps all printable characters to corresponding numbers and that A = 65 while a = 97. Feel free to look up the ASCII table to plan your code accurately.

Here are some sample interactions with the program. The users input is in blue:

Example 1:

Key Value: xyz

Usage: Please enter a valid non-negative integer

Example 2:

Key Value: -1

Usage: Try again, please enter a valid non-negative integer

Key Value: -3

Usage: Try again, please enter a valid non-negative integer

Key Value: 13

String to encode with key 13: hello, world

Caesar Cipher: uryyb, jbeyq

Ciphertext: jbeyquryyb,

Type q to quit, any other to continue: q

Example 3:

Key Value: 100

String to encode with key 100: Hello, World

Caesar Cipher: Dahhk, Sknhz

Ciphertext: SknhzDahhk,

Type q to quit, any other to continue: y

Key Value: 1

String to encode with key 13: HELLO

Caesar Cipher: IFMMP

Ciphertext: MMPIF

Type q to quit, any other to continue: q ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- CODE IN JAVASCRIPT

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!