栅栏密码python详细例子
时间: 2024-10-16 09:02:34 浏览: 41
Python脚本暴力破解栅栏密码
栅栏密码(Fence Pattern),也称为矩阵模式或阶梯密码,是一种加密技术,通过将明文字符按照一定规则分布在矩阵中,形成类似栅栏的样子。这里给你提供一个简单的Python实现,我们将使用二维列表来表示这个栅栏。
```python
def fence_cipher(text, rows):
encrypted_text = [[' ' for _ in range(len(text))] for _ in range(rows)]
direction = True
row, col = 0, 0
for char in text:
encrypted_text[row][col] = char
if (direction and row == rows - 1) or (not direction and col == 0):
direction = not direction
elif direction:
row += 1
else:
col -= 1
return ''.join(''.join(row) for row in encrypted_text)
# 示例
plaintext = "Hello, World!"
rows = 3
encrypted = fence_cipher(plaintext, rows)
print("原文:", plaintext)
print("加密后:", encrypted)
#
阅读全文