密碼學 - 單表代換密碼破解



近年來,技術已融入日常生活。它簡化了賬單支付和網上購物等任務。然而,人們經常在網上賬戶中儲存敏感資訊,卻意識不到駭客可以輕鬆訪問這些資訊。為了理解這種漏洞,必須以駭客的心態去認識他們如何繞過公司採用的安全措施。

在網路安全領域,雖然公司負責 50% 的防護,但另一半則取決於使用者以及他們對所提供安全工具的有效使用。駭客會使用諸如頻率分析、暴力破解和網路釣魚等技術。頻率分析是一種用於破解單表代換密碼的基本密碼分析方法。

破解單表代換密碼的方法

以下是一些破解或密碼分析單表代換密碼的方法:

  • 頻率分析 - 不同的字母在英語中出現的頻率不同。“E”是最常見的字母。研究字母在密文中出現的頻率可以幫助你找出哪個字母可能代表“E”。一旦你知道了這一點,你就可以找出其他字母。

  • 模式識別 - 檢視密文中是否存在重複出現的序列。一些字母組合,例如“TH”或“ING”,在英語中出現頻率很高。如果你注意到這些組合,你可以猜測它們分別代表哪些字母。

  • 猜測和檢查 - 根據上下文進行合理的猜測。例如:如果你懷疑一個單詞是“THE”,猜測可能代表“T”、“H”和“E”的字母。利用這一點來揭示其他字母。

  • 已知明文攻擊 - 如果你同時擁有原始訊息(明文)和加密訊息(密文),你可以利用這些資訊來找出加密金鑰。這種方法非常有效,但通常需要更多資源。

  • 暴力破解 - 作為最後的手段,嘗試所有可能的字母組合,直到找到正確的組合。這種方法需要大量時間,通常只適用於短訊息。

使用 Python 實現

單表代換密碼使用固定的替換來加密整個訊息。一個利用 Python 字典和 JSON 物件的單表代換密碼。透過使用此字典,我們可以加密字母並將相應的字母作為值儲存在 JSON 中。以下程式以類的形式建立了一個單表代換程式,其中包含所有加密和解密函式。

示例

from string import ascii_letters, digits
from random import shuffle

def random_monoalpha_cipher(pool=None):
   if pool is None:
      pool = ascii_letters + digits
   original_pool = list(pool)
   shuffled_pool = list(pool)
   shuffle(shuffled_pool)
   return dict(zip(original_pool, shuffled_pool))

def inverse_monoalpha_cipher(monoalpha_cipher):
   inverse_monoalpha = {}
   for key, value in monoalpha_cipher.items():
      inverse_monoalpha[value] = key
   return inverse_monoalpha

def encrypt_with_monoalpha(message, monoalpha_cipher):
   encrypted_message = []
   for letter in message:
      encrypted_message.append(monoalpha_cipher.get(letter, letter))
   return ''.join(encrypted_message)

def decrypt_with_monoalpha(encrypted_message, monoalpha_cipher):
   return encrypt_with_monoalpha(
      encrypted_message,
      inverse_monoalpha_cipher(monoalpha_cipher)
   )

# Generate a random monoalphabetic cipher
cipher = random_monoalpha_cipher()
print("Cipher:", cipher)

# Encrypt a message
message = 'Hello all you hackers out there!'
encrypted = encrypt_with_monoalpha(message, cipher)
print("Encrypted:", encrypted)

# Decrypt the message
decrypted = decrypt_with_monoalpha(encrypted, cipher)
print("Decrypted:", decrypted)

當你實現上面給出的程式碼時,你將得到以下輸出。

輸入/輸出

Monoalphabetic Output

因此,你可以使用定義的鍵值組合破解單表代換密碼,將密文轉換為明文。

使用 Java 實現

下面提供的 Java 程式碼具有與我們上面提到的 Python 程式碼類似的功能。它可以建立一個隨機的單表代換密碼,使用該密碼加密訊息,並解密加密的訊息。請參閱下面的程式碼:

示例

import java.util.*;

public class MonoalphabeticCipher {
   public static Map<Character, Character> randomMonoalphaCipher(String pool) {
      List<Character> originalChar = new ArrayList<>();
      List<Character> ShuffledChar = new ArrayList<>();

      for (char c : pool.toCharArray()) {
         originalChar.add(c);
         ShuffledChar.add(c);
      }

      Collections.shuffle(ShuffledChar);

      Map<Character, Character> cipher = new HashMap<>();
      for (int i = 0; i < originalChar.size(); i++) {
      cipher.put(originalChar.get(i), ShuffledChar.get(i));
      }

      return cipher;
   }

   public static Map<Character, Character> inverseCharCipher(Map<Character, Character> monoalphaCipher) {
      Map<Character, Character> inverseChar = new HashMap<>();
      for (Map.Entry<Character, Character> entry : monoalphaCipher.entrySet()) {
         inverseChar.put(entry.getValue(), entry.getKey());
      }
      return inverseChar;
   }

   public static String encryptMessage(String message, Map<Character, Character> monoalphaCipher) {
      StringBuilder etMsg = new StringBuilder();
      for (char letter : message.toCharArray()) {
         etMsg.append(monoalphaCipher.getOrDefault(letter, letter));
      }
      return etMsg.toString();
   }

   public static String decryptMessage(String etMsg, Map<Character, Character> monoalphaCipher) {
      return encryptMessage(etMsg, inverseCharCipher(monoalphaCipher));
   }

   public static void main(String[] args) {
      String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      Map<Character, Character> cipher = randomMonoalphaCipher(characters);
      System.out.println("Cipher: " + cipher);

      String message = "Hello all you hackers out there!";
      String encrypted = encryptMessage(message, cipher);
      System.out.println("Encrypted: " + encrypted);

      String decrypted = decryptMessage(encrypted, cipher);
      System.out.println("Decrypted: " + decrypted);
   }
}

以下是上面示例的輸出:

輸入/輸出

Monoalphabetic Output
廣告