Question: I need an explaining for each step in this code and the reason of it. import java.io.*; import java.net.*; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec;

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

import java.io.*; import java.net.*; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;

public class TCPServer {

/** * args the command line arguments */ public static void main(String[] args) { // TODO code application logic here

int port = 6000;

try (ServerSocket serverSocket = new ServerSocket(port)) {

System.out.println("Server is listening on port " + port);

while (true) { System.out.println("waiting for a client to connect."); Socket socket = serverSocket.accept(); System.out.println("New client connected");

DataInputStream dis = new DataInputStream(socket.getInputStream());

DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); int option; String text;

System.out.println("Waiting for mode."); option=dis.readInt(); switch(option) { case 1: System.out.println("open mode activated."); dos.writeUTF("open mode activated."); do { text=dis.readUTF(); System.out.println("client sent: " + text); dos.writeUTF(text); }while(!text.equals("exit")); break;

case 2: System.out.println("secure mode activated."); dos.writeUTF("secure mode activated."); String keyString="1234567890123456"; String ivString="6543210987654321"; IvParameterSpec iv=new IvParameterSpec(ivString.getBytes("UTF-8")); SecretKeySpec key=new SecretKeySpec(keyString.getBytes("UTF-8"),"AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, key, iv); String plainString="";

do { text=dis.readUTF(); System.out.println("client sent: " + text); byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(text)); plainString=new String(plainText); System.out.println("plain text is: " + plainString); dos.writeUTF(text); }while(!plainString.equals("exit")); break; case 3: socket.close(); System.out.println("client has disconnected"); break; } }

} catch (Exception ex) { /*System.out.println("Server exception: " + ex.getMessage()); ex.printStackTrace(); */ System.out.println("port : " + port);

} } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!