密碼學 - 轉置密碼加密



在上一章中,我們學習了轉置密碼,瞭解了這種技術的型別、基本實現、特性和缺點。現在我們將瞭解轉置密碼加密演算法,以及如何使用 Python、C++ 和 Java 等不同的語言來實現它。所以首先讓我們瞭解一下轉置演算法。

轉置密碼加密演算法

轉置密碼是一種透過重新排列訊息字母來加密訊息的方法。我們需要一個金鑰來理解字元重新排列的特定順序。金鑰可以是一個數字或一個用於轉置模式的關鍵詞。

以下是加密的分步過程:

  • 首先,我們需要將訊息分解成更小的單元或單個字元。

  • 然後根據金鑰定義一個網格。行數由金鑰的長度決定,列數由訊息長度除以行數決定。

  • 將訊息字元逐行寫入網格。如果訊息不能完全填充網格,我們可以用額外的字元(如空格或下劃線)填充它。

  • 最後,我們可以根據金鑰重新排列列。列的順序表示我們讀取字元以獲取密文的順序。

示例

  • 例如,如果關鍵詞是“SECRET”,我們必須根據“SECRET”中的字母重新排列訊息。

  • 因此,我們將寫下我們的訊息。例如,讓我們使用訊息“HELLO WORLD”。

  • 現在,對於我們關鍵詞中的每個字母,我們將重新排列我們訊息中的字母。因此,使用關鍵詞“SECRET”,我們將根據“SECRET”中字母的順序重新排列“HELLO WORLD”中的字母。

  • 在這種情況下,順序將是:5、1、2、4、6、3、7。因此,“HELLO WORLD”變為“OLHEL LWRDO”。

  • 最後,我們的加密訊息將是“OLHEL LWRDO”。

就是這樣!我們已經使用轉置密碼加密了我們的訊息。

使用 Python 實現

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

  • 使用 Python 列表和 range() 函式

  • 使用 pyperclip 和 random 模組

因此,讓我們在以下部分中逐一瞭解這些方法:

使用 Python 列表和 range() 函式

首先,我們將使用 Python 的列表和一些內建函式來構建轉置密碼加密。因此,我們將使用列表來建立一個空的加密訊息列表,並且我們還將使用 range() 函式來生成一系列數字。它將用於迭代訊息中的列。

示例

下面是使用列表和 range() 函式的轉置密碼加密演算法的簡單 Python 程式碼。請參見下面的程式:

def transposition_encrypt(message, key):
   encrypted = [''] * key
   for col in range(key):
      pointer = col
      while pointer < len(message):
         encrypted[col] += message[pointer]
         pointer += key
   return ''.join(encrypted)

message = "Hello Tutorialspoint"
key = 7
print("Original Message: ", message)
encrypted_message = transposition_encrypt(message, key)
print("Encrypted message:", encrypted_message)

以下是上述示例的輸出:

輸入/輸出
Original Message: Hello Tutorialspoint
Encrypted message: Husetploolrioin atTl

在上面的輸出中,您可以看到實際訊息是 Hello Tutorialspoint,加密訊息是 Husetploolrioin atTl。在加密訊息中,所有字母與原始訊息相同,但它們的順序不同。

使用 pyperclip 和 random 模組

如您所知,Python 的 random 模組用於在給定範圍內生成隨機數。Python 的 pyperclip 模組主要用於複製和貼上剪貼簿功能。因此,在此示例中,我們將使用這些模組來生成隨機金鑰並將加密訊息貼上到剪貼簿。我們將使用 random.randint() 函式在給定範圍內建立一個隨機整數金鑰。這種隨機性為加密過程增加了一層不可預測性。

示例

以下是使用隨機金鑰的轉置密碼加密的實現。請檢視下面的程式碼:

import pyperclip
import random

def generate_random_key():
   return random.randint(2, 10)

def transposition_encrypt(message, key):
   encrypted = [''] * key
   for col in range(key):
      pointer = col
      while pointer < len(message):
         encrypted[col] += message[pointer]
         pointer += key
   return ''.join(encrypted)

message = "Have a great day!"
key = generate_random_key()

print("Original Message: ", message)
encrypted_message = transposition_encrypt(message, key)
pyperclip.copy(encrypted_message)
print("Encrypted message:", encrypted_message)
print("Key used for encryption:", key)
print("Encrypted message copied to clipboard!")

以下是上述示例的輸出:

輸入/輸出
Original Message:  Have a great day!
Encrypted message: Heaavte  daa yg!r
Key used for encryption: 9
Encrypted message copied to clipboard!

Original Message: Have a great day! Encrypted message: H r !aaedv aaegty Key used for encryption: 4 Encrypted message copied to clipboard!

在上面的輸出中,您可以注意到每次使用隨機金鑰生成加密訊息。

使用 Java 實現

在此,我們將使用 Java 程式語言來實現轉置密碼加密。明文字串和金鑰是加密方法所需的輸入引數。根據明文和金鑰的長度,它確定需要多少行。接下來,它逐行使用明文字元生成一個矩陣。之後,透過逐列讀取矩陣建立密文。然後,我們可以在主方法中使用加密方法。

示例

以下是使用 Java 程式語言實現轉置密碼加密的示例:

public class TranspositionCipher {

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

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

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

        return ciphertext.toString();
    }

    public static void main(String[] args) {
        String plaintext = "Hello, This is very beautiful world";
        int key = 4;
        System.out.println("The Plaintext Message is: " + plaintext);
        String encryptedText = encrypt(plaintext, key);
        System.out.println("The Encrypted text: " + encryptedText);
    }
}

以下是上述示例的輸出:

輸入/輸出

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

使用 C++ 實現

現在我們將使用 C++ 來實現轉置密碼加密。明文字串和金鑰是加密函式的輸入引數。根據明文和金鑰的長度,它確定需要多少行。接下來,它逐行使用明文字元填充一個矩陣。之後,透過逐列讀取矩陣構造密文。在主函式中可以看到加密函式的使用。

示例

請參見下面使用 C++ 實現轉置密碼加密的示例:

#include <iostream>
#include <string>
#include <algorithm>

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

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

   // Read matrix column-wise to get ciphertext
   for (int j = 0; j < key; ++j) {
      for (int i = 0; i < rows; ++i) {
         ciphertext += matrix[i][j];
      }
   }

   return ciphertext;
}

int main() {
   std::string plaintext = "Hello, Tutorialspoint Family";
   int key = 4;
   std::cout << "The Plaintext message is: " << plaintext << std::endl;
   std::string encrypted_msg = encrypt(plaintext, key);
   std::cout << "The Encrypted text: " << encrypted_msg << std::endl;

   return 0;
}

以下是上述示例的輸出:

輸入/輸出

The Plaintext message is: Hello, Tutorialspoint Family
The Encrypted text: Houiptme,tao il oliFllTrsnay

總結

轉置密碼是一種簡單的加密方法,它根據給定的金鑰重新排列訊息的字元。在本章中,我們瞭解了轉置密碼加密技術的多種 Python 實現。這些實現包括使用 Python 列表和 range 函式、用於剪貼簿功能的 Pyperclip 模組、列序方法以及生成用於加密的隨機金鑰。

廣告
© . All rights reserved.