Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be writing a simple Java program that implements ROT-13 encryption. ROT-13 is a very simple encryption scheme that can be used to obfuscate

You will be writing a simple Java program that implements ROT-13 encryption. ROT-13 is a very simple encryption scheme that can be used to obfuscate text and is based on a very old encryption technique called a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: 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 Shifted alphabet: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C Punctuation, numbers, and other non-alphabetic characters are unaffected by the shift. So the message "EAT AT JOE'S AT 1:00" becomes "HDW DW MRH'V 1:00". ROT-13 is a special case of the Caesar cipher where the characters of the new alphabet are shifted (or "rotated") by 13 positions from the original alphabet: Original alphabet: 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 ROT-13 alphabet: N O P Q R S T U V W X Y Z A B C D E F G H I J K L M So in the case of ROT-13, the message "EAT AT JOE'S AT 1:00" becomes "RNG NG WBR'F NG 1:00". Unlike the general case of the Caesar cipher, ROT-13 has a special property that an encrypted message can be decrypted by applying ROT-13 a second time. So the message "RNG NG WBR'F NG 1:00" becomes "EAT AT JOE'S AT 1:00" when passed through a ROT-13 encryption. You will be writing a Java program that takes an arbitrary string as input, applys ROT13 to the string, and displays the results in a specific format. The code must work for uppercase and lowercase letters and must work correctly if punctuation and numbers are included in the message. A "skeleton" of this code is provided in the file given in the instructions above. You will see that there are three methods declared in this file with no code provided. You must fill in the appropriate code. Pay close attention to what each method should be doing based on the information given in the comments before the method. You may add more methods to this skeleton if you feel that it would be useful, but you MUST include code for all of the methods in the skeleton to receive full credit.

This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program. Enter your text to encrypt (!!! to quit): The quick brown fox jumped over the lazy dog. +----------------------------------------------------------+ | ORIGINAL: The quick brown fox jumped over the lazy dog. | | ENCRYPTED: Gur dhvpx oebja sbk whzcrq bire gur ynml qbt. | +----------------------------------------------------------+ Enter your text to encrypt (!!! to quit): Gur dhvpx oebja sbk whzcrq bire gur ynml qbt. +----------------------------------------------------------+ | ORIGINAL: Gur dhvpx oebja sbk whzcrq bire gur ynml qbt. | | ENCRYPTED: The quick brown fox jumped over the lazy dog. | +----------------------------------------------------------+ Enter your text to encrypt (!!! to quit): EAT AT JOE'S AT 1:00PM +-----------------------------------+ | ORIGINAL: EAT AT JOE'S AT 1:00PM | | ENCRYPTED: RNG NG WBR'F NG 1:00CZ | +-----------------------------------+ Enter your text to encrypt (!!! to quit): 1234567890 +-----------------------+ | ORIGINAL: 1234567890 | | ENCRYPTED: 1234567890 | +-----------------------+ Enter your text to encrypt (!!! to quit): ERROR! String must not be empty! Enter your text to encrypt (!!! to quit): !!! Goodbye! The program should continue executing until the user provides exactly three exclamation points as input. If those exclamation points include any other text, the program should function normally. (HINT:Use the String equals() method to test whether the input string is "!!!"): Enter your text to encrypt (!!! to quit): !!! Hello World! +-----------------------------+ | ORIGINAL: !!! Hello World! | | ENCRYPTED: !!! Uryyb Jbeyq! | +-----------------------------+ Enter your text to encrypt (!!! to quit): YAY!!! +-------------------+ | ORIGINAL: YAY!!! | | ENCRYPTED: LNL!!! | +-------------------+ Enter your text to encrypt (!!! to quit): !!! Goodbye! NOTE: The formatting here in important - your output should be contained within a box that changes size based on the length of your input and output strings. Take this code in stages - first get the ROT-13 encryption working properly, then try to get the output in the correct format. NOTE 2: This lab requires you to change characters based on changing individual characters. While there are many ways to do this, some will be easier than others. One way to do this easily is to take advantage of the fact that every character is represented in Java by a numeric value and the characters are numbered in sequence. The character 'A' has a numeric value of 65, 'B' has a numeric value of 66, 'C' has a value of 67, and so on up to 'Z' which has a value of 90. Lowercase letters start with 'a' with a numeric value of 97 and end with 'z' with a numeric value of 122. (This encoding is known as Unicode, and it is a standard used across the industry for representing characters on computing devices). Since each character is represented as a numeric value, we can manipulate the characters using simple math. For example, this code snippet should display the letter 'B': char myA = 'A'; char newChar = (char) (myA + 1); System.out.println(newChar); Note that you cannot just add 13 to every character to get the ROT-13 value - adding 13 to the numeric value for the letter 'Z' gives you a numeric value of 103, which translates to the character 'g'. You will need to implement the logic to allow your characters to "wrap around" yourself. There are many different ways you can approach this.

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab08b.java in the same ClosedLab08 folder. For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header: private static String shiftMessage(String input, int n) It should take a String as input and return the Caesar cipher of that input string with a shift of n. For example, calling shiftMessage("EAT AT JOE'S",3) should result in the string "HDW DW MRH'V". Then modify your main program so that after it accepts a String from the user as input, it also asks for the shift to use to encode the string and reports the proper encryption of that string. You may add more methods if you would like, but you must implement at least the shiftMessage method. (You may also want to implement a method to prompt the user for a shift value that is similar to the method used

Exercise 2 Sample Output

This is a sample transcript of what your program should do. Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 13 +---------------------------------------+ | ORIGINAL: ABCDEFGHIJKLMNOPQRSTUVWXYZ | | ENCRYPTED: NOPQRSTUVWXYZABCDEFGHIJKLM | +---------------------------------------+ Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 3 +---------------------------------------+ | ORIGINAL: ABCDEFGHIJKLMNOPQRSTUVWXYZ | | ENCRYPTED: DEFGHIJKLMNOPQRSTUVWXYZABC | +---------------------------------------+ Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz Enter amount to shift text by: 13 +---------------------------------------+ | ORIGINAL: abcdefghijklmnopqrstuvwxyz | | ENCRYPTED: nopqrstuvwxyzabcdefghijklm | +---------------------------------------+ Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz Enter amount to shift text by: 3 +---------------------------------------+ | ORIGINAL: abcdefghijklmnopqrstuvwxyz | | ENCRYPTED: defghijklmnopqrstuvwxyzabc | +---------------------------------------+ Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz Enter amount to shift text by: 0 +---------------------------------------+ | ORIGINAL: abcdefghijklmnopqrstuvwxyz | | ENCRYPTED: abcdefghijklmnopqrstuvwxyz | +---------------------------------------+ Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz Enter amount to shift text by: 6 +---------------------------------------+ | ORIGINAL: abcdefghijklmnopqrstuvwxyz | | ENCRYPTED: ghijklmnopqrstuvwxyzabcdef | +---------------------------------------+ Enter your text to encrypt (!!! to quit): !!! Goodbye! The user MUST provide a shift value between 0 and 26. If the user goes out of this range, the program should force the user to enter a valid value: Enter your text to encrypt (!!! to quit): the quick brown fox jumped over the lazy dog Enter amount to shift text by: -3 ERROR! Shift must be non-negative! Enter amount to shift text by: 33 ERROR! Shift must be no more than 26! Enter amount to shift text by: 25 +---------------------------------------------------------+ | ORIGINAL: the quick brown fox jumped over the lazy dog | | ENCRYPTED: sgd pthbj aqnvm enw itlodc nudq sgd kzyx cnf | +---------------------------------------------------------+ Enter your text to encrypt (!!! to quit): !!! Goodbye!

Here is the body that needs to be followed. I did it partially but did not get there quite right. Thanks!

/* * Lab08a.java * * A program that performs ROT13 encryption on a string. * Used to practice breaking code up into methods. * * @authors ENTER YOUR NAMES HERE * */ package osu.cse1223; import java.util.Scanner; public class Lab08a { public static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static void main(String[] args) { // Fill in the body } // Given a Scanner, prompt the user for an input string. Check to make sure that // the user inputs a non-empty string and return the String to the calling program./ private static String getText(Scanner inScanner) { // Fill in the body } // Given a string, return the ROT13 transformation of the String. For example, given the // input String "ABCXYZ" the function returns the String "NOPKLM". private static String rot13(String input) { // Fill in the body } // Given an input text and an encrypted text, display the strings in the formatted // output described by the instructions for closed lab 08. private static void displayResults(String inText, String encText) { // Fill in the body. } }

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

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions