Advertisement
robertsam

Password NEA P1 26/02/2020

Feb 26th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. package year_9_work;
  2. import java.util.Scanner;
  3. import java.util.stream.Collectors;
  4. import java.lang.Object;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. public class Class_PassCheck {
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. Scanner scan = new Scanner(System.in);
  11. while (true) {
  12. System.out.println
  13. ("1. Check Password\r\n" +
  14. "2. Generate Password\r\n" +
  15. "3. Quit.\r\n" +
  16. "Type Selection Number");
  17. int selection = scan.nextInt();
  18.  
  19. switch (selection) {
  20. case 1:
  21. System.out.println("Enter password to be checked");
  22. String password = scan.nextLine();
  23. String StrengthQual;
  24. if (Check.BasicCheck(password) == true) {
  25. int Strength = Check.StrengthCheck(password);
  26. if (Strength > 20) {
  27. StrengthQual = "Strong";
  28. }
  29. else if (Strength <= 0) {
  30. StrengthQual = "Medium";
  31. }
  32. else {
  33. StrengthQual = "Weak";
  34. }
  35. }
  36. break;
  37.  
  38. case 2:
  39.  
  40. break;
  41.  
  42. case 3:
  43. break;
  44.  
  45. default:
  46. break;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. class Check {
  53. public static boolean BasicCheck(String password) {
  54. //System.out.println(ValidList);
  55. //if(ValidList.contains('a')) {
  56. // System.out.println("True");
  57. //}
  58. boolean success = true;
  59. String validStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$%^&*()-_=+";
  60. ArrayList<Character> ValidList = (ArrayList<Character>) validStr.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
  61. if (password.length() < 8 ) {
  62. System.out.println("Innapropriate password length; Too short. (min 8)");
  63. success = false;
  64. }
  65. else if (password.length() > 24 ) {
  66. System.out.println("Innapropriate password length; Too long. (max 24)");
  67. success = false;
  68. }
  69. else {
  70. boolean ascii = true;
  71. for(int i = 0; i < password.length(); i++) {
  72. if (!ValidList.contains(password.charAt(i))) {
  73. System.out.println("Innapropriate character used ("+password.charAt(i)+").");
  74. ascii = false;
  75. }
  76. }
  77. if (ascii = true) {
  78. success = true;
  79. }
  80. else {
  81. success = false;
  82. }
  83. }
  84. return success;
  85. }
  86. public static int StrengthCheck(String password) {
  87. int Strength = password.length();
  88. String UpperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  89. ArrayList<Character> UpperList = (ArrayList<Character>) UpperStr.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
  90. String LowerStr = "abcdefghijklmnopqrstuvwxyz";
  91. ArrayList<Character> LowerList = (ArrayList<Character>) LowerStr.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
  92. String NumStr = "0123456789";
  93. ArrayList<Character> NumList = (ArrayList<Character>) NumStr.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
  94. String SymStr = "!$%^&*()-_=+";
  95. ArrayList<Character> SymList = (ArrayList<Character>) SymStr.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
  96. String QwertyStr = "qwertyuiopasdfghjklzxcvbnm";
  97. int[] QposArr = new int[password.length()];
  98. boolean Lower = false; boolean Upper = false; boolean Numb = false; boolean Symb = false;
  99.  
  100. for (int i = 0; i < password.length(); i++) {
  101. if (UpperList.contains(password.charAt(i))) {
  102. Upper = true;
  103. }
  104. if (LowerList.contains(password.charAt(i))) {
  105. Lower = true;
  106. }
  107. if (NumList.contains(password.charAt(i))) {
  108. Numb = true;
  109. }
  110. if (SymList.contains(password.charAt(i))) {
  111. Symb = true;
  112. }
  113. QposArr[i] = QwertyStr.indexOf(Character.toLowerCase(password.charAt(i)));
  114. }
  115.  
  116. if (Lower) {
  117. Strength+=5;
  118. System.out.println("Lower Case Used (+5). Total = "+Strength);
  119. }
  120. if (Upper) {
  121. Strength+=5;
  122. System.out.println("Upper Case Used (+5). Total = "+Strength);
  123. }
  124. if (Numb) {
  125. Strength+=5;
  126. System.out.println("Number Used (+5). Total = "+Strength);
  127. }
  128. if (Symb) {
  129. Strength+=5;
  130. System.out.println("Symbol Used (+5). Total = "+Strength);
  131. }
  132. if (Lower && Upper && Numb && Symb) {
  133. Strength+=10;
  134. System.out.println("All Character Types (+10). Total = "+Strength);
  135. }
  136. if ((Lower||Upper)&&!(Numb&&Symb)) {
  137. Strength-=5;
  138. System.out.println("Only Letters (-5). Total = "+Strength);
  139. }
  140. if ((Symb)&&!(Lower||Upper||Numb)) {
  141. Strength-=5;
  142. System.out.println("Only Symbols (-5). Total = "+Strength);
  143. }
  144. if ((Numb)&&!(Lower||Upper||Symb)) {
  145. Strength-=5;
  146. System.out.println("Only Numbers (-5). Total = "+Strength);
  147. }
  148. for (int i = 2; i < QposArr.length; i++) {
  149. if (((QposArr[i]-1)==(QposArr[i-1]))&&((QposArr[i-1]-1)==(QposArr[i-2]))) {
  150. Strength-=5;
  151. System.out.println("String of Adjacent characters from QWERTY ("+QwertyStr.charAt(QposArr[i-2])+QwertyStr.charAt(QposArr[i-1])+QwertyStr.charAt(QposArr[i-3]));
  152. }
  153. }
  154.  
  155. return Strength;
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement