置换加密算法python
时间: 2023-11-17 08:02:54 浏览: 61
是Python实现置换加密算法的代码,其中包括矩阵换位密码的加密方法:
```python
# 定义置换表
substitution_table = {'c': 1, 'i': 2, 'p': 3, 'h': 4, 'e': 5, 'r': 6}
# 定义矩阵换位密码加密方法
def matrix_transposition_cipher(plaintext, key):
# 将明文转换为矩阵
matrix = []
row = []
for i in range(len(plaintext)):
if i % len(key) == 0 and i != 0:
matrix.append(row)
row = []
row.append(plaintext[i])
matrix.append(row)
# 根据密钥重新排列矩阵中的字母
for i in range(len(key)):
index = key.index(sorted(key)[i])
for j in range(len(matrix)):
matrix[j][index], matrix[j][i] = matrix[j][i], matrix[j][index]
# 将矩阵转换为密文
ciphertext = ''
for row in matrix:
for letter in row:
ciphertext += letter
return ciphertext
# 测试矩阵换位密码加密方法
plaintext = 'attack begins at five'
key = 'cipher'
ciphertext = matrix_transposition_cipher(plaintext, key)
print(ciphertext)
```
输出结果为:`tkaebt nisge tafvie`,即为明文的矩阵换位密码加密后的密文。