Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package authenticationsystem;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Scanner;
- // Import necessary classes
- public class AuthenticationSystem{
- // Define the class AuthenticationSystem
- public static void main(String[] args) throws IOException{
- // Call the method login()
- login();
- }
- public class ValidateCredentials{
- private boolean isValid;
- private String filePath;
- private String credentialsFileName;
- public ValidateCredentials(){
- isValid = false;
- filePath = "C:\\Users\\Owner\\My Documents\\NetBeansProjects\\AuthenticationSystem\\";
- credentialsFileName = "credentials";
- }
- public static boolean isCredentialsValid(String name, String pwd) throws Exception{
- unsuccessfulattempts--;
- System.out.println("Username:");
- String name = br.readLine();
- System.out.println("Password:");
- String pwd = br.readLine();
- String original = pwd;
- 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));
- }
- System.out.println("");
- System.out.println("original:" + original);
- System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
- isValid = readDataFiles(userName, sb.toString());
- return isValid;
- }
- public boolean readDataFiles(String userName, String passWord) throws IOException{
- FileInputStream fileByteStream1 = null; // File input stream
- FileInputStream fileByteStream2 = null; // File input stream
- Scanner inFS1 = null; // Scanner object
- Scanner inFS2 = null; // Scanner object
- String textLine = null;
- boolean foundCredentials = false;
- // Try to open file
- System.out.println("");
- System.out.println("Opening file " + credentialsFileName + ".txt");
- fileByteStream1 = new FileInputStream(filePath + "credentials.txt");
- inFS1 = new Scanner(fileByteStream1);
- System.out.println("");
- System.out.println("Reading lines of text.");
- while (inFS1.hasNextLine()){
- textLine = inFS1.nextLine();
- System.out.println(textLine);
- if (textLine.contains(userName) && textLine.contains(passWord)){
- foundCredentials = true;
- break;
- }
- }
- // Done with file, so try to close it
- System.out.println("");
- System.out.println("Closing file " + credentialsFileName + ".txt");
- if (textLine != null) {
- fileByteStream1.close(); // close() may throw IOException if fails
- }
- if (foundCredentials == true) {
- // Try to open file
- System.out.println("");
- System.out.println("Opening file " + userName + ".txt");
- fileByteStream2 = new FileInputStream(filePath + userName + ".txt");
- inFS2 = new Scanner(fileByteStream2);
- System.out.println("");
- while (inFS2.hasNextLine()) {
- textLine = inFS2.nextLine();
- System.out.println(textLine);
- }
- // Done with file, so try to close it
- System.out.println("");
- System.out.println("Closing file " + userName + ".txt");
- if (textLine != null) {
- fileByteStream2.close(); // close() may throw IOException if fails
- }
- }
- return foundCredentials;
- }
- }
- public static void login(){
- // Define the method login()
- String pwd = "";
- int flag = 0;
- // Declare the variables
- int unsuccessfulattempts = 3;
- // Declare and initialize the unsuccessfulattempts to 3
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //Create object 'br' to read the input from the user
- System.out.println("Authentication System");
- // Display the Header of the program
- try{
- do{
- if (flag == 0){
- // The conditional statement that checks the username and password
- isCredentialsValid();
- // Call isCredentialsValid() method
- break;
- }
- if(unsuccessfulattempts == 0){
- // Checks if the user enters more then 3 attempts.
- System.out.println("You are attempting to login more than three times");
- System.out.println("You are out of attempts " + "Exiting!");
- System.exit(1);
- }
- else{
- // If condition is false, have user try submitting credentials again
- System.out.println("Invalid Username or Password.");
- System.out.println("Please try again.");
- System.out.println(unsuccessfulattempts + " more attemptes left.\n");
- }
- }
- while(unsuccessfulattempts>0);{
- catch (NoSuchAlgorithmException e){
- e.printStackTrace();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- public static void authorization(){
- // Create authorization() method
- String userLogOut;
- Scanner scnr = new Scanner(System.in);
- // Get a string from the user to logout by creating Scanner object
- System.out.println("\n\tWelcome Admin\t");
- System.out.println("Press EXIT for logout\n");
- do{
- userLogOut = scnr.nextLine();
- // Take input from the user until they enter EXIT
- }
- while(!userLogOut.equals("EXIT"));{
- if(userLogOut.equals("EXIT")){
- // If user enters EXIT to logout
- login();
- // Call the method login() to enter the credentials
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement