Guest User

Untitled

a guest
Feb 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. // Defines the main class
  2. public static void main(String[] args) throws IOException {
  3.  
  4. // Calls the login screen method
  5. loginScreen();
  6.  
  7. }
  8.  
  9. // This defines the login screen method
  10. @SuppressWarnings("resource")
  11. public static void loginScreen() {
  12.  
  13. // Defines the string variable to generate password
  14.  
  15. @SuppressWarnings("UnusedAssignment")
  16. String generatePass = "";
  17.  
  18. // sets the login attempts to 3
  19. int flag1 = 0, attmpts = 3;
  20.  
  21. // Define new input stream reader object
  22. BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  23.  
  24. // Display message to login
  25. System.out.println("nLogin");
  26.  
  27. // Try block loop
  28. try {// FIXME: move closer to file reader
  29.  
  30. do {
  31.  
  32. // counts down login attempts before locking out user
  33. attmpts--;
  34.  
  35. // prompts user to enter username
  36. System.out.println("Please Enter Your Username");
  37.  
  38. // stores the user input for username
  39. String userName = br1.readLine();
  40.  
  41. // prompts user for password
  42. System.out.println("Please Enter Your Password");
  43.  
  44. // This creates an instance of the MD5 hash algorithm
  45. // Stores password value
  46. String original = br1.readLine();
  47.  
  48. MessageDigest MD1 = MessageDigest.getInstance("MD5");
  49.  
  50. MD1.update(original.getBytes());
  51.  
  52. byte[] digest = MD1.digest();
  53.  
  54. StringBuilder Sb1 = new StringBuilder();
  55.  
  56. // for loop to iterate through the credentials file looking for hash
  57. for (int k = 0; k < digest.length; k++) {
  58.  
  59. Sb1.append(Integer.toString((digest[k] & 0xff) + 0x100, 16).substring(1));
  60. generatePass = Sb1.toString();
  61. }
  62.  
  63. // sets variable cuurent line
  64. String CurrentLne;
  65.  
  66. // Define an instance of the file reader to read through the credentials file
  67. BufferedReader bin1 = new BufferedReader(new FileReader
  68. ("C:\Users\fento\Documents\NetBeansProjects\NewAuthenticate\src\newauthenticate\credentialfile.txt"));
  69.  
  70. // Loop through the file until you reach the end of the file
  71. while ((CurrentLne = bin1.readLine()) != null) {
  72.  
  73. // Seperates each line within the file
  74. String[] strArr = CurrentLne.split("t");
  75.  
  76. // If statement to validate that user name matches
  77. if (strArr[0].equals(userName)) {
  78.  
  79. // If statement to verify if password matches
  80. if (strArr[1].equals(generatePass)) {
  81.  
  82. // Assign counter value
  83. flag1 = 1;
  84.  
  85. // Break
  86. break;
  87.  
  88. }
  89.  
  90. }
  91.  
  92. }
  93.  
  94. // If statement to iterate through login attempts beginning at 0
  95. if (attmpts == 0) {
  96.  
  97. // Display a message with number of logins remaining
  98. System.out.println("You have only" + attmpts + " more login attempts left");
  99.  
  100. // Displays an exit message message
  101. System.out.println("Exit...");
  102.  
  103. // Exits the system after 3 fail login attempts
  104. System.exit(1);
  105.  
  106. }
  107.  
  108. // If flag is 1
  109. if (flag1 == 1) {
  110.  
  111. // Calls the admin screen method
  112. AdminLogin();
  113.  
  114. // Break
  115. break;
  116.  
  117. } else {
  118.  
  119. // Displays a failed or invalid login attempt
  120. System.out.println("You have entered an Invalid Username or Password.");
  121.  
  122. // print a blank line
  123. System.out.println();
  124.  
  125. // prompts user to try to login again
  126. System.out.println(" Please try again.");
  127.  
  128. // print a blank line
  129. System.out.println();
  130.  
  131. // prompts user with number of login attempts left
  132. System.out.println(attmpts + " more attemptes left.n");
  133.  
  134. }
  135.  
  136. } // while loop
  137. while (attmpts > 0);
  138.  
  139. } // catches of no such algorithm exception error
  140. catch (NoSuchAlgorithmException nae) {
  141. System.out.println("Current Alogorithm does not function: ");
  142. }
  143. catch (NoSuchFileException e){
  144. System.out.println("Current file will not: ");
  145. }
  146. catch(IOException e){
  147. System.out.println("There is a problem reading the current file:");
  148. }
  149.  
  150.  
  151. }
  152.  
  153. // Definition of the admin screen method
  154. public static void AdminLogin() {
  155.  
  156. // Declare variable
  157. String logOut1;
  158.  
  159. // sets up a scanner variable and a new scanner object
  160. Scanner sc1 = new Scanner(System.in);
  161.  
  162. // dispalys a welcome admin prompt to the user
  163. System.out.println("nWelcome Admin");
  164.  
  165. // prompts user to continue or press 99 to exit
  166. System.out.println("continue or Press 99 for log outn");
  167.  
  168. // Do loop
  169. do {
  170.  
  171. logOut1 = sc1.nextLine();
  172.  
  173. } // while loop that looks for user inputting 99 to log out
  174. while (!logOut1.equals("99"));
  175.  
  176. // if statement looking for 99 and if entered program exits
  177. if (logOut1.equals("99")) {
  178.  
  179. // method call to the login screen
  180. loginScreen();
  181.  
  182. }
  183.  
  184. }
Add Comment
Please, Sign In to add comment