1.Write a Java program called Password that handles encrypting and decrypting a
ID: 3671640 • Letter: 1
Question
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 a left shift of 3, D would be replaced by A, E would become B, 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
Password.java
import java.util.Scanner;
public class Password {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the password :");
String password = scan.next();
if(password.trim().length() < 8){
System.out.println("Password must be greater than 8 characters");
System.exit(0);
}
System.out.println("Enter the key between 1 and 10 :");
int key = scan.nextInt();
if(key < 1 || key > 10){
System.out.println("Key must be between 1 and 10");
System.exit(0);
}
String encrypPass = encryptPassword(password, key);
System.out.println("Encrypt Password is : "+encrypPass);
System.out.println("Please enter the encrypted password :");
password = scan.next();
if(password.trim().length() < 8){
System.out.println("Password must be greater than 8 characters");
System.exit(0);
}
System.out.println("Enter the key between 1 and 10 :");
key = scan.nextInt();
if(key < 1 || key > 10){
System.out.println("Key must be between 1 and 10");
System.exit(0);
}
String decrypPass = decryptPassword(password, key);
System.out.println("Decrypt Password is : "+decrypPass);
}
public static String encryptPassword(String password, int key){
String encrypPass = "";
for(int i=0; i<password.length(); i++){
char ch = password.charAt(i);
char temp = '';
if(Character.isDigit(ch)){
int num = Integer.parseInt(String.valueOf(ch));
encrypPass = encrypPass + (num + key);
}
else{
int n = ((int)ch + key);
if(n <= 122){
temp = (char) n;
}
else{
temp = (char) (n - 122 + 31 +1);
}
encrypPass = encrypPass + String.valueOf(temp);
}
}
return encrypPass;
}
public static String decryptPassword(String password, int key){
String decrypPass = "";
for(int i=0; i<password.length(); i++){
char ch = password.charAt(i);
char temp = '';
if(Character.isDigit(ch)){
int num = Integer.parseInt(String.valueOf(ch));
decrypPass = decrypPass + (num - key);
}
else{
int n = ((int)ch - key);
if(n > 32){
temp = (char) n;
}
else{
temp = (char) (122 - (31 - n + 1));
}
decrypPass = decrypPass + String.valueOf(temp);
}
}
return decrypPass;
}
}
Output:
Please enter the password :
letmeiz1
Enter the key between 1 and 10 :
6
Encrypt Password is : rkzsko&7
Please enter the encrypted password :
rkzsko&7
Enter the key between 1 and 10 :
6
Decrypt Password is : letmeiz1
Please enter the password :
letmeiz1
Enter the key between 1 and 10 :
6
Encrypt Password is : rkzsko&7
Please enter the encrypted password :
Qvlztp!o8
Enter the key between 1 and 10 :
7
Decrypt Password is : Joesmith1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.