Strings Programming Assignment Password Checking Software to reset passwords oft
ID: 3565811 • Letter: S
Question
Strings Programming Assignment
Password Checking
Software to reset passwords often requires the user to enter the password twice, checking to make sure it was entered the same way both times. Write a method in Java that can verify that the two passwords entered are correct. . Your project should contain a method that ask the user to enter a password twice, then either tell the user that the two entries were not the same and start the process over again, or, if they are the same, tell the user that the new password was accepted.
Additionally, the user is required to enter in a password that meets specific security requirements.
Proper Passwords are required to follow the these rules:
Explanation / Answer
import java.util.Scanner;
public class PasswordCheck{
public static void main(String args[]){
int returnedValue;
ActualChecking process;
while(true){
process = new ActualChecking();
returnedValue = process.startProcess();
switch(returnedValue){
case 0:// Successfull if returned value is 0
System.out.println("The new password is accepted.");
break;
case 1:// PWD Mismatch if returned value is 1
System.out.println("Two entries are not same. Process starting agin.");
break;
case 2:// PWD is insecure if returned value is 2
System.out.println("Security requirements not met.");
break;
default:
break;
}
//Additional to printing Success Message, Breaking out of forever WHILE Loop.
if(returnedValue == 0) break;
}
}
}
class ActualChecking{
String pwd, rpwd;
public int startProcess(){
//To redirect Input
Scanner in;
in = new Scanner(System.in);
System.out.print("Enter a Password: ");
pwd = in.nextLine();
System.out.print("Reenter Password: ");
rpwd = in.nextLine();
//Checking for password missmatch
if(!(pwd.equals(rpwd))) return 1;
//Checking for insecure password
if(checkInternal() == false) return 2;
//If escaped from above checks then only returning '0'
return 0;
}
public boolean checkInternal(){
char currentChar;
//History variables to check sequence
char historyChars[] = {' ',' ',' '};
//Flags to check security
int startSymbolFlag = 1;
int SequenceFlag = 1;
int spaceFlag = 1;
int alphaFlag =0 ;
int intFlag = 0;
int charFlag = 0;
//Returning without check for other securities if length is less than 8
if(pwd.length()<8) return false;
for(int i=0; i<pwd.length(); i++){
currentChar = pwd.charAt(i);
if(i==0) {
if(currentChar == '!' || currentChar == '?') startSymbolFlag = 0;
}
if(Character.isWhitespace(currentChar)) spaceFlag = 0;
else if(Character.isLetter(currentChar)) alphaFlag = 1;
else if(Character.isDigit(currentChar)) intFlag = 1;
else charFlag = 1;
//historychars always contain three contineous characters from present character with the below logic
historyChars[0] = historyChars[1];
historyChars[1] = historyChars[2];
historyChars[2] = currentChar;
//Checking Sequence
if(historyChars[0] == historyChars[1] && historyChars[1] == historyChars[2])
SequenceFlag = 0;
}
//Product of all flags will be "1" only when all security checks are qualified else it will be "0"
if(startSymbolFlag*SequenceFlag*spaceFlag*alphaFlag*intFlag*charFlag == 0) return false;
else return true;
}
}