Java code Authentication System For security-minded professionals, it is importa
ID: 3713486 • Letter: J
Question
Java code
Authentication System
For security-minded professionals, it is important that only the appropriate people gain access to data in a computer system. This is called authentication. Once users gain entry, it is also important that they only see data related to their role in a computer system. This is called authorization. For the zoo, you will develop an authentication system that manages both authentication and authorization. You have been given acredentials file that contains credential information for authorized users. You have also been given three files, one for each role: zookeeper, veterinarian, and admin. Each role file describes the data the particular role should be authorized to access. Create an authentication system that does all of the following:
? Asks the user for a username
? Asks the user for a password
? Converts the password using a message digest five (MD5) hash o It is not required that you write the MD5 from scratch. Use the code located in this document and follow the comments in it to perform this operation.
? Checks the credentials against the valid credentials provided in the credentials file
o Use the hashed passwords in the second column; the third column contains the actual passwords for testing and the fourth row contains the role of each user.
? Limits failed attempts to three before notifying the user and exiting the program
? Gives authenticated users access to the correct role file after successful authentication
o The system information stored in the role file should be displayed. For example, if a zookeeper’s credentials is successfully authenticated, then the contents from the zookeeper file will be displayed. If an admin’s credentials is successfully authenticated, then the contents from the admin file will be displayed.
? Allows a user to log out
? Stays on the credential screen until either a successful attempt has been made, three unsuccessful attempts have been made, or a user chooses to exit You are allowed to add extra roles if you would like to see another type of user added to the system, but you may not remove any of the existing roles.
credential file
Zookeeper
veterinarian
admin
Message Digest 5 (MD5) document
import java.security.MessageDigest;
public class MD5Digest {
public static void main(String[] args) throws Exception {
//Copy and paste this section of code
String original = "letmein"; //Replace "password" with the actual password inputted by the user
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
//End copy/paste
System.out.println("original:" + original);
System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
}
}
NOTE -: WOULD LIKE PSEUDOCODE AND ACTUAL JAVA CODE
Explanation / Answer
============ PSEUDOCODE =========================
STEP 1: Enter choice 1. Login, 2. Exit program
STEP 2: If enters ‘1’ in STEP 1, GOTO STEP 4.
STEP 3: If enters ‘2’ in STEP 1, Exit the program
STEP 4: Enter username and password.
STEP 5: Convert password to MD5
STEP 6: Match username and converted password in credential file
STEP 7: If successful login, show respective files content.
STEP 8: Ask user to logout after viewing the content or exit the program
STEP 9: If unsuccessful login, re-enter username & password up to 3 times
STEP 10: Exit program if ‘3’ unsuccessful login attempts.
============== JAVA CODE =============================
package chegg;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
/**
* Created by Nirupom Biswas on 22-Apr-18.
*/
public class AuthenticationRunner {
private final String CREDENTIAL_FILE = "Credential.txt";
private final String ADMIN_FILE = "Admin.txt";
private final String VETERINARIAN_FILE = "Veterinarian.txt";
private final String ZOOKEEPER_FILE = "Zookeeper.txt";
private final int LOGIN_LIMIT = 3;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
AuthenticationRunner runner = new AuthenticationRunner();
while (true) {
System.out.println("1. Login 2. Exit");
int ch = scanner.nextInt();
switch (ch) {
case 1:
int choice = runner.login();
//If -1, that means 3 unsuccessful login attempts
if (choice == -1) {
System.out.println("You have Entered 3 Wrong username/password. System will Exit");
System.exit(-1);
} else if (choice == 0) { //If no role is displayed, then no details to show. So no login
System.out.println("You are not specified to any role. Unable to login.");
} else { //For Successful login, show respective details and ask to logout or exit
choice = runner.showContentAndLogout(choice);
if (choice == 1) { //Logout
System.out.println("Successfully Logged out");
} else { //Terminate Program
System.exit(0);
}
}
break;
case 2:
System.exit(0); //Terminate Program
default:
System.out.println("Wrong Choice");
}
}
}
/**
* Login using username and password
*
* @return : -1 for error and 1,2,3 for role details
* Returns roleType as integer
* 1 -> Admin
* 2 -> Veterinarian
* 3 -> Zookeeper
*/
private int login() {
Scanner scanner = new Scanner(System.in);
int roleType = 0;
int i = 1;
while (i <= LOGIN_LIMIT) {
boolean isAuthorized = false;
System.out.println("Enter Username : ");
String username = scanner.next();
System.out.println("Enter Password : ");
String password = scanner.next();
String md5Pass = getMD5String(password);
FileReader inputFile = null;
try {
inputFile = new FileReader(CREDENTIAL_FILE);
Scanner parser = new Scanner(inputFile);
while (parser.hasNextLine()) {
String line = parser.nextLine();
String[] splitString = line.split(","); //Split each line by comma separator
String fileUsername = splitString[0]; //First Column : username
String filePassword = splitString[1]; //Second Column : MD5 password
String fileRole = splitString[3]; //Forth Column : Role Details
if (username.equals(fileUsername) && md5Pass.equals(filePassword)) {
isAuthorized = true;
if (fileRole.equals("admin")) {
roleType = 1;
} else if (fileRole.equals("veterinarian")) {
roleType = 2;
} else if (fileRole.equals("zookeeper")) {
roleType = 3;
} else {
roleType = 0;
}
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (!isAuthorized) { // Check unsuccessful login attempts
System.out.println("Invalid Credential. " + (LOGIN_LIMIT - i) + " attempts left.");
i++;
} else break;
}
}
//Send Error code as -1
if (i >= LOGIN_LIMIT) {
return -1;
}
return roleType;
}
/**
* Show file for the logged in user role
*
* @param choice
*/
private int showContentAndLogout(int choice) {
FileReader fileReader = null;
Scanner scanner;
try {
if (choice == 1) { //Load Admin file
fileReader = new FileReader(ADMIN_FILE);
} else if (choice == 2) { //Load Veterinarian file
fileReader = new FileReader(VETERINARIAN_FILE);
} else if (choice == 3) { //Load Zookeeper file
fileReader = new FileReader(ZOOKEEPER_FILE);
}
//Read Each line from the file
scanner = new Scanner(fileReader);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Ask user to logout or exit program
System.out.println("1. Logout 2. Exit");
scanner = new Scanner(System.in);
return scanner.nextInt();
}
/**
* Generate MD5 Hash String
*
* @param original
* @return
*/
private String getMD5String(String original) {
StringBuffer sb = new StringBuffer();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
for (byte b : digest) {
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sb.toString();
}
}
=============== OUTPUT ===============================
1. Login
2. Exit
1
Enter Username :
jerome.grizzlybea
Enter Password :
grizzly1234
Invalid Credential. 2 attempts left.
Enter Username :
jerome.grizzlybear
Enter Password :
grizzly1234
Hello, Veterinarian!
As veterinarian, you have access to all of the animals' health records. This allows you to view each animal's medical history and current treatments/illnesses (if any), and to maintain a vaccination log.
1. Logout
2. Exit
1
Successfully Logged out
1. Login
2. Exit
1
Enter Username :
bernie.gorilla
Enter Password :
secret password
Hello, Veterinarian!
As veterinarian, you have access to all of the animals' health records. This allows you to view each animal's medical history and current treatments/illnesses (if any), and to maintain a vaccination log.
1. Logout
2. Exit
1
Successfully Logged out
1. Login
2. Exit
1
Enter Username :
a
Enter Password :
a
Invalid Credential. 2 attempts left.
Enter Username :
a
Enter Password :
a
Invalid Credential. 1 attempts left.
Enter Username :
s
Enter Password :
s
Invalid Credential. 0 attempts left.
You have Entered 3 Wrong username/password. System will Exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.