-Các thư viện cần dùng:
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Mã:
Class SinhVien implements java.io.Serializable{
private string ten;
private int tuoi;
public string getTen(){
return ten;
}
public void setTen(string ten){
this.ten=ten;
}
public void getTuoi(){
return tuoi;
}
public void setTuoi(string tuoi){
this.tuoi=tuoi;
}
}
Class MaHoa{
private static final String stringkey = "keymahoa";
public ObjectOutputStream EngryptFunc(String filename) {
try {
//Create key:
byte key[] = stringkey.getBytes();
DESKeySpec deskey = new DESKeySpec(key);
SecretKeyFactory keyfac = SecretKeyFactory.getInstance("DES");
SecretKey secret = keyfac.generateSecret(deskey);
//Create Cipher
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, secret);
//Create Stream
FileOutputStream fos = new FileOutputStream(filename);
BufferedOutputStream bos = new BufferedOutputStream(fos);
CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
ObjectOutputStream oos = new ObjectOutputStream(cos);
return oos;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public ObjectInputStream DegryptFunc(String filename) {
try {
//Create Key
byte key[] = stringkey.getBytes();
DESKeySpec deskey = new DESKeySpec(key);
SecretKeyFactory keyfac = SecretKeyFactory.getInstance("DES");
SecretKey secret = keyfac.generateSecret(deskey);
//Create Cpher:
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.DECRYPT_MODE, secret);
//Create Stream:
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
CipherInputStream cis = new CipherInputStream(bis, desCipher);
ObjectInputStream ois = new ObjectInputStream(cis);
return ois;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void MaHoaDanhSachSinhVien(String filename, ArrayList<SinhVien> obj) {
try {
ObjectOutputStream oos = EngryptFunc(filename);
//Write Object
oos.writeObject(obj);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<SinhVien> GiaiMaDSSV(String filename) {
try {
File checkload = new File(filename);
if (checkload.exists()) {
ObjectInputStream ois = DegryptFunc(filename);
//Read From File
ArrayList<SinhVien> obj = (ArrayList<SinhVien>) ois.readObject();
ois.close();
return obj;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
=>Với hàm MahoaDanhsachSinhVien và GiaiMaDSSV ta có thể mã hóa/giải mã một danh sách SV ghi trong file.
=>Nhưng nếu yêu cầu mã hóa/giải mã danh sách giáo viên,môn học....thì phải tạo thêm nhiều hàm tương ứng với mỗi kiểu thuộc tính.
=>Câu hỏi là: Làm sao để hàm mã hóa và giải mã không phụ thuộc vào kiểu của thuộc tính,ví dụ kiểu thuộc tính ở đây là SinhVien???
...Hehe hy vọng có bro nào giải thích và chỉ giúp vấn đề này!
View more random threads:
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ