Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java program that will cryptographically encode a message. The program

ID: 3629817 • Letter: C

Question

Create a Java program that will cryptographically encode a message. The program will first ask a user to enter the secret "key," which it will use to encrypt the messages that are typed in next. The key will be needed to decrypt the messages, but you will not have to write the decryption method.

Your program will use a simple substitution cypher, that is, each letter that is typed in will be substituted by the appropriate letter in the key. A space will not be encrypted, so it will remain a space.

Example Execution of the Program


Enter the key as a random ordering of the 26 letters of the English alphabet
0: b
1: w
2: y
3: a
4: c
5: g
6: e
7: i
8: k
9: m
10: o
11: q
12: s
13: u
14: z
15: x
16: v
17: t
18: r
19: p
20: n
21: l
22: j
23: h
24: f
25: d

Enter the message to be encrypted: hello spy
The encrypted message is: icqqz rxf

Enter the message to be encrypted: bye
The encrypted message is: wfc

Enter the message to be encrypted: ace of diamonds
The encrypted message is: byc zg akbszuar

Enter the message to be encrypted:
Goodbye!

In the main method,
Define a character array named 'key' of size 26
Use a loop to get the 26 letters for the key from the user
Use another loop to ask the user for messages. End the loop when the user enters an empty message of length zero
If the user enters a message, read it into a string variable (e.g., named 'message') and then convert it to a character array (e.g., named 'msgChar') using the statement

char[] msgChar = message.toCharArray();
Send the key array, the character array, and the length of the original message to a method named encrypt that will perform the encryption and return a character array containing the encrypted message
Print the encrypted message
In the encrypt method, use the following statement to find the index of the key where the letter to be substituted is located:

int index = (int)(uChar - 'a'); // assuming the user's character is uChar

Explanation / Answer

public static void main(String[] args) throws IOException { char[] key = new char[26]; System.out.println("Enter the key as a random ordering of the 26 letters of the English alphabet"); for(int i=0; i < 26; i++) { System.out.print((i+1)+":"); char tmp = (char) new InputStreamReader(System.in).read(); boolean contains = false; int ascii = tmp; if(ascii < 65 || (ascii>122) || ((ascii90 ))) { System.out.println("Please enter "+(i+1)+" again"); i--; continue; } if(ascii < 97) { ascii = ascii + 32; } for(int j = 0; j < 26; j++) { if(key[j] == tmp) { contains = true; break; } } if(!contains) { key[i] = tmp; } else { System.out.println("Please enter "+(i+1)+" again"); i--; continue; } } boolean askInput = true; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(askInput) { System.out.print("Enter the message to be encrypted: "); String message = br.readLine(); if(message.equalsIgnoreCase("")) { break; } char[] msgChar = message.toCharArray(); char[] encrypted = encrypt(key,msgChar,message.length()); String encryptedMessage = ""; for(int i = 0; i 90) { ascii = ascii - 32; } ascii = ascii - 'A'; encrypted[j] = key[ascii]; } return encrypted; }