Python 栅栏加密
时间: 2024-01-20 12:15:37 浏览: 95
以下是Python实现栅栏加密的代码示例:
```python
def fence_cipher_encrypt(plain_text, key):
cipher_text = ''
for i in range(key):
for j in range(i, len(plain_text), key):
cipher_text += plain_text[j]
return cipher_text
def fence_cipher_decrypt(cipher_text, key):
plain_text = [''] * len(cipher_text)
index = 0
for i in range(key):
for j in range(i, len(cipher_text), key):
plain_text[j] = cipher_text[index]
index += 1
return ''.join(plain_text)
plain_text = input('请输入需要加密的明文:')
key = int(input('请输入栅栏数(密钥):'))
cipher_text = fence_cipher_encrypt(plain_text, key)
print('加密后的密文:', cipher_text)
decrypted_text = fence_cipher_decrypt(cipher_text, key)
print('解密后的明文:', decrypted_text)
```
阅读全文