For JAVA i need help with my project. here is it: As a security-minded professio
ID: 3713379 • Letter: F
Question
For JAVA i need help with my project. here is it:
As a security-minded professional, it is important that only the appropriate people gain access to data in your computer system. This is called authentication. Once users gain entry, it is also important that they only see data related to their role in the zoo. This is called authorization. You have been given a file of users and their credentials. Create an authentication system that does all of the following:
? Asks a user for a username
? Asks a 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 file (use the hashed passwords in the second column; the third column contains the actual passwords for testing)
? Limits failed attempts to three before notifying the user and exiting
? After successful authentication, uses the role in the credential file to display the correct system information loaded from the specific role file
? 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
credentials.txt:
vet.txt:
admin.txt
MD5 Code :
mport 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
}
}
This is my code so far. It works but I need it to have at least two classes I don't know how to go about this:
import java.io.File;
import java.security.MessageDigest;
import java.util.Scanner;
import java.io.FileInputStream;
public class AuthenticationSystem {
public static void main(String[] args) throws Exception{
//Create a scanner object to read input from scnr
Scanner scnr = new Scanner(System.in);
//Declare a variable to keep track the number of attempts
int attempts = 0;
/*Repeat until a successful attempt has been made or
three unsuccessful attempts have been made or
a user chooses to exit*/
while(true) {
//Ask the user for a username
System.out.print("Enter user name: ");
String uName=scnr.nextLine();
//Ask the user for a password
System.out.print("Enter password: ");
String original = scnr.nextLine();
//Convert the password using a message digest five (MD5) hash
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));
}
//Declare a boolean variable to keep track whether
//a login is successful or not
boolean authenticationSuccess = false;
//Open credentials file
Scanner readCred=new Scanner(new FileInputStream("credentials.txt"));
//Search for the user credentials in the credentials file
while(readCred.hasNextLine()){
String record = readCred.nextLine();//Read a record
String columns[] = record.split(" ");//Split the record into individual fields
/*Check the credentials against the valid credentials
provided in the credentials file*/
//Check user name.
if(columns[0].trim().equals(uName)){
/*If user name is matched, check whether the Converted password using
a message digest five password with hashed password in the second column*/
if(columns[1].trim().equals(sb.toString())){//Check password
//If the passwords are same, set the boolean value
//uthenticationSuccess to true
authenticationSuccess = true;//Login success
//Open the role file
Scanner readRole = new Scanner(new File(columns[3].trim()+".txt"));
//Display the information stored in the role file
while(readRole.hasNextLine()){
System.out.println(readRole.nextLine());
}
break;
}
}
}
//If login successful, ask the user whether the user wants to log out or not
if(authenticationSuccess){
System.out.println();
System.out.println("Do you want to log out(y/n): ");
String choice = scnr.nextLine();
//If user wants to log out, exit the system.
if(choice.toLowerCase().charAt(0)=='y'){
System.out.println("Successfully loged out.");
break;
}
//If user wants to continue, set the boolean value
//uthenticationSuccess to true for new login
else{
authenticationSuccess = false;
}
}
//If login is not successful, update the number of attempts
else{
attempts++;
//If maximum attempts reached, notify the user and exit the program
if(attempts == 3) {
System.out.println();
System.out.println("Maximum attempts reached! Exiting...");
break;
}
//otherwise, prompt to enter credentials again
else{
System.out.println("Please enter correct credentials!");
System.out.println();
}
}
}
}
}
Explanation / Answer
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LoginSystem
{
List<User> users;
public List<User> getUsers()
{
return users;
}
public void initializeUsers() throws FileNotFoundException {
users=new ArrayList<>();
User user;
Scanner readCred=new Scanner(new FileInputStream("credentials.txt"));
while(readCred.hasNextLine())
{
String record = readCred.nextLine();
String columns[] = record.split(" ");
user=new User(columns[0],columns[1],columns[3]);
users.add(user);
}
}
public void login() throws Exception
{
Scanner sc = new Scanner(System.in);
boolean loginSuccess=false;
int loginAttempt=0,maxIncorrectAttempt=3;
while(true)
{
System.out.println("Enter username:");
String userName=sc.nextLine();
System.out.println("Enter password:");
String enteredPassword=sc.nextLine();
String encryptedPassword=MD5Digest.getEncryptedPassword(enteredPassword);
for(User currentUser:users)
{
if(userName.equals(currentUser.getUsername()))
{
if(encryptedPassword.equals(currentUser.getEncryptedPassword()))
{
loginSuccess=true;
printWelcomeMessage(currentUser.getRole());
break;
}
}
}
if(loginSuccess)
{
System.out.println();
System.out.println("Do you want to log out(y/n): ");
String logoutChoice = sc.nextLine();
if(logoutChoice.toLowerCase().charAt(0)=='y')
{
System.out.println("Successfully loged out.");
break;
}
else
{
loginSuccess=false;
}
}
else
{
loginAttempt++;
if(loginAttempt==maxIncorrectAttempt)
{
System.out.println("Maximum attempts reached! Exiting...");
break;
}
else
{
System.out.println("Please enter correct credentials!");
}
}
}
}
private void printWelcomeMessage(String role) throws FileNotFoundException
{
Scanner roleFile = new Scanner(new File(role.trim()+".txt"));
while(roleFile.hasNextLine())
{
System.out.println(roleFile.nextLine());
}
}
}
public class MD5Digest
{
public static String getEncryptedPassword(String original) throws Exception
{
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));
}
return sb.toString();
}
}
public class User
{
String username;
String encryptedPassword;
String role;
public User(String username, String encryptedPassword, String role)
{
this.username = username;
this.encryptedPassword = encryptedPassword;
this.role = role;
}
public String getUsername() {
return username;
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public String getRole() {
return role;
}
}
public class Driver
{
public static void main(String[] args) throws Exception
{
LoginSystem loginSystem=new LoginSystem();
loginSystem.initializeUsers();
loginSystem.login();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.