This task will have you implementing a very simple (and very easy to break) enco
ID: 3873187 • Letter: T
Question
This task will have you implementing a very simple (and very easy to break) encoding system, but the concepts you use to do so are at the heart of more advanced ones as well. You should be sure to read through the SingleShiftsample code before working on this to familiarize yourself with some Java syntax and problem solving approaches that will be useful here. The core idea was actually used in early online discussion groups as a way to make a post unreadable at first glance, but easy to decode once a person decided they wanted to read it. Our Task 2 system is called Rot32. Since there are 64 valid characters in this project when it comes to messages, one way to easily encode and decode a message is to "rotate" each character by 32 positions. This means that the same algorithm can be used to encode and to decode messages. It also means that as soon as someone knows our approach, all of our messages can be instantly decoded by the enemy.
One subtle point is that you need to rotate "around" the end of the valid UNICODE range. For example, the letter H has an UNICODE value of 72. When you add 32 to this you get 104. However, we said we'd only be using characters between UNICODE positions 32 and 95. The way we handle this is that if the new value is greater than 95, we simple "wrap around" to the start of our range. To do this, we can subtract 64 (the range of values we have) from the number, and we will have essentially have "wrapped around" the valid character list. We now have 40, which is ( in UNICODE.
Using this system, the message
Hello class
would first need to be changed to
HELLO CLASS
and then encoded as
(%,,/@#,!33.
Work this out by hand using the ASCII part of the UNICODE table to make sure you are comfortable with the idea. There is one method to implement:
The parameter message will refer to a String object. Your code will need to iterate through this character by character, "rotate" that character by 32 positions, and then use it as part of a new String that you will return at the end of the method. Don't forget that we will only be using upper-case letters! Once again, you might find the .toUpperCase method that all String objects can invoke is useful
This task wil have you implementing a very simple (and very easy to break) encoding system, t the concepts you use to do so are at the heart of more advanced ones as well. You should be sure to read throngh the SingleShift saple cde before working on this to fauiliarie yoursel with soe Java syntax and proble olving approaches thal will be useful hee The vore idea was aclly used in early ouline discussion groups as a way to make a post unrcadable at first glance, but casy to decode once a persou decided they wanted to rcad it. Our Task 2 system is called Rot3. Since there are 64 valid characters in this project when it conmes to cssages, oac way to casily cncodc and decode a message is to "rotate" each character by 32 posirions. This means that the same algorithmesed o encode and to decode messages. It also means that as soon as somene knows our appach, all of our messages can he ustautly decoded by thee nemy one sitle point is hat you need to rotate aroind the end of the valid NICODE range. For example the letter H has an t TCODE value of 72 when you add 32 this von get 104 However, we said we'd only be using characters between UNICODE positions 32 and 95. The way we handle this is that if the new value is greater than 95, we siple "wrap arouud" to the start of our range. lo do this, we cau subtract 64 (the rauge of values e have) from the number, and we will have essentially have "wrapped around" the valid character list. We now have 40, which is (in UNICODE. Using this syste the message Hello class would first need to bc changed to HELLO CLASS and then encoded as ,,/24,133 Work this out by hand using the ASCIL part of the UNICODE table to make sure you are comfortable with the idea. There is oue mcthod to impleient: public tatic String rat32 (String nng) The parameter message will refer to a String ohject. Your code will need to iterate through this character by character, "rotare" that character hy 32 postons, and then use it as part of a new Sing thar you will e atthe end of the method. Dont orget that we will onlv be using upper-case letters Once ngalu, you might ind the to UpperCase method that a String ob ecis cau Lnvoke Is useful Then take t us a gori m and hand write the pseudocode oJavuExplanation / Answer
Java Implementation of the algorithm:
/*
Java Implementation of the rot32 algorithm
*/
public class Rot32 {
public static String rot32(String message){
String result = "";
message = message.toUpperCase(); // Converting string to Upper case
// Looping through each character of the string
for (int i= 0; i < message.length(); i++){
char c = message.charAt(i);
int newUNICODE = (int)c + 32; // Adding 32 to its UNICODE
if (newUNICODE > 95){ // Checking it its new UNICODE is above 95
newUNICODE = newUNICODE - 64; // Subtracting 64 (range) if its above 95
}
char newChar = (char) newUNICODE; // Converting UNICODE integer to character
result = result + newChar; // Appending it to the result string
}
return result; // Returning final result
}
public static void main(String[] str){
System.out.println(rot32("Hello class"));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.