Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need an explaining for each step in this code and the reason of it. public class Client { public static void main(String[] args) {

I need an explaining for each step in this code and the reason of it.

public class Client { public static void main(String[] args)

{ String HostIP = "192.18.6.1"; int Port = 6000; DataInputStream Input = null; DataOutputStream Output = null; int options; String message; // socket allow communication batween two computers try(Socket s1 = new Socket(HostIP,Port)){ Input = new DataInputStream(s1.getInputStream()); Output = new DataOutputStream(s1.getOutputStream()); Scanner scan = new Scanner(System.in); // need scan to let user choose a number do { System.out.println("Select an option:"); System.out.println("1 : Open mode"); System.out.println("2 : Secure mode"); System.out.println("3 : Quit app"); options = scan.nextInt(); switch (options) { case 1: //here is open mode that well send clear message to the server Output.writeInt(options); String result = Input.readUTF(); System.out.println(result); Boolean x = true; do { System.out.println("Enter something: "); if(x){ message = scan.nextLine(); x = false; } message = scan.nextLine(); Output.writeUTF(message); result = Input.readUTF(); System.out.println(result); } while (!message.equals("exit")); break; case 2: // here is secure mode the message will encrypted Output.writeInt(options); result = Input.readUTF(); System.out.println(result); x = true; String encryptionKey = "0123456789"; String enccryption = "2345678901"; IvParameterSpec iv = new IvParameterSpec(enccryption.getBytes("UTF-8")); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"),"AES"); Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING"); do { System.out.println("Enter something:"); if(x){ message = scan.nextLine(); x = false; } message = scan.nextLine(); c.init(c.ENCRYPT_MODE , key , iv); byte[] cMessage = c.doFinal(message.getBytes()); Output.writeUTF(Base64.getEncoder().encodeToString(cMessage)); result = Input.readUTF(); System.out.println(result); c.init(c.DECRYPT_MODE, key , iv); byte[] b = c.doFinal(Base64.getDecoder().decode(result)); System.out.println(new String(b)); } while (!message.equals("exit")); break; case 3: //this option will let user to quit the application Output.write(options); s1.close(); System.out.println("connection is closed"); break; default: System.out.println("uncorrect option"); } } while (options != 3); } catch(ConnectException e){ // here if the client not connected to the server System.out.println("not found"); } catch(Exception exc){ System.out.println(exc.getMessage()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions