加密策略概覽
本實作結合兩種經典密碼學方法進行三重加密:
- 第一層:柵欄密碼(奇偶位分組重組)
- 第二層:柵欄密碼(再次奇偶分組)
- 第三層:摩斯密碼(字元轉點劃編碼)
柵欄密碼實作
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));
}
}
安全性分析與局限