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

Write a JAVA program that reads from the keyboard a small password and then offe

ID: 3688236 • Letter: W

Question

Write a JAVA program that reads from the keyboard a small password and then offers two options:

1. enter your name and a filename to save or

2. enter a file name to load.

With the option 1 proceed to save to the file an encrypted version of your name (details below), with the option 2 proceed to load the information from the file and decrypt the name and print it to the screen. The options 1 and 2 are in reverse (1 saves the encrypted information to the file, 2 decrypts the information from the file). Details of the encryption: Say your name is Andrei and you chose the password 1234, then you do XOR between the ASCII code for A (first letter in your name) and the ASCII code for 1(first letter in your password), I will denote this operation (A,1) then you do XOR between n and 2 (n,2) (second letter in your name with second letter in password) and so on, thus we will have the following operations to perform: (A,1) (n,2) (d,3) (r,4) (e,1) (i,2). Obviously, if you would choose the password qwer then the XOR operations will be between the following pairs: (A,q)(n,w)(d,e)(r,r)(e,q)(i,w). The trick is that the encrypted text can be decrypted due to the following property of XOR: A XOR B=C; C XOR B=A, thus if I have the results of the XOR operations in the file (and this is your name encrypted) then it is sufficient to do XOR with the same password (with the same ASCII codes) and I will get back the original string. For example, with the first password, (A XOR 1) XOR 1 =A, (n XOR 2) XOR 2=n and so on: ((A,1),1) ((n,2),2) ((d,3),3) ((r,4),4) ((e,1),1) ((i,2),2)= A n d r e i.

Explanation / Answer

class Test{ public static void main(String[] args) throws IOException { System.out.println("Enter password:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String pass = br.readLine(); char[] passArr = pass.toCharArray(); System.out.println("1. enter your name and a filename to save or " + "2. enter a file name to load."); int ch = Integer.parseInt(br.readLine()); switch (ch){ case 1: { System.out.println("Enter name and filename :"); String name = br.readLine(); String fname = br.readLine(); char[] nameArr = name.toCharArray(); String result = ""; int i=0; for(char c : nameArr){ int r = c ^ passArr[i%(passArr.length)]; result = result + r; i++; } try { File file = new File(fname); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(result); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } break; case 2: { System.out.println("Enter filename to load:"); String fname = br.readLine(); String encName = ""; try { br = new BufferedReader(new FileReader(fname)); encName = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } char[] encNameArr = encName.toCharArray(); String result = ""; int i=0; for(char c : encNameArr){ int r = (c ^ passArr[i%(passArr.length)])^(passArr[i%(passArr.length)]); result = result + r; i++; } System.out.println("Decrypted Name: "+result); } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote