密码学 - 转置密码加密



在上一章中,我们学习了转置密码,了解了这种技术的类型、基本实现、特性和缺点。现在我们将了解转置密码加密算法,以及如何使用 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.