I've been doing this for the school assignment.
So the whole idea of this program is to use random numbers to disguise a message by translating it into pairs of numbers; or to reveals messages by translating a series of pairs of numbers back in to the original characters. It works by using a random number generated for each character position in the original message. Also, for the encoding part, it requires First number: The seed number for the random number generator; Second number: The number of characters in the decoded message; Each subsequent number: The integer version of the original character offset by a random number(If the character position number is zero or even the random number is added to the character’s integer value. Otherwise it is subtracted.)
Thank you in advance!
This is what I got so far:
import java.util.Scanner;
import java.util.Random;
import java.util.regex.Pattern;
public class CodingProject{
public static final int SEED_NUMBER = 150;
public static void main(String[] arges){
Scanner scan = new Scanner(System.in);
// prompt the user to choose decoding or encoding message.
System.out.println("Please choose from the following:");
System.out.println("\nE-Encoding a message \n\nD-Decoding a message");
System.out.print("Desired Action: ");
String choice = scan.nextLine();
choice = choice.toLowerCase();
Random gen = new Random (150);
//Encoding message
if(choice.equals("e")){
// prompt user to give a message to be encoded.
System.out.println("On the next line please type or paste the message to be encoded:");
String line = scan.nextLine();
int chr = line.length();
System.out.print(SEED_NUMBER + " " + chr + " ");
for(int i = 1; i <= line.length() - 1; i++){
int offset = gen.nextInt(SEED_NUMBER) + 1;
int value = (int)line.charAt(i);
if(chr == 0 || chr % 2 == 0){
int newValue = offset + value;
System.out.print(newValue + " ");
}else{
int newValue = offset - value;
System.out.print(newValue + " ");
}
} // end for loop
}else{ // Decoding message
System.out.println("On the next line please type or paste the message to be encoded:");
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
scan.useDelimiter(delimiters);
while (scan.hasNextInt()){
int k = scan.nextInt(); // this is where I got stuck,
//somehow I managed to let the user input series of pairs of numbers,
//but I don't know what to do next in order to apply the rules and convert the numbers into characters.
}
}
} // end main method
} // end class