This project will use The Vigenere Cipher to encrypt passwords. Simple letter su
ID: 3881429 • Letter: T
Question
This project will use The Vigenere Cipher to encrypt passwords.
Simple letter substitution ciphers are ones in which one letter is substituted for another. Although their output looks impossible to read, they are easy to break because the relative frequencies of English letters are known.
The Vigenere cipher improves upon this. They require a key word and the plain text to be encrypted.
Create a Java application that uses:
decision constructs
looping constructs
basic operations on an ArrayList of objects (find, change, access all elements)
more than one class and has multiple objects.
User Class
The class that contains user information including the username, password, encrypted password and key (see UML class diagram below)
User
username : String
clearPassword : String
encryptedPassword : String
key : String
+User()
+User(String, String, String)
+getUserName() : String
+setUserName(String)
+getClearPassword(): String
+setClearPassword(String)
+getEncryptedPassword():String
+setEncryptedPassword(String)
+getKey(): String
+setKey(String)
-encrypt()
+toString():String
Constructors
Default constructor sets all fields to the empty String (“”)
Parameterized constructors
takes in the username, clearPassword and the key
calls the private method encrypt to encrypt the clearPassword using the Vigenere Cypher.
Methods
Accessor and mutator methods as listed in the Class Diagram
encrypt method
Private method – only accessible to other methods of the User class.
Uses the Vigenere Cypher to encrypts the clearPassword instance variable using the key
Stores the encrypted password in encryptPassword instance variable.
toString – returns a nicely formatted String representing the user to include username, encrypted password, clear password and key such as:
Jsmith ]Umka^ password house
Accounts Class
This class contains all the user objects for our company.
The class’s instance variable includes company name, company address, and an ArrayList of User Objects (see UML class diagram below)
Accounts
companyName : String
companyAddress : String
users : ArrayList
NOTFOUND: int = -1
+Accounts()
+Accounts(String, String)
+getCompanyName(): String
+setCompanyName(String)
+getCompanyAddress(): String
+setCompanyAddress(String)
+addUser(User)
+getUser(String): User
+deleteUser(String):boolean
-findUser(String):int
+toString(): String
Instance Variables
companyName – name of company
companyAddress – the address of the company
users – an ArrayList containing User Objects
Constructors
Default constructor sets company name and address to the empty String and creates an ArrayList of User objects
Parameterized constructor sets the name and address parameters to the appropriate instance variable and creates the ArrayList of User Objects.
Methods
Accessor and mutator methods as listed in the Class Diagram
addUser
Takes in a User object
Adds that User object to the ArrayList
getUser
Takes in a String with the user’s username
Calls the private findUser method to locate the index of the User object in the ArrayList.
Returns the User object from the ArrayList that is associated with that username. (assume unique username)
If the username does not exist
print out the error message: does not exist.
Return null
deleteUser
Takes in a String with the user’s username
Calls the private findUser method to locate the index of the User object in the ArrayList
Returns true if user found and deleted, false if user not found.
findUser
Private method that takes in a String representing a username
Searches the ArrayList looking for this username (again assume usernames are unique)
If found, returns the ArrayList index of the username
If not found returns NOTFOUND constant.
toString
returns a nicely formatted String table representing all the users information in the ArrayList(see example below)
use of the User toString method is advisable.
ABC Company 1234 Holly Lane, Pensacola Florida
Username EncryPass ClearPass Key
Jsmith ]Umka^ password house
mjones GYOX)r*z abcd1234 argos
AccountTester
The purpose of this class is to thoroughly test all the methods and constructors in the Accounts and User classes.
Methods can be tested directly or indirectly
Indirect testing is testing a method by calling another method such as
If the constructor’s calls the mutator methods when setting instance variables rather than setting them directly.
If the toString method calls accessor method rather than accessing instance variables directly
No user input, just create variables and/or hardcode values
To simplify the program some, No error handling required
All passwords are 8 legal characters (as outlined in the requirements)
All keys are 5 characters
Program Flow (suggestion)
Create several User Objects
Add each to the Accounts class
Call the Accounts toString
Search for username including one that is not found.
Delete a user account.
Call Account toString before exiting.
Programming/Client Communication
When developing a program requirement are rarely perfect, ambiguities, omissions and errors are usually present.
To keep this communication organized please post to the appropriate Programming Project Topic any question you may have. I will answer those question there and thus we will have a written record.
If you ask a question in class, I will answer it but you need to post the question as well.
Submission Requirements:
Your project must be submitted using the instructions below. Any submissions that do not follow the stated requirements will not be graded.
1. You should have the following files for this assignment:
User.java - The User class
Accounts.java – The collection class for User information.
AccountTester.java – The testing class for this project
User
username : String
clearPassword : String
encryptedPassword : String
key : String
+User()
+User(String, String, String)
+getUserName() : String
+setUserName(String)
+getClearPassword(): String
+setClearPassword(String)
+getEncryptedPassword():String
+setEncryptedPassword(String)
+getKey(): String
+setKey(String)
-encrypt()
+toString():String
Explanation / Answer
/***********************************************User.java************************************************************/
package cipher;
/**
* The Class User.
*/
public class User {
/** The user name. */
private String userName;
/** The clear password. */
private String clearPassword;
/** The encrypted password. */
private String encryptedPassword;
/** The key. */
private String key;
/**
* Instantiates a new user.
*/
public User() {
this.userName = "";
this.clearPassword = "";
this.encryptedPassword = "";
this.key = "";
}
/**
* Instantiates a new user.
*
* @param userName
* the user name
* @param clearPassword
* the clear password
* @param key
* the key
*/
public User(String userName, String clearPassword, String key) {
super();
this.userName = userName;
this.clearPassword = clearPassword;
this.key = key;
}
/**
* Gets the user name.
*
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* Sets the user name.
*
* @param userName
* the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* Gets the clear password.
*
* @return the clearPassword
*/
public String getClearPassword() {
return clearPassword;
}
/**
* Sets the clear password.
*
* @param clearPassword
* the clearPassword to set
*/
public void setClearPassword(String clearPassword) {
this.clearPassword = clearPassword;
}
/**
* Gets the encrypted password.
*
* @return the encryptedPassword
*/
public String getEncryptedPassword() {
return encryptedPassword;
}
/**
* Sets the encrypted password.
*
* @param encryptedPassword
* the encryptedPassword to set
*/
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
/**
* Gets the key.
*
* @return the key
*/
public String getKey() {
return key;
}
/**
* Sets the key.
*
* @param key
* the key to set
*/
public void setKey(String key) {
this.key = key;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.userName + " " + this.encryptedPassword + " " + this.clearPassword + " " + this.key;
}
/**
* Encrypt.
*/
public void encrypt() {
String res = "";
String password = this.clearPassword.toUpperCase();
for (int i = 0, j = 0; i < this.clearPassword.length(); i++) {
char c = password.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
this.encryptedPassword = res;
}
}
/********************************************Accounts.java********************************************************/
import java.util.ArrayList;
/**
* The Class Accounts.
*/
public class Accounts {
/** The company name. */
private String companyName;
/** The company address. */
private String companyAddress;
/** The users. */
private ArrayList<User> users;
/** The notfound. */
private int NOTFOUND = -1;
/**
* Instantiates a new accounts.
*/
public Accounts() {
this.companyName = "";
this.companyAddress = "";
this.users = new ArrayList<>();
}
/**
* Instantiates a new accounts.
*
* @param companyName the company name
* @param companyAddress the company address
*/
public Accounts(String companyName, String companyAddress) {
super();
this.companyName = companyName;
this.companyAddress = companyAddress;
this.users = new ArrayList<>();
}
/**
* Gets the company name.
*
* @return the companyName
*/
public String getCompanyName() {
return companyName;
}
/**
* Sets the company name.
*
* @param companyName
* the companyName to set
*/
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
/**
* Gets the company address.
*
* @return the companyAddress
*/
public String getCompanyAddress() {
return companyAddress;
}
/**
* Sets the company address.
*
* @param companyAddress
* the companyAddress to set
*/
public void setCompanyAddress(String companyAddress) {
this.companyAddress = companyAddress;
}
/**
* Gets the users.
*
* @return the users
*/
public ArrayList<User> getUsers() {
return users;
}
/**
* Sets the users.
*
* @param users
* the users to set
*/
public void setUsers(ArrayList<User> users) {
this.users = users;
}
/**
* Adds the user.
*
* @param user the user
*/
public void addUser(User user) {
this.users.add(user);
}
/**
* Gets the user.
*
* @param username the username
* @return the user
*/
public User getUser(String username) {
int index = findUser(username);
if (index == -1) {
System.out.println("does not exist.");
return null;
} else {
return this.users.get(index);
}
}
/**
* Find user.
*
* @param username the username
* @return the int
*/
public int findUser(String username) {
int i = 0;
for (User user : this.users) {
if (username.equals(user.getUserName())) {
return i;
}
i++;
}
return NOTFOUND;
}
/**
* Delete user.
*
* @param username the username
* @return true, if successful
*/
public boolean deleteUser(String username) {
int index = findUser(username);
if (index != -1) {
this.users.remove(index);
return true;
} else {
return false;
}
}
@Override
public String toString() {
return this.companyName + " " + this.companyAddress;
}
}
/**********************************************AccountTester.java*************************************************/
/**
* The Class AccountTester.
*/
public class AccountTester {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
User user = new User("Jsmith", "password", "house");
user.encrypt();
User user1 = new User("mjones", "abcd1234", "argos");
user1.encrypt();
Accounts accounts = new Accounts("ABC Company","1234 Holly Lane, Pensacola Florida");
accounts.addUser(user);
accounts.addUser(user1);
System.out.println(accounts.toString());
System.out.println("Username EncryPass ClearPass Key");
System.out.println(user.toString());
System.out.println(user1.toString());
accounts.findUser("krishna");
accounts.deleteUser("mjones");
System.out.println(accounts.toString());
System.out.println("Username EncryPass ClearPass Key");
System.out.println(user.toString());
System.out.println(user1.toString());
}
}
/******************************************output*******************************************/
ABC Company 1234 Holly Lane, Pensacola Florida
Username EncryPass ClearPass Key
Jsmith CUSQGBLD password house
mjones GYOX abcd1234 argos
ABC Company 1234 Holly Lane, Pensacola Florida
Username EncryPass ClearPass Key
Jsmith CUSQGBLD password house
mjones GYOX abcd1234 argos
Thanks a lot. Please let me know if you have any doubt. HIT LIKE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.