Advertisement
Guest User

Untitled

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