首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java DES 加密解密極簡實作(CBC 模式 + 無填充)

     編輯:Java 相關知識

雖然 AES 已成為對稱加密標準,但 DES 在舊系統維護、嵌入式裝置、低安全性場景仍有應用空間。本文提供最精簡的 Java DES 實作,剔除冗長的編碼轉換、長度判斷等非核心邏輯,聚焦於「演算法本身」。

核心類別:ComCipher

import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;

public class ComCipher {
    private final byte[] desKey = new byte[8];  // DES 固定 8 位元組金鑰
    private final byte[] desIv  = new byte[8];  // CBC 模式需 8 位元組 IV
    private Cipher cipher;
    private SecretKey secretKey;
    private IvParameterSpec ivSpec;
    private final SecureRandom random = new SecureRandom();

    public ComCipher() {
        // 示範用固定金鑰/IV,實務請用 PBKDF2 衍生
        for (int i = 0; i < 8; i++) {
            desKey[i] = 0x01;
            desIv[i]  = 0x00;
        }
    }

    public void init() throws Exception {
        DESKeySpec keySpec = new DESKeySpec(desKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        secretKey = factory.generateSecret(keySpec);
        ivSpec = new IvParameterSpec(desIv);
        // CBC + NoPadding:資料長度必須為 8 位元組倍數
        cipher = Cipher.getInstance("DES/CBC/NoPadding");
    }

    // 加密
    public byte[] encrypt(byte[] plaintext) throws Exception {
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec, random);
        return cipher.doFinal(plaintext);
    }

    // 解密
    public byte[] decrypt(byte[] ciphertext) throws Exception {
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec, random);
        return cipher.doFinal(ciphertext);
    }
}

呼叫範例

public static void main(String[] args) {
    try {
        ComCipher cipher = new ComCipher();
        cipher.init();

        // 測試資料:正好 8 位元組(NoPadding 要求)
        byte[] input = new byte[8];
        for (int i = 0; i < 8; i++) input[i] = 0x01;

        System.out.println("原文: " + bytesToHex(input));

        byte[] encrypted = cipher.encrypt(input);
        System.out.println("密文: " + bytesToHex(encrypted));

        byte[] decrypted = cipher.decrypt(encrypted);
        System.out.println("解密: " + bytesToHex(decrypted));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

// 輔助:byte[] 轉十六進位字串
private static String bytesToHex(byte[] data) {
    StringBuilder sb = new StringBuilder();
    for (byte b : data) sb.append(String.format("%02X", b));
    return sb.toString();
}

關鍵設定說明

項目設定值備註
演算法DES56-bit 有效金鑰,已不建議用於高機密資料
模式CBC需初始化向量 (IV),比 ECB 安全
填充NoPadding資料長度必須為 8 的倍數;改用 PKCS5Padding 可支援任意長度
金鑰長度8 bytes (64 bits)含 8 位 parity bits,實際強度 56 bits
IV 長度8 bytesCBC 模式必須

實務注意事項

  • 金鑰管理:範例使用硬編碼金鑰,生產環境必須透過 PBKDF2、BCrypt 或 KMS 產生/保護金鑰
  • IV 唯一性:同一金鑰下,每次加密應使用隨機 IV,並將 IV 與密文一起儲存(IV 不需保密)
  • 填充選擇:若資料長度不固定,請改用 DES/CBC/PKCS5Padding,並移除長度限制檢查
  • 安全性警告:DES 已被證明可在數小時內暴力破解,新系統請直接使用 AES-256-GCM 或 ChaCha20-Poly1305

延伸閱讀

  • Java AES 加密解密實作(本系列下一篇)
  • NIST SP 800-38A:區塊加密模式標準
  • OWASP Cryptographic Storage Cheat Sheet

掌握 DES 核心流程,有助於理解區塊加密基本原理,維護舊系統時也能得心應手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved