Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want you to modify this code and not change output 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

I want you to modify this code and not change output

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 = 5555;

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;

do {

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;

}

} while (option !=3);

}

} catch (Exception ex) {

System.out.println("Server exception: " + ex.getMessage());

ex.printStackTrace();

}

}

}

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

Summarize the uncontrollable determinants of problem recognition?

Answered: 1 week ago

Question

1. How might volunteering help the employer and the employee?

Answered: 1 week ago