给定纯文本消息和数字键,使用 Rail Fence 算法对给定文本进行加密/解密。
围栏密码(也称为之字形密码)是转置密码的一种形式。它的名字来源于它的编码方式。
例子:
加密
输入:“GeeksforGeeks”
键 = 3
输出:GsGsekfrek eoe
解密
输入:GsGsekfrek eoe
键 = 3
输出:“GeeksforGeeks”
加密
输入:“保卫东墙”
键 = 3
输出:dnhaweedtees alf tl
解密
输入: dnhaweedtees alf tl
键 = 3
输出:保卫东墙
加密
输入:“立即攻击”
键 = 2
输出:atc toctaka ne
解密
输入:“atc toctaka ne”
键 = 2
输出:立即攻击
加密
在转置密码中,重新排列字母的顺序以获得密文。
在围栏密码中,明文被向下和对角地写在想象围栏的连续铁轨上。
当我们到达底部轨道时,我们沿对角线向上移动,到达顶部轨道后,再次改变方向。因此,消息的字母以锯齿形方式书写。
在编写完每个字母表之后,将各个行组合起来以获得密文。
例如,如果消息是“GeeksforGeeks”并且轨道数 = 3,则密码准备为:
.'.它的加密将按行进行,即 GSGSEKFREKEOE
解密
正如我们之前看到的,rail fence cipher 中的列数仍然等于纯文本消息的长度。并且键对应于导轨的数量。
因此,可以相应地构建轨道矩阵。一旦我们得到矩阵,我们就可以找出应该放置文本的位置(使用相同的方式交替上下对角移动)。
然后,我们明智地填充密文行。填充后,我们以之字形的方式遍历矩阵,得到原始文本。
实现:
设 cipher-text = “GsGsekfrek eoe” , Key = 3
矩阵中的列数 = len(cipher-text) = 13
行数 = 键 = 3
因此原始矩阵将是 313 ,现在将带有文本的地方标记为 '' 我们得到
- _ _ _ *
_ *
下面是一个使用上述算法加密/解密消息的程序。
# Python3 program to illustrate
# Rail Fence Cipher Encryption
# and Decryption
# function to encrypt a message
def encryptRailFence(text, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix
# to distinguish filled
# spaces from blank ones
rail = [['\n' for i in range(len(text))]
for j in range(key)]
# to find the direction
dir_down = False
row, col = 0, 0
for i in range(len(text)):
# check the direction of flow
# reverse the direction if we've just
# filled the top or bottom rail
if (row == 0) or (row == key - 1):
dir_down = not dir_down
# fill the corresponding alphabet
rail[row][col] = text[i]
col += 1
# find the next row using
# direction flag
if dir_down:
row += 1
else:
row -= 1
# now we can construct the cipher
# using the rail matrix
result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])
return("" . join(result))
# This function receives cipher-text
# and key and returns the original
# text after decryption
def decryptRailFence(cipher, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix to
# distinguish filled spaces
# from blank ones
rail = [['\n' for i in range(len(cipher))]
for j in range(key)]
# to find the direction
dir_down = None
row, col = 0, 0
# mark the places with '*'
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
# place the marker
rail[row][col] = '*'
col += 1
# find the next row
# using direction flag
if dir_down:
row += 1
else:
row -= 1
# now we can construct the
# fill the rail matrix
index = 0
for i in range(key):
for j in range(len(cipher)):
if ((rail[i][j] == '*') and
(index < len(cipher))):
rail[i][j] = cipher[index]
index += 1
# now read the 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:
dir_down = True
if row == key-1:
dir_down = False
# place the marker
if (rail[row][col] != '*'):
result.append(rail[row][col])
col += 1
# find the next row using
# direction flag
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
# Driver code
if __name__ == "__main__":
print(encryptRailFence("attack at once", 2))
print(encryptRailFence("GeeksforGeeks ", 3))
print(encryptRailFence("defend the east wall", 3))
# Now decryption of the
# same cipher-text
print(decryptRailFence("GsGsekfrek eoe", 3))
print(decryptRailFence("atc toctaka ne", 2))
print(decryptRailFence("dnhaweedtees alf tl", 3))
# This code is contributed
# by Pratik Somwanshi
输出:
atc toctaka ne
GsGsekfrek eoe
dnhaweedtees alf tl
GeeksforGeeks
attack at once
delendfthe east wal