密碼學 - 簡單替換密碼的解密



在上一章中,我們看到了簡單替換密碼的不同加密程式。現在我們將討論解密方法,使用 Python、Java 和 C++ 使用簡單替換密碼解密給定的訊息。

如您所知,解密是加密的反向過程,因此我們將反轉我們在上一章中為加密建立的所有程式。

使用 Python 實現

我們可以使用以下方法實現簡單替換密碼的解密:

  • 使用指定的偏移量

  • 使用字典對映

  • 直接使用 ASCII 值

在接下來的部分中,我們將嘗試詳細描述這些方法中的每一種,以便您可以更好地理解 Python 密碼學和簡單替換密碼解密。

方法 1:使用指定的偏移量

要為之前的加密程式建立解密函式,我們可以使用相同的方法,但反轉偏移方向。以下函式透過以與原始偏移相反的方向移動密文中每個字母來反轉加密過程。它的工作原理是減去偏移量而不是加上它。所以基本上我們在撤銷加密。

以下是使用此方法在 Python 中實現解密過程:

示例

# function for decryption
def substitution_decrypt(encrypted_text, shift):
   decrypted_text = ""
   for char in encrypted_text:
      if char.isalpha():
         ascii_val = ord(char)
         if char.isupper():
            # Reverse the shift for uppercase letters
            shifted_ascii = ((ascii_val - 65 - shift) % 26) + 65
         else:
            # Reverse the shift for lowercase letters
            shifted_ascii = ((ascii_val - 97 - shift) % 26) + 97
         decrypted_char = chr(shifted_ascii)
         decrypted_text += decrypted_char
      else:
         decrypted_text += char
   return decrypted_text

# our cipher text
encrypted_text = "Mjqqt, jajwdtsj!"
shift = 5
decrypted_text = substitution_decrypt(encrypted_text, shift)
print("Cipher Text: ", encrypted_text)
print("Decrypted text:", decrypted_text)

以下是上述示例的輸出:

輸入/輸出
Cipher Text:  Mjqqt, jajwdtsj!
Decrypted text: Hello, everyone!

方法 2:使用字典對映

在這裡,我們將使用字典對映來實現簡單替換密碼解密。正如我們在上一章中使用字典對映來加密明文一樣。現在我們將使用相同的技術,但反向。我們需要反轉此對映。而不是將每個字母對映到下一個字母,我們將密文中每個字母映射回明文中的原始字母。

這需要建立一個反向字典,其中鍵是密文中的字元,值是明文中的相應字元。

以下是使用反向字典對映實現簡單替換密碼解密的 Python 程式碼:

# function for decryption
def substitution_decrypt(encrypted_text, key):
   # a reverse dictionary mapping for decryption
   reverse_mapping = {key[i]: chr(97 + i) for i in range(26)}
   # Decrypt the using reverse mapping
   decrypted_text = ''.join(reverse_mapping.get(char, char) for char in encrypted_text)
   return decrypted_text


# our cipher text
encrypted_text = "ifmmp, efbs gsjfoe!"
key = "bcdefghijklmnopqrstuvwxyza"  # Example substitution key
decrypted_text = substitution_decrypt(encrypted_text, key)
print("Cipher Text: ", encrypted_text)
print("Decrypted text:", decrypted_text)

以下是上述示例的輸出:

輸入/輸出

Cipher Text:  ifmmp, efbs gsjfoe!
Decrypted text: hello, dear friend!

方法 3:直接使用 ASCII 值

ASCII 是一種字元編碼標準,我們為字母、數字和其他字元分配數值。在加密過程中,明文的每個字母都會使用 ASCII 值偏移固定數量。現在我們必須解密,因此我們將透過從每個字元中減去相同的固定數量來反轉偏移過程。

例如,如果我們正在解密偏移了 3 的“d”(ASCII 值 100),我們將減去 3 以返回到“a”(ASCII 值 97)。

因此,以下是使用 ASCII 值直接實現簡單替換密碼解密的 Python 程式碼:

# function for decryption
def substitution_decrypt(encrypted_text, shift):
   decrypted_text = ""
   for char in encrypted_text:
     if char.isalpha():
         # Use ASCII values directly to reverse the shift for decryption
         ascii_val = ord(char)
         shifted_ascii = ascii_val - shift
         if char.isupper():
            if shifted_ascii < 65:
               shifted_ascii += 26
            elif shifted_ascii > 90:
               shifted_ascii -= 26
         elif char.islower():
            if shifted_ascii < 97:
               shifted_ascii += 26
            elif shifted_ascii > 122:
               shifted_ascii -= 26
         decrypted_text += chr(shifted_ascii)
     else:
         decrypted_text += char
   return decrypted_text

# our cipher text
encrypted_text = "Khoor, pb ghdu froohdjxh!"
shift = 3
decrypted_text = substitution_decrypt(encrypted_text, shift)
print("Cipher Text: ", encrypted_text)
print("Decrypted text:", decrypted_text)

以下是上述示例的輸出:

輸入/輸出

Cipher Text:  Khoor, pb ghdu froohdjxh!
Decrypted text: Hello, my dear colleague!

使用 Java 實現

在此示例中,我們將使用 Java 實現簡單替換密碼的解密。因此,我們將使用 Java 的 Hashmap 和 Map 庫來實現此功能。decryptMessage() 方法的工作原理與加密方法類似,但在此我們將使用解密對映。它將每個加密的字母映射回字母表中的實際字母。我們將使用此方法解密我們在上一章中生成的密文並獲取原始明文。

示例

因此,使用 Java 的 Hashmap 和 Map 庫的實現如下:

import java.util.HashMap;
import java.util.Map;

public class SSC {

   private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
   private static final String encrypted_alphabet = "bcdefghijklmnopqrstuvwxyza";
   private static final Map<Character, Character> encryptionMap = new HashMap<>();
   private static final Map<Character, Character> decryptionMap = new HashMap<>();

   static {
      for (int i = 0; i < alphabet.length(); i++) {
         encryptionMap.put(alphabet.charAt(i), encrypted_alphabet.charAt(i));
         decryptionMap.put(encrypted_alphabet.charAt(i), alphabet.charAt(i));
      }
   }

   public static String decryptMessage(String ciphertext) {
     StringBuilder plaintext = new StringBuilder();
     for (char c : ciphertext.toLowerCase().toCharArray()) {
         if (decryptionMap.containsKey(c)) {
            plaintext.append(decryptionMap.get(c));
         } else {
            plaintext.append(c);
         }
      }
      return plaintext.toString();
   }

   public static void main(String[] args) {
      String et = "ifmmp uvupsjbmtqpjou";
      System.out.println("The Encrypted text: " + et);

      String dt = decryptMessage(et);
      System.out.println("The Decrypted text: " + dt);
   }
}

以下是上述示例的輸出:

輸入/輸出

The Encrypted text: ifmmp uvupsjbmtqpjou
The Decrypted text: hello tutorialspoint

使用 C++ 實現

現在我們將使用 C++ 來實現簡單替換密碼的解密過程。因此,我們將反轉我們在上一章中討論過的加密過程。因此,在此我們使用 C++ 的“unordered_map”容器。它用於將每個加密的字母映射回字母表中的實際形式。我們將使用此方法解密密文並獲取明文訊息。

示例

請參閱以下 C++ 程式語言中簡單替換密碼解密函式的程式碼:

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

class SSC {
private:
   const string alphabet = "abcdefghijklmnopqrstuvwxyz";
   const string encrypted_alphabet = "bcdefghijklmnopqrstuvwxyza";
   unordered_map<char, char> encryptionMap;
   unordered_map<char, char> decryptionMap;

public:
   SSC() {
      for (int i = 0; i < alphabet.length(); i++) {
         encryptionMap[alphabet[i]] = encrypted_alphabet[i];
         decryptionMap[encrypted_alphabet[i]] = alphabet[i];
      }
   }

   string decryptMessage(string ciphertext) {
      string plaintext = "";
      for (char c : ciphertext) {
         if (decryptionMap.find(tolower(c)) != decryptionMap.end()) {
            plaintext += decryptionMap[tolower(c)];
         } else {
            plaintext += c;
         }
      }
      return plaintext;
   }
};

int main() {
   SSC cipher;
   string et = "ifmmp uifsf ipx bsf zpv!";
   cout << "The Encrypted text: " << et << endl;
   string dt = cipher.decryptMessage(et);
   cout << "The Decrypted text: " << dt << endl;

   return 0;
}

以下是上述示例的輸出:

輸入/輸出

The Encrypted text: ifmmp uifsf ipx bsf zpv!
The Decrypted text: hello there how are you!

總結

本章介紹了在 Python、Java 和 C++ 中解密簡單替換密碼的不同方法。我們已經看到了使用指定偏移量、字典對映、直接使用 ASCII 值以及透過對映的方法。這些方法解釋了使用簡單替換密碼解密加密文字的不同方法,這些方法提供了使用不同程式語言的靈活性。

廣告

© . All rights reserved.