Multi-threaded Encryption in Java Homework Sample

The encryption used is very simple, a character shift of 5. You need to sleep for 5ms during the encrypt and decrypt to simulate the time take for a more complex algorithm. In normal code you might have if (x == 5) { y = x * 2; } and you could guarantee that y would be 10, but when dealing with multi-threaded code, x may have changed value between the time take to check if x is 5 and the subsequent doubling of x and storing in y. Use synchronized methods to ensure thread safety. For more Java programming assignments contact us for details.

Solution:

CipherShifter.java

public class CipherShifter extends Thread
{
private Password obj = null;
//default constructor setting the Password variable obj
public CipherShifter(Password obj)
{
this.obj=obj;
}

@Override
public synchronized void run() {
//calling cipher method written in Password class
obj.cipher();

}
}

DecrypterUtil.java

public class DecrypterUtil extends Thread {
private Password obj = null;
//default constructor setting the Password variable obj
public DecrypterUtil(Password obj) {
this.obj=obj;
}

@Override
public void run() {
//for loop that runs 10 times calling decrypt() function
for(int i=0;i<10;i++) {
try {
obj.decrypt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

EncrypterUtil.java

public class EncrypterUtil extends Thread {
private Password obj = null;
//default constructor setting the Password variable obj
public EncrypterUtil(Password obj) {
this.obj=obj;
}

@Override
public void run() {
//for loop that runs 10 times calling encrypt() function
for(int i=0;i<10;i++) {
try {
obj.encrypt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Password.java

public class Password
{
char array[];
//default constructor storing character array value of the string
//password in variable array
public Password(String password) {
array=password.toCharArray();
}
//showPassword() returning the string value of the array variable
public String showPassword()
{
return new String(array);
}
//encrypt() method made thread safe with “synchronized” keyword
public synchronized void encrypt() throws InterruptedException
{
//for loop for encrypting each character
for(int i=0;i<array.length;i++) {
//storing ASCII value of the character in variable k and increment it by 5
int k=array[i];
k=k+5;
//update the value in array
array[i]=(char)k;
}
//sleep for 5 milliseconds
Thread.sleep(5);
}

public synchronized void decrypt() throws InterruptedException
{
//for loop for decrypting each character
for(int i=0;i<array.length;i++) {
//storing ASCII value of the character in variable k and decrement it by 5
int k=array[i];
k=k-5;
//update the value in array
array[i]=(char)k;
}
//sleep for 10 milliseconds
Thread.sleep(10);
}
//cipher method used in CipherShifter class
public synchronized void cipher() {
for(int i=0;i<array.length;i++) {
//storing ASCII value of the character in variable k and decrement it by 1 if
//k is odd or increment it by 1 if k is even
int k=array[i];
if(k%2==0) {
k=k+1;
}
else {
k=k-1;
}
//update the value in array
array[i]=(char)k;
}
}
}

UserDecrypterUtil.java

public class UserDecrypterUtil extends Thread {
private UserDetails obj = null;
//default constructor setting the UserDetails variable obj
public UserDecrypterUtil(UserDetails obj) {
this.obj=obj;
}

@Override
public void run() {
//for loop that runs 10 times calling decrypt() function
for(int i=0;i<10;i++) {
try {
obj.decrypt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

UserDetails.java

public class UserDetails {

private char username[];
private Password password;
private boolean encrypted;
//default constructor setting username,password,encryted variables
public UserDetails(String username, String password) {
this.username=username.toCharArray();
this.password=new Password(password);
encrypted=false;
}
//showUserName() returns string value of username character array
public String showUserName()
{
return new String(username);
}
//showPassword() returns string value of password
public String showPassword()
{
return password.showPassword();
}
//getUserDetails() returns the username and password in required format
public String getUserDetails()
{
return showUserName()+”|”+showPassword();
}

public void encrypt() throws InterruptedException
{
synchronized(this) {
//if encrypted is true, then return
if(encrypted) {
return;
}
//Creating threadUser thread used for encrypting username
Thread threadUser=new Thread(new Runnable() {

@Override
public void run() {
for(int i=0;i<username.length;i++) {
int k=username[i];
k=k+5;
username[i]=(char)k;
}
try {
Thread.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

} );

//Creating threadPass thread used for encrypting password using encrypt() method
//of password variable
Thread threadPass=new Thread(new Runnable() {

@Override
public void run() {
try {
password.encrypt();
} catch (InterruptedException e) {
e.printStackTrace();
}

}

});

//starting both numbers
threadUser.start();
threadPass.start();
//ending both threads
threadUser.join();
threadPass.join();
//setting encrypted to true
encrypted=true;
}
}

public void decrypt() throws InterruptedException
{
//returns if encrypted is false that is, the username and password are
//already decrypted
synchronized(this) {
if(!encrypted) {
return;
}
//Creating threadUser thread used for decrypting username
Thread threadUser2=new Thread(new Runnable() {

@Override
public void run() {
for(int i=0;i<username.length;i++) {
int k=username[i];
k=k-5;
username[i]=(char)k;
}
try {
Thread.sleep(11);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

} );
//Creating threadPass thread used for decrypting password using decrypt() method
//of password variable
Thread threadPass2=new Thread(new Runnable() {

@Override
public void run() {
try {
password.decrypt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

});
//starting both threads
threadUser2.start();
threadPass2.start();
//closing both threads
threadUser2.join();
threadPass2.join();
//setting encrypted to false
encrypted=false;
}
}
}

UserEncrypterUtil.java

public class UserEncrypterUtil extends Thread {
private UserDetails obj = null;
//default constructor setting the UserDetails variable obj
public UserEncrypterUtil(UserDetails obj) {
this.obj=obj;
}

@Override
public void run() {
//for loop that runs 10 times calling encrypt() function
for(int i=0;i<10;i++) {
try {
obj.encrypt();
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
}