Write a method which validates a password in JAVA. Then write a program in JAVA
ID: 3681667 • Letter: W
Question
Write a method which validates a password in JAVA. Then write a program in JAVA that prompts the user to enter a password (and verify it). Your program will call your method to validate the password. Your method must check that the password strings match and contain at least 8 characters. Furthermore, the password must contain at least 1 special character (!,@,#,$,%,^,&,*,_,-), 1 number, 1 lower case, and 1 upper case letter.
Your validation method should print out a message for every violation that the password has.
Now that you have a working password checker, we will add some encryption so that you can be a little bit safer when storing user passwords. In this task, you are to create a method that implements a Caesar cipher. A Caesar cipher is an old and very insecure method of encryption. It involves shifting characters in order to create a new string. For example, if the input string is “abc” then the output string would be “bcd” (see that every character was shifted 1 position to the right). If we wanted to revert the encrypted string back to the original, we would simply shift it 1 to the left. The number of positions to shift is referred to as the key. So if the key is 3, then we would shift right 3 to encrypt and left 3 to decrypt.Create a method that takes three arguments: a String representing the plain text, an integerthat represents the key, and another integer that represents the mode (encrypt or decrypt). Here, mode is 0 for encryption and 1 for decryption. To make things easier, we will be dealing with ASCII characters from the range of decimal 33 to decimal 126. You can find the ASCII table here: http://www.asciitable.com/. This makes the size of our alphabet to be 126 - 33 = 93.
Cipher = E(PlainText) = (PlainText + key) mod alphabet_size
PlainText = D(Cipher) = (Cipher – key) mod alphabet_size
Explanation / Answer
/***The java program that prompts user to enter the password
* and re enter the same password for validity of password.
* If password not valid, print the type of error in the password.
* if password matches, then display mode of encryption
* Then encrypt or decrypt the password */
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
//Scanner class object
Scanner scanner=new Scanner(System.in);
String password;
String matchingPassword;
int key;
System.out.println("Please enter a password:");
//read password
password=scanner.nextLine();
System.out.println("Please enter the same password again:");
//read matching password
matchingPassword=scanner.nextLine();
//Check for validity of password
if(isValid(password,matchingPassword))
{
System.out.println("Password verified!");
System.out.println("Enter a key value to encrypt");
//read key of encryption
key=Integer.parseInt(scanner.nextLine());
System.out.println("Ceaser Cypher ");
System.out.println("Encryption-0");
System.out.println("Decryption-1");
System.out.println("Enter 0 for encryption");
int mode=Integer.parseInt(scanner.nextLine());
String encryptedPwd="";
//Check if mode is 0 then call encryption
if(mode==0)
{
encryptedPwd=encrypt(password, key);
encryption(password,key,mode);
}
System.out.println("Enter 1 for decryption");
mode=Integer.parseInt(scanner.nextLine());
//Check if mode is 1 then call deecryption
if(mode==1)
decryption(encryptedPwd,key,mode);
else
System.out.println("Invalid entry");
}
else
System.out.println("Password not verified!");
}
//helper method
private static void decryption(String password, int key, int mode) {
System.out.println("Decryption Password : "+decrypt(password, key));
}
//helper method
private static void encryption(String password, int key, int mode) {
System.out.println("Encrypted Password : "+encrypt(password, key));
}
//encryption method
private static String decrypt(String encryptPwd, int key) {
String encryptedMessage="";
for (int i = 0; i < encryptPwd.length(); i++) {
encryptedMessage+=(char)(((int)(encryptPwd.charAt(i)-key))%126);
}
return encryptedMessage;
}
//decryption method
private static String encrypt(String password, int key) {
String encryptedMessage="";
for (int i = 0; i < password.length(); i++) {
encryptedMessage+=(char)(((int)password.charAt(i)+key)%126);
}
return encryptedMessage;
}
//Method to check for validity
private static boolean isValid(String password, String matchingPassword) {
boolean valid=true;
if(password.length()>=8 && matchingPassword.length()>=8
&&!password.equals(matchingPassword))
{
if(!containsDigit(password))
{
valid=false;
System.out.println("Password does not contain a digit");
}
if(!containsUpper(password))
{
valid=false;
System.out.println("Password does not contain an upper case character.");
}
if(!containsLower(password))
{
valid=false;
System.out.println("Password does not contain an lower case character.");
}
if(!containsSpecial(password))
{
valid=false;
System.out.println("Password does not contain a special character.");
}
return valid;
}
else if(password.length()<8 && matchingPassword.length()<8)
{
System.out.println("Password must be at least 8 characters.");
return false;
}
return true;
}
//Helper method
private static boolean containsSpecial(String passwd) {
int splCount=0;
for (int i = 0; i < passwd.length(); i++) {
switch(passwd.charAt(i))
{
case '!':
case '@':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '_':
case '-':
splCount++;
break;
}
}
if(splCount>=1)
return true;
else
return false;
}
private static boolean containsLower(String passwd) {
int lowerCount=0;
for (int i = 0; i < passwd.length(); i++) {
if(passwd.charAt(i)>='a' &&passwd.charAt(i)<='z')
lowerCount++;
}
if(lowerCount>=1)
return true;
else
return false;
}
private static boolean containsUpper(String passwd) {
int upperCount=0;
for (int i = 0; i < passwd.length(); i++) {
if(passwd.charAt(i)>='A' &&passwd.charAt(i)<='Z')
upperCount++;
}
if(upperCount>=1)
return true;
else
return false;
}
private static boolean containsDigit(String passwd) {
int digitCount=0;
for (int i = 0; i < passwd.length(); i++) {
if(passwd.charAt(i)>='0' &&passwd.charAt(i)<='9')
digitCount++;
}
if(digitCount>=1)
return true;
else
return false;
}
}
--------------------------------------------------------------------------------------------------------------------
Sample otput:
Please enter a password:
TacoTuesday7!
Please enter the same password again:
TacoTuesday7!
Password verified!
Enter a key value to encrypt
1
Ceaser Cypher
Encryption-0
Decryption-1
Enter 0 for encryption
0
Encrypted Password : UbdpUvftebz8"
Enter 1 for decryption
1
Decryption Password : TacoTuesday7!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.