Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Java program that: creates a user designed class uses proper design te

ID: 3674661 • Letter: D

Question

Develop a Java program that: creates a user designed class uses proper design techniques including reading UML Class Diagrams reads input from the keyboard using a Scanner Object and its methods uses selection (if and if else) statements uses iteration (while, do while or for) statements uses String comparison methods. follows standard acceptable programming practices. handles integer overflow errors Prep Readings: Absolute Java Chapter 1 – 4 and Security Injection Labs (Integer Error and Input Validation) Background Information: The Unified Modeling Language (UML) provides a useful notation for designing and developing object-oriented software systems. One of the basic components of the UML is a class diagram, which are used to depict the attributes and behaviors of a class. A basic class diagram (as shown in the figure below) has three components. The first is the class name. The second component includes the class's attributes or fields. Each attribute is followed by a colon (:) and its data type. The third component includes the class's behaviors or methods. If the method takes parameters, their types are included in parentheses. Each behavior is also followed by a colon (:) and its return type. If the return value of a method is void, the return type can be omitted. For more information on the UML, refer to HYPERLINK "http://www.uml.org/" http://www.uml.org/. Project Requirements: This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. The Encryption class: (Additionally See UML Class Diagram) Instance Variables Key – Integer representing the password key encryptedPassword – String representing the encrypted password. Constructors Default (no parameter) Constructor – sets all instance variables to a default value Parameterized Constructor Takes in a key and the clear text password as parameters Set the instance variable key from the associated parameter Call the encrypt method to encrypt the clear text password. Methods encrypt Takes in a clear text password as a parameter Uses the instance variable key to encrypt the password. Stores the encrypted password in the encryptedPassword instance variable isValidPassword Take in a clear text password as a parameter Compares the clear text password with the stored encryptedPassword (Hint you must first encrypt the clear text password parameter) Returns true if the passwords match, false if they don’t. getEncryptedPassword – returns the encrypted password. setKey – takes in a new key as a parameter and sets the instance variable key to that value. getKey – returns the instance variable: key toString – returns a nicely formatted String in the form below use the getKey and getEncryptedPassword method to retrieve those values, do not use the instance variable directly. The encrypted password is UeiY3$2Spw. The used to generate this password is 3. Encryption - key : int - encryptedPassword : String + Encryption() + Encryption (int, String) + encrypt(String) + isValidPassword(String) : boolean + getEncryptedPassword() : String + setKey(int) + getKey() : int + toString(): String Password Class This class is very similar to Project 3 however there is some additional error handling and the use of the Encryption class. Ask the user for a password Check to ensure the password is a least 8 characters long. If it is not, then print the following error message and ask the user to enter a password that is at least 8 characters, continue asking until they enter a valid password: The password must be at least 8 characters long, your password is only X characters long. (X is the length of the current password) Ask the user a number between 1 and 10 (Inclusive) that will be used as the encryption key. If it is not between 1 and 10, then print the following error message and ask the user to enter a key that is between 1 and 10 continue asking until they enter a valid password. The key must be between 1 and 10, you entered is X. (X is the number entered) Create two objects of the Encryption class: Create the first Encryption class object using the default constructor Create the second Encryption class object using the parameterized constructor (pass the password and key entered by the user as its parameters)(Remember the parametrized constructor calls the encryption method within the Encryption class) Call the second object’s toString and display the results. Test the encrypt method Prompt the user to enter their password Calls the isValidPassword method using this clear text password as its parameter Display a message indicating the results of password validation. Test all other methods of the Encryption class that haven’t already been tested, such as setKey. Prompt the user if they would like to run the program and if so restart the program. Encryption Algorithm Implement a simple shift cipher. A shift cipher is a form of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with shift of 3, A would be replaced by D, B would become E, and so on. 320px-Caesar3 The shift for this program will be the key entered by the user. The password may be any of the characters in the HYPERLINK "http://www.asciitable.com/" ASCII table from character 33 the exclamation point ‘!’ to character 122 the lower case ‘z’. If the shift cause the character to be greater than 122, the shift will continue starting with character 33. Example: If the user entered 8 as the key and a letter in the password is lower case ‘x’ (char 120) a shift of 8 would result in the character ‘&’ (char 38). Include javadoc class comment following Project Comment Template on the content page. The project requires two files to be turned in - Password.java and Encrytion.java (the source code of your Password program) Be sure to follow the Programming Project Submission Instructions posted by your instructor on the elearning site. The submission requirements are part of the grading for this assignment.

Explanation / Answer

Encryption.java

public class Encryption {

private int key;

private String encryptedPassword;

  

   public Encryption()

   {

       key=-1;

       encryptedPassword="";

   }

  

   public Encryption(int encKey,String plainText)

   {

       key=encKey;

       encrypt(plainText);

   }

  

   public void encrypt(String plainText)

   {

       encryptedPassword="";

       for(int i=0;i<plainText.length();++i)

       {

           int ascii=(int)plainText.charAt(i);

           int encAscii=ascii+key;

           if(encAscii>122)

           {

               encAscii=(encAscii)%122+32;

           }

           char encChar=(char)encAscii;

           String x=Character.toString(encChar);

           encryptedPassword=encryptedPassword.concat(x);

       }

   }

  

   public boolean isVaidPassword(String plainText)

   {

       String pass=encryptedPassword;

       encrypt(plainText);

       if(pass.equals(encryptedPassword))

       {

           return true;

       }

       return false;

   }

  

   public String getEncryptedPassword()

   {

       return encryptedPassword;

   }

   public int getKey() {

       return key;

   }

   public void setKey(int key) {

       this.key = key;

   }

  

   public String toString()

   {

       return "The encrypted password is "+this.getEncryptedPassword()+

               " The key used to generate this password is "+this.getKey();

   }

}

Password.java

import java.util.Scanner;

public class Password {

   public static void main(String[] args) {

       encryptionTester();

   }

  

   public static void encryptionTester()

   {

       Scanner sc=new Scanner(System.in);

       String pass;

       int key=-1;

       //for again and again re prompting if password is not validS

       while(true)

       {

           System.out.println("Enter the password");

           pass=sc.nextLine();

           if(pass.length()<8)

           {

               System.out.println("Enter a valid password with length greater than 8 characters ");

               continue;

           }

           break;

       }

       while(true)

       {

           System.out.println("Enter the key between 1 and 10");

           key=sc.nextInt();

           if(key>=1 && key<=10)

           {

               break;

           }

           System.out.println("The key must be between 1 and 10, you entered is "+key);

       }

      

       Encryption enc1=new Encryption();

       Encryption enc2=new Encryption(key,pass);

      

       System.out.println(enc2.toString());

      

       //checking the isValidPassword by taking a password input from user and

       //checking it against original password stored in encryption classS

       System.out.println("Enter your password");

       pass=sc.next();

       if(enc2.isVaidPassword(pass))

       {

           System.out.println("Password is correct");

       }

       else

       {

           System.out.println("Invalid password");

       }

      

       //testing method setKey()

       enc2.setKey(8);

       enc2.encrypt(pass);

       System.out.println(enc2.toString());

      

       //asking the user if he want's to continueS

       System.out.println("Do you want to re run the program,press y for yes and n for no");

       String c=sc.next();

       if(c.equals("y"))

       {

           encryptionTester();

       }

       else

       {

           return;

       }

   }

}

sample output:

Enter the password

Ceaser&82372

Enter the key between 1 and 10

4

The encrypted password is Giewiv*<67;6 The key used to generate this password is 4

Enter your password

Ceaser&82372

Password is correct

The encrypted password is Kmi!mz.@:;?: The key used to generate this password is 8

Do you want to re run the program,press y for yes and n for no

n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote