Original Question (Problem 3)
I'm completing a problem for class that requires converting a string message from ascii with a key to a decoded String message. I've tried both accessing the string from message.charAt(i) and by converting it to a char array but both times I get this weird out put on the console.
This is the method I have running
public static char[] decrypt(String message) {
char[] decoded = new char[message.length()];
char[] newmessage = message.toCharArray();
int ascii;
for(int key=0; key<=100; key++) {
for(int i=0; i<message.length(); i++) {
ascii = ( (int)newmessage[i] + 127) - 32;
if(ascii > 126)
decoded[i] = (char)((int)newmessage[i] - key);
else
decoded[i] = (char)((((int)newmessage[i] - key) +127) -32);
}
}
System.out.println(decoded);
return decoded;
}
This is where I called it in main
System.out.println("Problem 3");
String message = ":mmZ\\dxZmx]Zpgy";
System.out.println("Message Received: ");
System.out.println(message);
decrypt(message);
I can't seem to figure out where I went wrong with it. The expected output is for each key to be printed with the corresponded decoded message. The 88th key will show the message is "Attack at Dawn!".
