Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.util.Scanner;
  9. // Import necessary classes
  10.  
  11. public class AuthenticationSystem{
  12. // Define the class AuthenticationSystem
  13.  
  14. public static void main(String[] args) throws IOException{
  15.  
  16. }
  17.  
  18. public class ValidateCredentials{
  19.  
  20. private boolean isValid;
  21. private String filePath;
  22. private String credentialsFileName;
  23.  
  24. public ValidateCredentials(){
  25. isValid = false;
  26. filePath = "C:\\Users\\Owner\\My Documents\\NetBeansProjects\\AuthenticationSystem\\";
  27. credentialsFileName = "credentials";
  28. }
  29.  
  30. public boolean isCredentialsValid() throws Exception{
  31. int flag = 0;
  32. // Declare the variables
  33. int unsuccessfulattempts = 3;
  34. // Declare and initialize the unsuccessfulattempts to 3
  35. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36. //Create object 'br' to read the input from the user
  37. System.out.println("Authentication System");
  38. // Display the Header of the program
  39.  
  40. do{
  41. if(unsuccessfulattempts == 0){
  42. // Checks if the user enters more then 3 attempts.
  43. System.out.println("You are attempting to login more than three times");
  44. System.out.println("You are out of attempts " + "Exiting!");
  45. System.exit(1);
  46. }
  47. else{
  48. // If condition is false, have user try submitting credentials again
  49. System.out.println("Invalid Username or Password.");
  50. System.out.println("Please try again.");
  51. System.out.println(unsuccessfulattempts + " more attemptes left.\n");
  52. }
  53. }
  54. while(unsuccessfulattempts>0);
  55.  
  56. unsuccessfulattempts--;
  57. System.out.println("Username:");
  58. String name = br.readLine();
  59. System.out.println("Password:");
  60. String pwd = br.readLine();
  61. String original = pwd;
  62. MessageDigest md = MessageDigest.getInstance("MD5");
  63. md.update(original.getBytes());
  64. byte[] digest = md.digest();
  65. StringBuffer sb = new StringBuffer();
  66. for (byte b : digest) {
  67. sb.append(String.format("%02x", b & 0xff));
  68. }
  69. System.out.println("");
  70. System.out.println("original:" + original);
  71. System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
  72.  
  73. isValid = readDataFiles(name, sb.toString());
  74.  
  75. return isValid;
  76. }
  77.  
  78. public boolean readDataFiles(String userName, String passWord) throws IOException{
  79. FileInputStream fileByteStream1 = null; // File input stream
  80. FileInputStream fileByteStream2 = null; // File input stream
  81.  
  82. Scanner inFS1 = null; // Scanner object
  83. Scanner inFS2 = null; // Scanner object
  84.  
  85. String textLine = null;
  86. boolean foundCredentials = false;
  87.  
  88. // Try to open file
  89. System.out.println("");
  90. System.out.println("Opening file " + credentialsFileName + ".txt");
  91. fileByteStream1 = new FileInputStream(filePath + "credentials.txt");
  92. inFS1 = new Scanner(fileByteStream1);
  93.  
  94. System.out.println("");
  95. System.out.println("Reading lines of text.");
  96.  
  97. while (inFS1.hasNextLine()){
  98. textLine = inFS1.nextLine();
  99. System.out.println(textLine);
  100.  
  101. if (textLine.contains(userName) && textLine.contains(passWord)){
  102. foundCredentials = true;
  103. break;
  104. }
  105. }
  106. // Done with file, so try to close it
  107. System.out.println("");
  108. System.out.println("Closing file " + credentialsFileName + ".txt");
  109.  
  110. if (textLine != null) {
  111. fileByteStream1.close(); // close() may throw IOException if fails
  112. }
  113. if (foundCredentials == true) {
  114. // Try to open file
  115. System.out.println("");
  116. System.out.println("Opening file " + userName + ".txt");
  117. fileByteStream2 = new FileInputStream(filePath + userName + ".txt");
  118. inFS2 = new Scanner(fileByteStream2);
  119.  
  120. System.out.println("");
  121.  
  122. while (inFS2.hasNextLine()) {
  123. textLine = inFS2.nextLine();
  124. System.out.println(textLine);
  125. }
  126.  
  127. // Done with file, so try to close it
  128. System.out.println("");
  129. System.out.println("Closing file " + userName + ".txt");
  130.  
  131. if (textLine != null) {
  132. fileByteStream2.close(); // close() may throw IOException if fails
  133. }
  134. }
  135.  
  136. return foundCredentials;
  137.  
  138. }
  139.  
  140. }
  141.  
  142. public class Authorization{
  143.  
  144. public Authorization(){
  145. // Create authorization() method
  146. String userLogOut;
  147. Scanner scnr = new Scanner(System.in);
  148. // Get a string from the user to logout by creating Scanner object
  149. System.out.println("\n\tWelcome Admin\t");
  150. System.out.println("Press EXIT for logout\n");
  151.  
  152. do{
  153. userLogOut = scnr.nextLine();
  154. // Take input from the user until they enter EXIT
  155. }
  156.  
  157. while(!userLogOut.equals("EXIT"));
  158.  
  159. if(userLogOut.equals("EXIT")){
  160. // If user enters EXIT to logout
  161. System.exit(1);
  162. }
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement