Question: Lab: Cryptography - Caesar Cipher In this lab, you will create a program to encrypt a secret message using a special variation of Caesar cipher.
Lab: Cryptography - Caesar Cipher
In this lab, you will create a program to encrypt a secret message using a special variation of Caesar cipher.
Program Specifications:
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.
Your program should perform a substring swap where the latter half of the Caesar cipher string is placed before the former half of the string. Your program should then 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.
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.
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 Only use Java Scanner Import No Specific Functions
-----------------------------------------------------------
----- Sample Code -----
import java.util.Scanner;
public class Cipher {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); {
// Loop until user quits
// Ask for key value
// Validate key value and handle errors properly
// Ask for plaintext
// Loop through plaintext and perform Caesar cipher using key
// Print Caesar cipher result
// Perform the substring shift and print result
scnr.close();
}
}
}
----------------------------------
Thanks in advance for any help!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
