密碼學 - 轉置密碼解密



在上一章中,我們學習了轉置密碼加密。現在我們將學習轉置密碼的解密及其使用不同方法的實現。

解密過程只是反轉加密演算法的步驟。我們使用相同的金鑰來查詢讀取列以恢復實際訊息的正確順序。

轉置密碼解密演算法

以下是轉置密碼解密的演算法:

輸入

  • 密文 - 加密的訊息。

  • 金鑰 - 用於加密的相同金鑰。

步驟

  • 首先,我們需要根據金鑰長度將密文分成多行。這意味著我們將為金鑰中的每個字元建立一個行。

  • 現在,我們將建立一個空的網格,其行數與金鑰長度相同,列數等於密文長度除以金鑰長度的結果。

  • 接下來,我們必須根據金鑰填充網格。因此,迭代密文字元以確定其列索引。

  • 並將字元新增到網格中的相應單元格中。

  • 從第一列開始,向下讀取字元,到達底部後移動到下一列。對所有列繼續此過程。這會根據原始轉置順序重新排列字元。

  • 列印明文訊息。

示例

輸入

  • 密文:OLHEL LWRDO

  • 金鑰:SECRET

例如,如果我們知道金鑰是“SECRET”,並且原始訊息是根據金鑰中字母的順序進行轉置的。我們可以像下面這樣重新排列列:

金鑰 S E C R E T
5, 1, 2, 4, 6, 3

現在,我們將根據金鑰重新排序轉置訊息“OLHEL LWRDO”的列,如下所示:

S O L
E H E
C L W
R H R
E E D
T L O

最後,我們逐行讀取字元以獲取原始訊息:

原始訊息:HELLO WORLD

因此,使用金鑰“SECRET”對加密訊息“OLHEL LWRDO”解密後的訊息是“HELLO WORLD”。

太棒了!我們已經使用轉置密碼解密了我們的訊息。

使用 Python 實現

可以使用不同的方法實現轉置密碼的解密:

  • 使用 Math 模組

  • 使用 Pyperclip 模組

因此,讓我們在以下部分逐一檢視這兩種方法:

使用 Math 模組

此 Python 程式碼解密使用轉置密碼加密的訊息。我們將使用 math 模組執行數學運算,主要是計算解密訊息所需的列數。math.ceil() 用於將訊息長度除以金鑰的結果向上取整。因此,此程式碼有效地解密轉置密碼訊息。

示例

以下是使用 Math 模組的轉置密碼解密演算法的 Python 程式碼。請參見下面的程式:

import math

def transposition_decrypt(key, message):
   num_of_columns = math.ceil(len(message) / key)
   num_of_rows = key
   num_of_shaded_boxes = (num_of_columns * num_of_rows) - len(message)
   plaintext = [''] * num_of_columns
   col = 0
   row = 0

   for symbol in message:
      plaintext[col] += symbol
      col += 1
      if (col == num_of_columns) or (col == num_of_columns - 1 and row >= num_of_rows - num_of_shaded_boxes):
         col = 0
         row += 1

   return ''.join(plaintext)

ciphertext = 'Toners raiCntisippoh'
key = 6
plaintext = transposition_decrypt(key, ciphertext)
print("Cipher Text: ", ciphertext)
print("The plain text is: ", plaintext)

以下是上述示例的輸出:

輸入/輸出
Cipher Text:  Toners raiCntisippoh
The plain text is:  Transposition Cipher

在上面的輸出中,您可以看到密文訊息是 Toners raiCntisippoh,解密後的訊息是 Transposition Cipher。

使用 Pyperclip 模組

在此示例中,我們將使用 Python 的 pyperclip 模組,它用於複製和貼上剪貼簿功能。因此,我們將使用此模組將解密的訊息複製到剪貼簿。

此程式碼與上面的程式碼類似,但在本程式碼中,我們使用 pyperclip 模組將解密的訊息複製到剪貼簿。

示例

以下是使用 pyperclip 模組的轉置密碼解密演算法的 Python 程式碼。請檢查下面的程式碼:

import math
import pyperclip

def transposition_decrypt(key, message):
   num_of_columns = math.ceil(len(message) / key)
   num_of_rows = key
   num_of_shaded_boxes = (num_of_columns * num_of_rows) - len(message)
   plaintext = [''] * num_of_columns
   col = 0
   row = 0

   for symbol in message:
      plaintext[col] += symbol
      col += 1
      if (col == num_of_columns) or (col == num_of_columns - 1 and row >= num_of_rows - num_of_shaded_boxes):
         col = 0
         row += 1

   return ''.join(plaintext)

ciphertext = 'Toners raiCntisippoh'
key = 6
plaintext = transposition_decrypt(key, ciphertext)
print("Cipher Text:", ciphertext)
print("The plain text is:", plaintext)

# Copy the decrypted plaintext to the clipboard
pyperclip.copy(plaintext)
print("The Decrypted Message is Copied to the Clipboard")

以下是上述示例的輸出:

輸入/輸出
Cipher Text: Toners raiCntisippoh
The plain text is: Transposition Cipher
The Decrypted Message is Copied to the Clipboard

在上面的輸出中,我們可以看到明文已複製到剪貼簿,並且顯示訊息“解密後的訊息已複製到剪貼簿”。

使用 Java 實現

我們將使用 Java 程式語言來實現轉置密碼的解密。基本上,我們將使用 Java 在此實現中反轉加密過程。

請參見 Java 中的以下程式碼:

示例

public class TranspositionCipher {
   // Function to decrypt using transposition cipher
   public static String decrypt(String ciphertext, int key) {
      StringBuilder plaintext = new StringBuilder();
      int cols = (int) Math.ceil((double) ciphertext.length() / key);
      char[][] matrix = new char[cols][key];

      // Fill matrix with ciphertext characters
      int index = 0;
      for (int j = 0; j < key; ++j) {
         for (int i = 0; i < cols; ++i) {
            if (index < ciphertext.length())
               matrix[i][j] = ciphertext.charAt(index++);
            else
               matrix[i][j] = ' ';
         }
      }

      // Read matrix row-wise to get plaintext
      for (int i = 0; i < cols; ++i) {
         for (int j = 0; j < key; ++j) {
            plaintext.append(matrix[i][j]);
         }
      }

      return plaintext.toString();
   }

   public static void main(String[] args) {
      String ciphertext = "Hohiebtlre,isrei ll s yafwdlT v uuo ";
      int key = 4;
      System.out.println("The Encrypted text: " + ciphertext);
      String decryptedText = decrypt(ciphertext, key);
      System.out.println("Decrypted text: " + decryptedText);
   }
}

以下是上述示例的輸出:

輸入/輸出

The Encrypted text: Hohiebtlre,isrei ll s yafwdlT v uuo 
Decrypted text: Hello, This is very beautiful world 

使用 C++ 實現

現在,我們將使用 C++ 程式語言來實現轉置密碼的解密演算法。在其中,我們將透過 decrypt 函式反轉加密過程。它透過計算密文長度和金鑰來找到所需的列數。它將密文中的字元按列新增到矩陣中。之後,它使用矩陣的逐行讀取來構建明文。

以下是使用 C++ 實現轉置密碼解密的程式碼:

示例

#include <iostream>
#include <string>
#include <cmath>

// Function to decrypt using transposition cipher
std::string decrypt(std::string ciphertext, int key) {
   std::string plaintext = "";
   int cols = (ciphertext.length() + key - 1) / key;
   char matrix[cols][key];

   // Fill matrix with ciphertext characters
   int index = 0;
   for (int j = 0; j < key; ++j) {
      for (int i = 0; i < cols; ++i) {
         if (index < ciphertext.length())
            matrix[i][j] = ciphertext[index++];
         else
            matrix[i][j] = ' ';
      }
   }

   // Read matrix row-wise to get plaintext
   for (int i = 0; i < cols; ++i) {
      for (int j = 0; j < key; ++j) {
         plaintext += matrix[i][j];
      }
   }

   return plaintext;
}

int main() {
   std::string ciphertext = "Houiptme,tao il oliFllTrsnay";
   int key = 4;
   std::cout << "The Encrypted text: " << ciphertext << std::endl;
   std::string decrypted_text = decrypt(ciphertext, key);
   std::cout << "The Decrypted text: " << decrypted_text << std::endl;

   return 0;
}

以下是上述示例的輸出:

輸入/輸出

The Encrypted text: Houiptme,tao il oliFllTrsnay
The Decrypted text: Hello, Tutorialspoint Family

總結

在本章中,我們已經瞭解瞭如何使用 Python、C++ 和 Java 中的轉置密碼解密演算法將密文解密回其原始形式。

廣告
© . All rights reserved.