Advertisement
Guest User

Untitled

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