I got confused in an assignment 😀
given a UML diagram I had to Implement a course management application, one part is the Student class as follow
public class Student extends Person implements Encryptable {
private String program;
private Vector<Course> courses;
//constructor.
public Student(String Name, String pnr, String tel, String prog) {
super(Name, pnr, tel);
program = prog;
courses = new Vector<Course>();
}
/* implementation of related methods
...... */
@Override
public void encrypt(String password) {
}
@Override
public void decrypt(String password) {
}
all other classes and all related setters and getters of this class are implemented also the Encryptable interface is as follow
public interface Encryptable {
public void encrypt(String password);
public void decrypt(String password);
}
in an earlier task I had implemented a class “PassworCrypter” that I should use that class
public class PasswordCrypter {
Cipher ecipher;
Cipher dcipher;
SecretKey key;
DESKeySpec dks;
SecretKeyFactory skf;
byte[] psword;
public PasswordCrypter(String password) {
try {
psword = password.getBytes("UTF-16");
dks = new DESKeySpec(psword);
skf = SecretKeyFactory.getInstance("DES");
key = skf.generateSecret(dks);
ecipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher = Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchAlgorithmException e) {
throw new CrypterException(e);
} catch (NoSuchPaddingException e) {
throw new CrypterException(e);
} catch (InvalidKeyException e) {
throw new CrypterException(e);
} catch (InvalidKeySpecException e) {
throw new CrypterException(e);
} catch (UnsupportedEncodingException e) {
throw new CrypterException(e);
}
}
public byte[] encrypt(byte[] array) {
try {
return ecipher.doFinal(array);
} catch (IllegalBlockSizeException e) {
throw new CrypterException(e);
} catch (BadPaddingException e) {
throw new CrypterException(e);
}
}
public byte[] decrypt(byte[] array) {
try {
return dcipher.doFinal(array);
} catch (IllegalBlockSizeException e) {
throw new CrypterException(e);
} catch (BadPaddingException e) {
throw new CrypterException(e);
}
}
}
I should use PasswordCrypter class on local and inherited fields of the Student
class. When a Student object is encrypted it should not be possible to get data
such as a student’s name without first calling decrypt. A Student object should
always be encrypted except for when you need to access any of its data.
can any one give me idea or tell me how should I encrypt this damn student 🙂
I think you have problem in
Encryptable. The signature of the method should beWhere you can define two separate interfaces
EncrypterandDecrypter. so finally you can usePasswordCrypteras central class which implements these two methods.So now inside your Student class you can do