Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE READ: I got the majority of this working. That being said, all I need is to utilize my java code from EncryptDecrypt to encrypt

PLEASE READ: I got the majority of this working. That being said, all I need is to utilize my java code from EncryptDecrypt to encrypt the username and password credentials that come from Reserve App.java to Reserve Server.java. EncryptDecrypt.java doesn't need to be edited. EncryptDecrypt.java utilizes AES. If you require more than 1 question for this please make mention of that in your comment

EncryptDecrypt.java

image text in transcribed

ReserverServer.java (Server)

/* Server side of the login and reservation program */

import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Scanner;

public class ReserveServer {

public static void main(String[] args) throws IOException {

//Create a welcome socket to take any connection request from a reservation app client

final int PORT = 50480;

ServerSocket welcomeSocket = new ServerSocket(PORT);

System.out.println(

"Now listening at " + welcomeSocket.getLocalSocketAddress());

while(true) { //The server can keep processing new client connection request

Socket connectionSocket = welcomeSocket.accept();

System.out.println(

"Connected to " + connectionSocket.getInetAddress());

BufferedReader inFromClient = new BufferedReader(

new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient = new DataOutputStream(

connectionSocket.getOutputStream());

String request = inFromClient.readLine();

System.out.println("Received from client: " + request);

String[] reqArgs = request.split(";");

//If the request is about login, extract username and password, and then check them with the stored passowrd file

if(reqArgs[0].equalsIgnoreCase("Login")){

String username = reqArgs[1];

String password = reqArgs[2];

String LoginResponseMsg;

//Open the password file to check whether there is a record that matches the username and password

if(checkUsernamePassword(username, password)){ //If the username and password matches

//Send a successful login response message back

LoginResponseMsg = "LoginResponse;"+"Success"+" ";

System.out.println("Username and password matches a record! Login succeeded!");

}

else{ //The username does not exist or the password was wrong

//Send a failed login response message back to the client

LoginResponseMsg = "LoginResponse;"+"Failed"+" ";

System.out.println("Username or password does not match! Login failed!");

}

outToClient.writeBytes(LoginResponseMsg);

}

}

}

/* ************************************************************************

* Method name: checkUsernamePassword*/

public static boolean checkUsernamePassword(String username, String password){

//setup variables

String combo = username + "," + password;

boolean match = false;

try{

//read the file searching for username/password combo

File myFile = new File("usernamepassword.txt");

Scanner myScanner = new Scanner(myFile);

while(myScanner.hasNextLine()){

String line = myScanner.nextLine();

if(line.equals(combo)){

match = true;

}

}

//deal with exceptions

} catch (FileNotFoundException e){

System.out.println("Cannot find the password file"); //I probably don't need both of these

System.err.println(e);

}

return match;

}

}

ReserveApp.java

/* Client side of the login and reservation program*/

import java.util.*;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Socket;

import java.net.UnknownHostException;

public class ReserveApp {

public static void main(String args[]) throws IOException{

//I. Connect with the server through TCP socket

final String HOST = "localhost"; //The server's IP address if we are using two computers

final int PORT = 50480;

System.out.print("Connecting to server...");

Socket clientSocket = null;

try {

clientSocket = new Socket(HOST, PORT);

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(" Connected.");

Scanner input = new Scanner(System.in);

//II. Login

//Allow one to try up to 5 times if login failed

//1. Prepare the login message

//Ask the user to type the username

System.out.println("Input the username:");

String username = input.next();

//Ask the user to type the password

System.out.println("Input the password:");

String password = input.next();

//Prepare the Login message using the username and password information

String LoginMessage = "Login;"+username+";"+password+" ";

//2. Send out the login request in the encrypted format

DataOutputStream outToServer =

new DataOutputStream(clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader(

new InputStreamReader(clientSocket.getInputStream()));

System.out.println("Sending Login request to the server...");

outToServer.writeBytes(LoginMessage);

System.out.println(" Login request sent out, waiting the response....");

//Process the response

String response = inFromServer.readLine();

System.out.println("Response: " + response);

//III. Reserve if login succeeded

//IV. Tear down the connection when the session is done

clientSocket.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions