我在作业中感到困惑:D 给定一个 UML 图,我必须实现一个课程管理应用程序,其中一部分是 Student 类,如下所示
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) {
}
所有其他类以及该类的所有相关setter和getter也实现了Encryptable接口如下
public interface Encryptable {
public void encrypt(String password);
public void decrypt(String password);
}
在较早的任务中,我实现了一个类“PassworCrypter”,我应该使用该类
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);
}
}
}
我应该在 Student 类的本地和继承字段上使用 PasswordCrypter 类。当学生对象被加密时,如果不先调用解密,就不可能获得学生姓名等数据。除非您需要访问其任何数据,否则应始终对 Student 对象进行加密。谁能给我想法或告诉我应该如何加密这个该死的学生:)