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

Java 密碼三重加密實作:柵欄密碼與摩斯密碼組合應用

     編輯:Java 相關知識

加密策略概覽

本實作結合兩種經典密碼學方法進行三重加密:

  1. 第一層:柵欄密碼(奇偶位分組重組)
  2. 第二層:柵欄密碼(再次奇偶分組)
  3. 第三層:摩斯密碼(字元轉點劃編碼)

柵欄密碼實作

public class FenceCipher {
    /** 加密:奇數位在前,偶數位在後 */
    public String encrypt(String plaintext) {
        StringBuilder odd = new StringBuilder();
        StringBuilder even = new StringBuilder();
        
        for (int i = 0; i < plaintext.length(); i++) {
            if (i % 2 == 0) odd.append(plaintext.charAt(i));
            else even.append(plaintext.charAt(i));
        }
        return odd.toString() + even.toString();
    }
    
    /** 解密:還原原始順序 */
    public String decrypt(String ciphertext) {
        int mid = (ciphertext.length() + 1) / 2; // 奇數長度時奇數位較多
        String oddPart = ciphertext.substring(0, mid);
        String evenPart = ciphertext.substring(mid);
        
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < oddPart.length(); i++) {
            result.append(oddPart.charAt(i));
            if (i < evenPart.length()) {
                result.append(evenPart.charAt(i));
            }
        }
        return result.toString();
    }
}

摩斯密碼編碼表

字元摩斯字元摩斯字元摩斯
A.-N-. 0-----
B-...O---1.----
C-.-.P.--.2..---
D-..Q--.-3...--
E.R.-.4....-
F..-.S...5.....
G--.T-6-....
H....U..-7--...
I..V...-8---..
J.---W.--9----.
K-.-X-..-..-.-.-
L.-..Y-.--,--..--
M--Z--..?..--..

摩斯密碼實作(含雙層柵欄)

public class MorseCipher {
    private static final Map ENCODE_MAP = buildEncodeMap();
    private static final Map DECODE_MAP = buildDecodeMap();
    private final FenceCipher fence = new FenceCipher();
    
    private static Map buildEncodeMap() {
        Map map = new HashMap<>();
        map.put('A', ".-"); map.put('B', "-..."); map.put('C', "-.-."); map.put('D', "-..");
        map.put('E', "."); map.put('F', "..-."); map.put('G', "--."); map.put('H', "....");
        map.put('I', ".."); map.put('J', ".---"); map.put('K', "-.-"); map.put('L', ".-..");
        map.put('M', "--"); map.put('N', "-."); map.put('O', "---"); map.put('P', ".--.");
        map.put('Q', "--.-"); map.put('R', ".-."); map.put('S', "..."); map.put('T', "-");
        map.put('U', "..-"); map.put('V', "...-"); map.put('W', ".--"); map.put('X', "-..-");
        map.put('Y', "-.--"); map.put('Z', "--..");
        map.put('0', "-----"); map.put('1', ".----"); map.put('2', "..---"); map.put('3', "...--");
        map.put('4', "....-"); map.put('5', "....."); map.put('6', "-...."); map.put('7', "--...");
        map.put('8', "---.."); map.put('9', "----.");
        map.put('.', ".-.-.-"); map.put(',', "--..--"); map.put('?', "..--..");
        // 可擴充更多符號...
        return map;
    }
    
    private static Map buildDecodeMap() {
        Map map = new HashMap<>();
        ENCODE_MAP.forEach((k, v) -> map.put(v, k));
        return map;
    }
    
    /** 三重加密:柵欄 → 柵欄 → 摩斯 */
    public String encrypt(String plaintext) {
        String step1 = fence.encrypt(plaintext.toUpperCase());
        String step2 = fence.encrypt(step1);
        
        StringBuilder morse = new StringBuilder();
        for (char c : step2.toCharArray()) {
            String code = ENCODE_MAP.getOrDefault(c, "?");
            morse.append(code).append("/"); // 以 / 分隔字元
        }
        return morse.toString();
    }
    
    /** 解密:摩斯 → 柵欄解密 → 柵欄解密 */
    public String decrypt(String morseCipher) {
        StringBuilder step2 = new StringBuilder();
        for (String code : morseCipher.split("/")) {
            if (!code.isEmpty()) {
                step2.append(DECODE_MAP.getOrDefault(code, '?'));
            }
        }
        
        String step1 = fence.decrypt(step2.toString());
        return fence.decrypt(step1);
    }
}

使用範例與測試

public class TripleEncryptionDemo {
    public static void main(String[] args) {
        MorseCipher cipher = new MorseCipher();
        String password = "MySecret123";
        
        System.out.println("原始密碼:" + password);
        
        String encrypted = cipher.encrypt(password);
        System.out.println("加密結果:" + encrypted);
        
        String decrypted = cipher.decrypt(encrypted);
        System.out.println("解密結果:" + decrypted);
        
        System.out.println("驗證:" + password.equalsIgnoreCase(decrypted));
    }
}

安全性分析與局限

現代替代方案建議

// 生產環境密碼處理標準做法
// 1. 密碼雜湊(儲存):BCrypt / Argon2 / PBKDF2
String hashed = BCrypt.hashpw(password, BCrypt.gensalt(12));

// 2. 對稱加密(需可逆):AES-GCM
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
// 3. 非對稱加密(金鑰交換):RSA-OAEP / ECIES
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
層面評估
加密強度極低,屬古典密碼學,易遭頻率分析破解
金鑰管理無金鑰,安全性依賴演算法保密(不符合柯克霍夫原則)
適用場景教學示範、簡易混淆、非敏感資料標記
生產環境嚴禁使用,需採用 AES、RSA 等現代加密標準