密码学 - 栅栏密码

密码学 - 栅栏密码

密码学 - 栅栏密码

上一页

下一页

栅栏法是一种基本的换位密码。它是一种加密过程,其中消息中的字母被重新排列以形成新的、看似无关的消息。这种方法的名称来自我们书写消息的方式。当使用栅栏法创建文本时,结果是一个之字形图案,其中每个字母在转到下一行之前都会被拼写出来。

为了使用栅栏法加密消息,必须将消息写入表格的第一行。此外,消息的第二个字母需要写在第二行。这个过程必须继续进行,直到所有消息的字母都被写入。最后,我们按行读取数据库以创建加密的消息。

现在让我们开始讨论如何解码消息。解密的第一步是根据加密消息的长度找到表格的行数。此外,我们必须将加密消息的第一个字母写在第一行,第二个字母写在第二行,依此类推。这个过程必须遵循,直到所有消息的字母都被写入。

总而言之,栅栏法的加密非常简单。它不能提供非常强的安全性。即使是具有基本密码学知识的人也可以轻松破解它。但是,在不需要高安全性的时候,它仍然可以用于简单的通信。

栅栏密码的工作原理?

本节将详细解释栅栏密码使用的加密和解密过程。

加密

为了使用栅栏密码解密消息,我们应该首先选择轨道数,使用该数字以之字形图案对角线写入消息,然后从左到右组合每条轨道上的字母。我们将在下面的示例中逐步讲解每个步骤。

让我们以“RAILFENCE”作为明文为例。现在假设有三个轨道或栅栏,也称为密钥。之字形图案的高度将由密钥决定。然后可以以之字形图案对角线从左到右写入消息:

为了创建密文,我们将合并不同的行,在本例中为“RFEALECIN”。

解密

在我们开始解密过程之前,需要确定密文中行和列的数量。密文的长度等于列数。之后,我们需要确定加密了多少行——充当密钥。

现在我们知道了有多少行和列,我们可以构建表格并弄清楚字母应该放在哪里,因为栅栏密码以之字形图案从左到右对角线加密文本:

星号 (*) 表示插入密文中的字母以创建明文的位置。从顶行(即第一“轨道”)开始,我们从左到右填写字母。直到所有星号位置都用密文中的字母填充,然后我们在下一轨道上继续此模式,依此类推:

让我们完成上面的表格:

最后,我们能够从左到右和从上到下组合字符以获得明文“RAILFENCE”。

实现

现在我们将使用 Python、Java、C++ 和 Javascript 实现栅栏密码。

使用 Python 实现

首先,我们将使用 Python 创建代码并实现栅栏密码。我们将创建两个不同的函数,一个用于加密,另一个用于解密。请看下面的代码:

示例

# Function to encrypt a message

def encrypt_rail_fence(plaintext, rails):

# Create the matrix for cipher

rail_matrix = [['\n' for i in range(len(plaintext))]

for j in range(rails)]

# Find the direction

down_direction = False

row, col = 0, 0

for i in range(len(plaintext)):

# Check the direction of flow

# Reverse the direction if just filled the top or bottom rail

if (row == 0) or (row == rails - 1):

down_direction = not down_direction

# Fill the corresponding alphabet

rail_matrix[row][col] = plaintext[i]

col += 1

# Find the next row using direction flag

if down_direction:

row += 1

else:

row -= 1

# Construct the cipher using the rail matrix

cipher_text = []

for i in range(rails):

for j in range(len(plaintext)):

if rail_matrix[i][j] != '\n':

cipher_text.append(rail_matrix[i][j])

return("" . join(cipher_text))

# Function to decrypt the cipher-text

# Function to decrypt the cipher-text

def decrypt_rail_fence(cipher, rails):

# Create the matrix to cipher

# plaintext - rows, length(cipher) - columns

# Fill the rail matrix to distinguish filled spaces from blank ones

rail_matrix = [['\n' for i in range(len(cipher))]

for j in range(rails)]

# Find the direction

down_direction = None

row, col = 0, 0

for i in range(len(cipher)):

# Check the direction of flow

if row == 0:

down_direction = True

if row == rails - 1:

down_direction = False

# Place the cipher text in the rail matrix

rail_matrix[row][col] = '*'

col += 1

# Find the next row using direction flag

if down_direction:

row += 1

else:

row -= 1

# Reconstruct the rail matrix with cipher text

index = 0

for i in range(rails):

for j in range(len(cipher)):

if rail_matrix[i][j] == '*' and index < len(cipher):

rail_matrix[i][j] = cipher[index]

index += 1

# Read the rail matrix in zig-zag manner to construct the resultant text

result = []

row, col = 0, 0

for i in range(len(cipher)):

# Check the direction of flow

if row == 0:

down_direction = True

if row == rails - 1:

down_direction = False

# Add characters from the rail matrix to the result

if rail_matrix[row][col] != '*':

result.append(rail_matrix[row][col])

col += 1

# Find the next row using direction flag

if down_direction:

row += 1

else:

row -= 1

return ''.join(result)

# Driver code

if __name__ == "__main__":

print("First Encrypted Text: ", encrypt_rail_fence("Hello Tutorialspoint", 2))

print("Second Encrypted Text: ", encrypt_rail_fence("Simple Text", 3))

print("Third Encrypted Text: ", encrypt_rail_fence("I am great Cipher", 5))

# Decryption of the cipher-text

print("First Decrypted Text: ", decrypt_rail_fence("HloTtrasonel uoilpit", 2))

print("Second Decrypted Text: ", decrypt_rail_fence("SleipeTxm t", 3))

print("Third Decrypted Text: ", decrypt_rail_fence("Iar etear hmgCp i", 5))

以下是上述示例的输出:

输入/输出

First Encrypted Text: HloTtrason

Second Encrypted Text: S

Third Encrypted Text: I

First Decrypted Text: H

Second Decrypted Text: S

Third Decrypted Text: I

使用 Java 实现

现在我们将使用 Java 编程语言实现栅栏密码。我们将使用与 Python 中相同的 approach。在这里,我们将使用 Java 的 Arrays 库来实现栅栏矩阵。代码如下:

示例

import java.util.Arrays;

public class CustomRailFence {

//Encrypt a message

public static String encryptMessage(String plaintext, int rails) {

// Create the matrix

char[][] railMatrix = new char[rails][plaintext.length()];

// Filling the rail matrix

for (int i = 0; i < rails; i++)

Arrays.fill(railMatrix[i], '\n');

boolean dirDown = false;

int row = 0, col = 0;

for (int i = 0; i < plaintext.length(); i++) {

// Check the direction of flow

if (row == 0 || row == rails - 1)

dirDown = !dirDown;

// Fill the corresponding alphabet

railMatrix[row][col++] = plaintext.charAt(i);

// Find the next row using direction flag

if (dirDown)

row++;

else

row--;

}

// Now we can create the cipher

StringBuilder result = new StringBuilder();

for (int i = 0; i < rails; i++)

for (int j = 0; j < plaintext.length(); j++)

if (railMatrix[i][j] != '\n')

result.append(railMatrix[i][j]);

return result.toString();

}

//Decrypt the ciphertext

public static String decryptMessage(String cipher, int rails) {

// Create the matrix

char[][] railMatrix = new char[rails][cipher.length()];

// Filling the rail matrix

for (int i = 0; i < rails; i++)

Arrays.fill(railMatrix[i], '\n');

// Find the direction

boolean dirDown = true;

int row = 0, col = 0;

// Mark the places with '*'

for (int i = 0; i < cipher.length(); i++) {

// Check the direction of flow

if (row == 0)

dirDown = true;

if (row == rails - 1)

dirDown = false;

// Place the marker

railMatrix[row][col++] = '*';

// Find the next row

if (dirDown)

row++;

else

row--;

}

// Now we can produce the fill the rail matrix

int index = 0;

for (int i = 0; i < rails; i++)

for (int j = 0; j < cipher.length(); j++)

if (railMatrix[i][j] == '*' && index < cipher.length())

railMatrix[i][j] = cipher.charAt(index++);

StringBuilder result = new StringBuilder();

row = 0;

col = 0;

for (int i = 0; i < cipher.length(); i++) {

// Check the direction of flow

if (row == 0)

dirDown = true;

if (row == rails - 1)

dirDown = false;

// Place the marker

if (railMatrix[row][col] != '*')

result.append(railMatrix[row][col++]);

// Find the next row using direction flag

if (dirDown)

row++;

else

row--;

}

return result.toString();

}

// Driver function

public static void main(String[] args) {

// Encryption

System.out.println("The Encrypted Messages: ");

System.out.println(encryptMessage("hello world", 2));

System.out.println(encryptMessage("Simple Text", 4));

System.out.println(encryptMessage("Java is great", 5));

// Now decryption of the same cipher-text

System.out.println("\nThe Decrypted Messages: ");

System.out.println(decryptMessage("hlowrdel ol", 2));

System.out.println(decryptMessage("S ieTmletpx", 4));

System.out.println(decryptMessage("Jga rvseaia t", 5));

}

}

以下是上述示例的输出:

输入/输出

The Encrypted Message:

hlowrdel ol

S ieTmletpx

Jga rvseaia t

The Decrypted Messages:

hello world

Simple Text

Java is great

使用 C++ 实现

在本节中,我们将构建C++代码来展示栅栏密码的过程。它将使用必要的库,如iostream和string。程序定义了两个主要函数,encryptMessage和decryptMessage,用于使用栅栏密码加密和解密消息。这些函数使用矩阵表示来创建栅栏密码算法,根据给定的轨数以锯齿形图案填充矩阵。以下是带有栅栏密码加密和解密函数的C++程序:

示例

#include

#include

using namespace std;

// Function to encrypt a message using Rail Fence Cipher

string encryptMessage(string plaintext, int rails) {

// Create a matrix to represent the rail fence

char rail[rails][plaintext.length()];

// Fill the rail matrix with newline characters

for (int i = 0; i < rails; i++) {

for (int j = 0; j < plaintext.length(); j++) {

rail[i][j] = '\n';

}

}

// Determine the direction of movement

bool moveDown = false;

int row = 0, col = 0;

// Traverse the plaintext and fill the rail matrix

for (int i = 0; i < plaintext.length(); i++) {

if (row == 0 || row == rails - 1) {

moveDown = !moveDown;

}

rail[row][col++] = plaintext[i];

moveDown ? row++ : row--;

}

// Construct the cipher text from the rail matrix

string ciphertext;

for (int i = 0; i < rails; i++) {

for (int j = 0; j < plaintext.length(); j++) {

if (rail[i][j] != '\n') {

ciphertext.push_back(rail[i][j]);

}

}

}

return ciphertext;

}

// Function to decrypt the cipher text using Rail Fence Cipher

string decryptMessage(string ciphertext, int rails) {

char rail[rails][ciphertext.length()];

// Fill the rail matrix with newline characters

for (int i = 0; i < rails; i++) {

for (int j = 0; j < ciphertext.length(); j++) {

rail[i][j] = '\n';

}

}

// Determine the direction of movement

bool moveDown = true;

int row = 0, col = 0;

// Mark the places with '*'

for (int i = 0; i < ciphertext.length(); i++) {

if (row == 0) {

moveDown = true;

}

if (row == rails - 1) {

moveDown = false;

}

rail[row][col++] = '*';

moveDown ? row++ : row--;

}

// Reconstruct the rail matrix with cipher text

int index = 0;

for (int i = 0; i < rails; i++) {

for (int j = 0; j < ciphertext.length(); j++) {

if (rail[i][j] == '*' && index < ciphertext.length()) {

rail[i][j] = ciphertext[index++];

}

}

}

// Construct the plaintext from the rail matrix

string plaintext;

row = 0, col = 0;

for (int i = 0; i < ciphertext.length(); i++) {

if (row == 0) {

moveDown = true;

}

if (row == rails - 1) {

moveDown = false;

}

if (rail[row][col] != '*') {

plaintext.push_back(rail[row][col++]);

}

moveDown ? row++ : row--;

}

return plaintext;

}

int main() {

cout << "The Encrypted Messages: " << endl;

cout << encryptMessage("hello Tutorialspoint", 2) << endl;

cout << encryptMessage("Simple Text", 4) << endl;

cout << encryptMessage("Cplusplus is great", 5) << endl;

// Now decryption of the same cipher-text

cout << "\nThe Decrypted Messages: " << endl;

cout << decryptMessage("hloTtrasonel uoilpit", 2) << endl;

cout << decryptMessage("S ieTmletpx", 4) << endl;

cout << decryptMessage("Csapu etllirupsgs", 5) << endl;

return 0;

}

以下是上述示例的输出:

输入/输出

The Encrypted Messages:

hloTtrasonel uoilpit

S ieTmletpx

Csapu etllirupsgs

The Decrypted Messages:

hello Tutorialspoint

Simple Text

Cptrgulus lpssiea

复杂性和应用

创建消息所需的行的数量决定了栅栏技术的复杂程度。使用的行数越多,加密就越复杂。此外,如果行数更多,攻击者可能更难以找出消息中字符的初始位置。

然而,即使有多行,栅栏技术仍然不是一种非常安全的加密形式。它始终容易受到某些类型的攻击,攻击者可以快速破解它。

在现代通信中,栅栏技术并不常用。它是各种基本低安全通信协议的组成部分。但是,现在有更安全、更适合保护私人数据的解决方案。

栅栏方法的一个用途是用于儿童的教育游戏或活动。它可以是一种快速而有趣的方法来讲解密码学的 fundamentals。但是,它不应用于任何重要的安全应用。

优点

现在我们了解了栅栏方法背后的基本概念。让我们讨论一下它的优点:

栅栏方法具有许多优点,包括多功能性、简单性和易用性。

栅栏技术相对易于理解和应用,因此在不需要更高安全级别的情况下,它是基本通信的有用选择。栅栏技术可以在无需专用工具或软件的情况下手动执行。

最后,栅栏系统可以使用任意数量的行。它允许用户选择最符合其需求的复杂程度。

缺点

现在让我们讨论栅栏密码的缺点:

该技术的主要缺点是其容易受到攻击、缺乏安全性以及有效性有限。

由于任何具有基本密码学知识的人都可以轻松破解栅栏技术,因此它不是一种安全的加密形式。对于较长的消息,这种方法也不太成功。

此外,某些类型的攻击,例如已知明文攻击,可以针对栅栏方法。因此,攻击者可以简单地发起攻击来解密数据。

限制

使用频率分析,栅栏密码的加密很容易被破解。小于或等于密文长度的数字充当加密密钥。因此它非常容易受到暴力攻击。

打印页面

上一页 下一页

广告

相关推荐

王者荣耀安装包升级失败了,可能因为你的网络状况不佳,王者荣耀安装包升级失败?四大原因分析及解决方法大揭秘!
英国海淘官网allsole购物下单教程!
皇冠365bet体育投

英国海淘官网allsole购物下单教程!

⌛ 07-25 👁️ 4489
探索x5650处理器性能及应用领域(x5650处理器的性能评估和适用场景解析)
玉黄金 玉米专用增产抗病抗倒伏矮化植株籽粒饱满提质
华为MT7截屏功能详解(华为MT7截屏功能的使用方法及特点)
365打水账号怎么防止封号

华为MT7截屏功能详解(华为MT7截屏功能的使用方法及特点)

⌛ 07-04 👁️ 5816
笔记本电脑如何使用手机流量卡上网的四种方法
365bet官方下载

笔记本电脑如何使用手机流量卡上网的四种方法

⌛ 08-22 👁️ 7035
by default中文翻译,by default是什么意思,by default发音、用法及例句
365打水账号怎么防止封号

by default中文翻译,by default是什么意思,by default发音、用法及例句

⌛ 07-12 👁️ 3264
快乐番薯廖明通:要学会降低成本,缩减SKU 作者/蒙嘉怡编辑/薛向出品/茶咖观察茶饮市场经过一轮又一轮的洗牌,虽有不少商家淘汰,但也涌现出一波优秀的区域性品牌。去年...
钉钉消息多久可以撤回?
365bet官方下载

钉钉消息多久可以撤回?

⌛ 07-19 👁️ 1789