Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 4 của 4
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    JAVA_Mã hóa 1 ArrayList Trong File,Có cách nào tốt hơn?

    -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!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Sao ko serialize nó ra dãy byte nhỉ [IMG]images/smilies/biggrin.png[/IMG]
    p/s: giờ này ai dùng DES nhỉ, DES lấy card AMD quay mấy vòng là ra rồi [IMG]images/smilies/smile.png[/IMG]

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    quay ra kiểu gì vậy ví dụ cái[IMG]images/smilies/online.gif[/IMG]

  4. #4
    Ngày tham gia
    Feb 2014
    Bài viết
    0
    Không biết các cty làm phần mềm họ mã hóa các file dữ liệu của mình ntn nhỉ?
    -Một array dữ liệu họ lấy ra từng phần tử sau đó ghép hết lại với nhau vs 1 dấu hiệu phân cách như "00900" sau đó mã hóa string rồi lưu ra file?
    -Hay là mã hóa cả Arr bằng một cách nào đó??
    -Hay là họ có cách mã hóa theo một phương thức nào đó??
    Ai đã đi làm ở một cty uy tín và chuyên về security vào khai sáng cho em với,thank rất nhiều ạ!!!
    UP UP UP.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •