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

Project Requirements: 1.Write a Java program called Password that handles encryp

ID: 3668305 • Letter: P

Question

Project Requirements:

1.Write a Java program called Password that handles encrypting and decrypting a password.

2.The program must perform encryption as follows:

a.Ask the user for a password.

b.Check to ensure the password is a least 8 characters long. If it is not, then print the following error message and exit the program:

The password must be at least 8 characters long, your password is only X characters long. (X is the length of the current password)

c.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 exit the program:

The key must be between 1 and 10, you entered is X. (X is the number entered)

d.Encrypt the password using the formula below.

e.Display the password and the encrypted password in a nicely formatted String such as:

Your password is letmeiz the encrypted version of your password is rkzsko% with a key of 6.

3.The program must perform decryption as follows:

a.Ask the user for their encrypted password

b.Ask the user the key used to generate the password.

c.Decrypt the password using a reverse of the encryption formula

d.Display the encrypted password and the password in a nicely formatted String such as:

Your encrypted password is Qvlztp!o8 the password is Joesmith1 with a key of 7.

4.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.

The shift for this program will be the key entered by the user.

The password may be any of the characters in the 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).  

Explanation / Answer

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class EncryptDecrypt {

   public static void main(String[] args) throws Exception {

       System.out.println("Enter Your Choice : 1.Encrypt 2.Decrypt");
       BufferedReader reader = new BufferedReader(new InputStreamReader(
               System.in));
       int choice = Integer.parseInt(reader.readLine());
       switch (choice) {
       case 1:
           encrypt(reader);
           break;

       case 2:
           decrypt(reader);
           break;
       default:
           System.out.println("Wrong choice!");

       }
   }

   private static void encrypt(BufferedReader reader) throws Exception {
       System.out.println("Enter your password for encryptin : ");
       String password = reader.readLine();
       if (!(password.length() >= 8)) {
           System.out
                   .println("The password must be at least 8 characters long, your password is only "
                           + password.length() + " characters long.");
           return;
       }
       System.out.println("Enter key encrypt : ");
       int key = Integer.parseInt(reader.readLine());
       if (key < 1 && key > 10) {
           System.out
                   .println("The key must be between 1 and 10, you entered is "
                           + key);
           return;
       }
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < password.length(); i++) {
           int temp = (int) password.charAt(i);
           int enc = temp + key;
           if(enc>122){
               enc-=90;
           }
           sb.append((char) enc);
       }
       System.out.println("Your password is " + password
               + " the encrypted version of your password is " + sb
               + " with a key of " + key);
   }

   private static void decrypt(BufferedReader reader) throws Exception {
       System.out.println("Enter your encrypted password for decryption : ");
       String encryptedPassword = reader.readLine();
       System.out.println("Enter key encrypt : ");
       int key = Integer.parseInt(reader.readLine());
       if (key < 1 && key > 10) {
           System.out
                   .println("The key must be between 1 and 10, you entered is "
                           + key);
           return;
       }

       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < encryptedPassword.length(); i++) {
           int temp = (int) encryptedPassword.charAt(i);
           int enc = temp - key % 89;
           if (enc < 0) {
               enc += 90;
           }
           sb.append((char) enc);
       }
       System.out.println("Your password is " + encryptedPassword
               + " the encrypted version of your password is " + sb
               + " with a key of " + key);
   }

}