Thứ Ba, 2 tháng 9, 2014

[Java Security] Phần 3 : Ví dụ về thuật toán Secret-key bằng JAVA

Phần 3 : Ví dụ về thuật toán Secret-key bằng JAVA
Yêu cầu lập trình  :
- Dùng 1 mật mã (password) tùy thích để mã hóa nội dung trong tập tin plaintext.txt và nội dung mã hóa được lưu lại trong tập tin cipher.txt.
- Cũng dùng mật mã trên để giải mã nội dung trong tập tin cipher.txt và lưu nội dung giải mã này trong tập tin plaintext1.txt
Trước khi lập trình phải bảo đảm trong máy tính của bạn đã có:
- Java Development Kid (JDK).
- Java Cryptography Extension (JCE).
I. Thuật toán DES :
a. Tạo 1 DES Key dựa trên password :
Password chỉ là 1 chuỗi ký tự bình thường nên tính bảo mật không cao. Do đó, khi sử dụng một thuật toán mã hóa mào đó, password phải được mã hóa theo một dạng nhật định được gọi là Key, trong trường hợp sử dụng thuật toán DES nên gọi là DES Key.
Các lớp của gói JCE sẽ được trình bày chi tiết ở những phần sau, nên tạm thời chúng ta chỉ cần nói đại khái cách tạo ra 1 DesKey:
- Đầu tiên  pasword được chuyển thành mảng byte.
- Sử dụng class DESKeySpec tạo ra DES Key .
- Sử dụng  class SecretKeyFactory để chỉ ra thuật toán nào dùng cho mã hóa key.
- class SecretKey chứa kết quả của quá trình mã hóa key .  
Lưu ý : chuỗi password phải ích nhất 8 kí tự trở lên.
b. Mã hóa nội dung tập tin plaintext.txt:
c. Giải mã nội dung tập tin ciphertext.txt:
Code hoàn chỉnh :
import java.io.*;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.PBEKeySpec;

public class DESEncryptConsoleApp1 {

       public static void main(String[] args) {
                String help = " Su dung DES de ma hoa tap tin plaintext.txt";
                System.out.println(help);
               try {
                       desEncrypt("plaintext.txt", "ciphertext1.txt", "adfddfds");
                       desDecrypt("ciphertext1.txt", "plaintext1.txt", "adfddfds");
               } catch (Exception e) {
                       System.err.println(e.getLocalizedMessage());
               }
       }

       public static SecretKey secretkey(String pass) throws Exception {
               byte[] passbyte = pass.getBytes();
               DESKeySpec keyspec = new DESKeySpec(passbyte);
               SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
               SecretKey Deskey = keyFactory.generateSecret(keyspec);

               return Deskey;
       }

       public static void desEncrypt(String f1, String f2, String pass) throws Exception {
               SecretKey DesKey = secretkey(pass);
               
               // Create a cipher
               Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, DesKey);

// Read and encrypt file.
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1));
CipherOutputStream out = new CipherOutputStream(
                         new BufferedOutputStream(new FileOutputStream(f2)),cipher);

// Read the file and encrypt
int i;
do {
i = in.read();
if (i != -1)    out.write(i);
} while (i != -1);
in.close();
out.close();
}
public static void desDecrypt(String f1, String f2, String pass) throws Exception {
// Open the key file -- It must be in the current directory.
SecretKey DesKey = secretkey(pass);

// Create a cipher
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, DesKey);

// Read and decrypt file.
CipherInputStream in = new CipherInputStream(
                                   new BufferedInputStream(new FileInputStream(f1)),cipher);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
int i;
do {
i = in.read();
if (i != -1) out.write(i);
} while (i > 0);
in.close();
out.close();
}
}
}

II. Thuật toán DESede : tương tư như DES, chỉ thay đổi chút ích trong code.
DES
DESede
DESKeySpec keyspec =
                   new DESKeySpec(passbyte);
DESedeKeySpec keyspec =
                      new DESedeKeySpec(passbyte);
SecretKeyFactory keyFactory =
       SecretKeyFactory.getInstance("DES");
SecretKeyFactory keyFactory =
      SecretKeyFactory.getInstance("DESede");
Cipher cipher = Cipher.getInstance("  
                 DES/ECB/PKCS5Padding");
Cipher cipher = Cipher.getInstance("
                     DESede/ECB/PKCS5Padding");
Các đoạn code khác hoàn toàn giống nhau.
Lưu ý : chuỗi password trong DESede phải có ích nhất 32 ký tự trở lên.
III.Thuật toán Blowfish : không sử dụng được trong bài “yêu cầu lập trình” này bởi:
Sun không có lớp KeySpec để chỉ ra đặc tính của key dành cho Blowfish. Chúng ta chỉ có thể tạo ra Blowfish key bằng cách mặc định :
Sau đó lưu Key này vào trong 1 file nào đó, khi mã hóa cũng như giải mã đọc file đó để lấy lại key.
DES
Blowfish
DESKeySpec key spec =
                       new DESKeySpec(passbyte);
Không có class BlowfishSpecKey
SecretKeyFactory keyFactory =
        SecretKeyFactory.getInstance("DES");
SecretKey Deskey =
           keyFactory.generateSecret(keyspec);
KeyGenerator keygen =
              KeyGenerator.getInstance("Blowfish");
SecretKey  key = keygen.generateKey();
Cipher cipher = Cipher.getInstance("
                 DES/ECB/PKCS5Padding");
 Cipher cipher = Cipher.getInstance("
                       Blowfish/ECB/PKCS5Padding");


Không có nhận xét nào:

Đăng nhận xét