Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Challenge #4 Fun with Strings The purpose of this challenge is to get you to see how useful your String manipulation skills can be, especially

Challenge #4 Fun with Strings

The purpose of this challenge is to get you to see how useful your String manipulation skills can be, especially if you were James Bond trying to crack a message being sent by a spy from Russia

Encryption is the process of encoding messages to keep them secret, and decryption is the process of reversing encryption and putting the encrypted message back to the original text. Did you know that the Caesar Cipher is one of the first techniques ever used to perform encryption? It shifts the alphabet by a certain number of letters, so the original message appears encrypted.

You are James Bond, Java programmer, and counter-spy. Your mission, should you choose to accept it, is to encrypt the password that the spy types using the Caesar Cipher, shifting all the letters over by 3 characters. The following chart depicts the pattern:

a ===> d b ===> e c ===> f etc.,. Notice that at the end of the alphabet, the last 3 letters wrap around to the beginning of the alphabet:

x ===> a y === > b z === > c All the letters can be shifted by 3 simply by doing the following code snippet: String spyMessage = password ; String scrambledMessage = ; char letter, newLetter; int newLetterNum; Loop Pseudo Code: 0. For the sake of simplicity, please convert the spyMessage to all lower case letters. 1. Get the letter in spyMessage at index i until i < spyMessage.length. letter = spyMessage.charAt(i); //need to keep adding 1 to i, until the end of the message 2. Check to see if the letter is either x, y, or z, and if so, hard code its scrambled new value:

If (letter == x ) { newLetter = a; } else if (letter == y) { newLetter = b; } else if (letter == z) { newLetter = c; }

3. For all the other letters, convert the letter to an int, add 3 to it, then convert it back to a char: newLetterNum = ( (int) letter ) + 3; newLetter = (char) newLetterNum;

*The reason this works is because each letter has a positive integer assigned to it in this chart: http://www.asciitable.com/ 4. Regardless of how the newLetter was created, concatenate it to the scrambledMessage: scrambledMessage += newLetter; 5. Continue Looping until all the letters of the spyMessage are processed. 6. Display the original spyMessage and the scrambledMessage

7. Once the encryption algorithm works, think about how you would reverse it, to get the scrambled message back to the spyMessage.

What 3 letters would need special consideration, and have to be hardcoded to the decrypted value? What would the algorithm for the rest of the letters look like? See the ASCII chart here: http://www.asciitable.com/

The Challenge Loop to display a menu that will ask the user to choose 1 of 3 options:

1. Encryption Enter a password to see what it looks like scrambled. 2. Decryption Enter an encrypted word to see what it looks like unscrambled. 3. End Spy Mission When the user enters option 1, ask the user for the word to encrypt. Only use letters a z, then, display the encrypted message to the user. Note: passwords dont have spaces. When the user enters option 2, ask the user for the encrypted word to decrypt. Again, only use letters a z. Then, display the decrypted word to the user.

When the user enters option 3, advise the user to that this program will self-destruct in seconds, and show 5 4 3- 2- 1- Boom!

For extra credit, after the user selects the option 1 or 2, and after the user enters a word to encrypt or decrypt, ask the user to guess what the encrypted or decrypted message would be. Compare the users guess to the encrypted or decrypted message, and tell the user if he/she guessed correctly or not. If the user guessed correctly, display a message stating: Successfully encryptedmission accomplished! If the user guessed incorrectly, display a message stating Unsuccessfully encrypted...Danger, danger!

For more extra credit, instead of the algorithm provided above, for shifting letters, try this one:

public static char encrypt(char letter, int offset)

{

if (offset < 0) { // if the offset is negative, add 26 to it so that it will work with the modulus

offset = 26 + offset;

}

letter -= 97; // subtract 97 from the letter to ensure that it will be from 0-26

letter = (char) ((letter + offset) % 26); // offset the character and compensate for offsets > 25.

letter += 97; // adds 97 back to the letter so that it returns back to its original position, but offset

return letter; } To call the method:

encrypt(spyMessage.charAt(i), 3); // for a 3-character offset to the right

encrypt(spyMessage.charAt(i), -3); // for a 3-character offset to the left

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jamesbondtester; /** * * @author cristy */ public class JamesBondTester { /** * @param args the command line arguments */ public static void main(String[] args) { //Add a do-while loop that keeps looping while the user //enters either 1 or 2. int menuOption = displayMenu(); switch(menuOption) { case 1: encryptPassword(); break; case 2: decryptPassword(); break; case 3: selfDestruct(); break; } } public static int displayMenu() { //Display the menu to show: // 1. Encrypt a Password? // 2. Decrypt a Password? // 3. Stop Spying... //Get input from user and store in a local variable //loop to validate value entered as 1, 2, or 3 //and if not, keep looping and asking user to enter valid value //return value entered return 0; //temporary } public static void encryptPassword() { //1. Ask user for for to encrypt //2. Instantiate the PasswordEncryption object, passing // it the word and a boolean value of true, meaning encrypt //3. Display the encrypted word to the user. //4. Extra Credit: Before displaying encrypted word, // ask user to guess the encrypted word. If user guesses // correctly, state "Successfully encryptedmission accomplished" // If user did not guess correctly, state "Unsuccessfully encrypted...Danger, danger! } public static void decryptPassword() { //1. Ask user for encrypted word to decrypt //2. Instantiate the PasswordEncryption object, passing // it the word and a boolean value of false, meaning decrypt //3. Display the decrypted word to the user. //4. Extra Credit: Before displaying decrypted word, // ask user to guess the decrypted word. If user guesses // correctly, state "Successfully decryptedmission accomplished" // If user did not guess correctly, state "Unsuccessfully decrypted...Danger, danger! } public static void selfDestruct() { //Advise the user that this program will self-destruct in 5 seconds //5 - 4 - 3 - 2 - 1 - 0 Boom! } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jamesbondtester; /** * * @author cristy */ public class PasswordEncryption { String origWord; String encryptWord; public PasswordEncryption(String word, boolean encrypt) { if (encrypt) { this.origWord = word; this.encryptWord = ""; } else { this.encryptWord = word; this.origWord = ""; } } public String getOrigWord() { return origWord; } public void setOrigWord(String origWord) { this.origWord = origWord; } public String getEncryptedWord() { return encryptWord; } public void setEncryptedWord(String encryptWord) { this.encryptWord = encryptWord; } public String toString() { return "Original Word: " + origWord + " Encrypted Word:" + encryptWord; } public void encryptOrig() { //Add code to take the origWord and encrypt it, storing //the encrypted word in encryptWord } public void decryptEncrypt() { //Add code to take the encryptWord and decrypt it, storing //the original word in orgiWord } }

Need help completing this. Doing this on netbeans.

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

Mastering Big Data Interview 751 Comprehensive Questions And Expert Answers

Authors: Mr Bhanu Pratap Mahato

1st Edition

B0CLNT3NVD, 979-8865047216

More Books

Students also viewed these Databases questions