DES là gì? Code ví dụ DES bằng Java

DES là gì? Code ví dụ DES bằng Java.

(Code ví dụ mã hóa với RSA)

(Code ví dụ mã hóa với AES)

 DES là gì?

Data Encryption Standard (DES) là một thuật toán mã hóa dữ liệu.

Đặc điểm:

  • Là dạng mã hóa khối, kích thước khối vào 64 bít
  • Khóa 64 bít, trong đó thực sử dụng 56 bít, 8 bít dùng cho kiểm tra chẵn lẻ
  • DES sử dụng chung một giải thuật cho mã hóa và giải mã.

Hiện nay DES không được coi là an toàn do:

  • Không gian khóa nhỏ (khóa 64 bít, trong đó thực sử dụng 56 bít)
  • Tốc độ tính toán của các hệ thống máy tính ngày càng nhanh.

DES là loại mã hóa đối xứng (mã hóa khóa bí mật), sử dụng một khóa bí mật duy nhất cho cả quá trình mã hóa và giải mã

(Ở bài này mình tập trung vào cài đặt ví dụ mã hóa và giải mã DES, còn chi tiết về DES thì nó khá dài, mình sẽ viết riêng một bài khác, hoặc các bạn có thể tham khảo chi tiết tại:

https://en.wikipedia.org/wiki/Data_Encryption_Standard

http://tryingshare123.blogspot.com/2014/09/java-security-phan-2-secret-key.html)

Ví dụ mã hóa, giải mã với DES

1. Tạo key (tạo khóa mã hóa/giải mã)

String SECRET_KEY = "12345678";
SecretKeySpec skeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "DES");

2. Cipher Info

Tạo một đối tượng Cipher (đối tượng này dùng để mã hóa, giải mã) và chỉ rõ các thông tin:

  • Tên thuật toán
  • Mode (tùy chọn)
  • Padding scheme (tùy chọn)
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5PADDING");
Note:
DES = Data Encryption Standard.
ECB = Electronic Codebook mode.
PKCS5Padding = PKCS #5-style padding.

 3. Mã hóa

String original = "stackjava.com";

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] byteEncrypted = cipher.doFinal(original.getBytes());
String encrypted =  Base64.getEncoder().encodeToString(byteEncrypted);

Khi thực hiện mã hóa hay giải mã nó sẽ thực hiện trên byte[] (ở đây mình ví dụ mã hóa text nên chuyển text sang byte, các bạn có thể mã hóa tương tự với file)

Ở đây mình chuyển byte sang dạng base64 để hiển thị.

4. Giải mã

cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] byteDecrypted = cipher.doFinal(byteEncrypted);
String decrypted = new String(byteDecrypted);

5. Demo

package stackjava.com.demodes.main;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class DemoDES {
  public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    String SECRET_KEY = "12345678";
    SecretKeySpec skeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "DES");
    
    String original = "stackjava.com";
    
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] byteEncrypted = cipher.doFinal(original.getBytes());
    String encrypted =  Base64.getEncoder().encodeToString(byteEncrypted);
    
    
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] byteDecrypted = cipher.doFinal(byteEncrypted);
    String decrypted = new String(byteDecrypted);
    
    System.out.println("original  text: " + original);
    System.out.println("encrypted text: " + encrypted);
    System.out.println("decrypted text: " + decrypted);
    
  }
}

Kết quả:

original  text: stackjava.com
encrypted text: xvHjRlZOjIxmi+R3h4/gUw==
decrypted text: stackjava.com

DES là gì? Code ví dụ DES bằng Java

DES là gì? Code ví dụ DES bằng Java

Okay, Done!
Download code ví dụ trên tại đây

stackjava.com