Python 实现普莱费尔格密码加密:密钥 “NETWORK” 加密明文 “we will have a meeting”
以下是一个使用 Python 实现普莱费尔格密码加密的示例代码: ```python import string def generate_matrix(key): # 去除重复字母并将字母转换为大写 key = ''.join(dict.fromkeys(key.upper())) alphabet = string.ascii_uppercase matrix = [] for char in key: if char not in matrix: matrix.append(char) for char in alphabet: if char not in matrix: matrix.append(char) return [matrix[i:i + 5] for i in range(0, 25, 5)] def find_pos(matrix, char): for i in range(5): for j in range(5): if matrix[i][j] == char: return i, j def encrypt(plaintext, key): matrix = generate_matrix(key) plaintext = plaintext.replace(" ", "").upper() ciphertext = "" i = 0 while i < len(plaintext): char1 = plaintext[i] if i + 1 == len(plaintext): char2 = 'X' else: char2 = plaintext[i + 1] if char1 == char2: char2 = 'X' i += 1 row1, col1 = find_pos(matrix, char1) row2, col2 = find_pos(matrix, char2) if row1 == row2: ciphertext += matrix[row1][(col1 + 1) % 5] ciphertext += matrix[row2][(col2 + 1) % 5] elif col1 == col2: ciphertext += matrix[(row1 + 1) % 5][col1] ciphertext += matrix[(row2 + 1) % 5][col2] else: ciphertext += matrix[row1][col2] ciphertext += matrix[row2][col1] i += 2 return ciphertext key = "NETWORK" plaintext = "we will have a meeting" ciphertext = encrypt(plaintext, key) print("密文:", ciphertext) ``` ######[AI写代码神器 | 513点数解答 | 2024-09-24 18:40:04]
- 普莱费尔格密码:用密钥“network”加密“we will have a meeting”,密文揭晓! (字节豆包 | 276点数解答 | 2024-09-24 18:36:35)244
- C 语言实现:用密钥“network”加密“we will have a meeting”的普莱费尔格密码(字节豆包 | 788点数解答 | 2024-09-24 18:38:13)223
- Python 实现普莱费尔格密码加密:密钥 “NETWORK” 加密明文 “we will have a meeting” (字节豆包 | 513点数解答 | 2024-09-24 18:40:04)363
- Python实现AES加密解密:含代码示例与关键注意点(字节豆包 | 836点数解答 | 2025-10-14 16:35:32)75
- Python 实现句子反转:单词大小写转换与数字倒转(字节豆包 | 131点数解答 | 2024-11-11 16:44:49)426
- C 语言实现句子反转:单词大小写转换与数字倒转(字节豆包 | 393点数解答 | 2024-11-11 16:45:21)554
- Python 实现句子单词逆序及大小写、数字转换,开发要点揭秘!(百度文心 | 451点数解答 | 2025-10-25 11:33:27)87
- C语言实现:句子反转,单词大小写与数字倒转处理(百度文心 | 1010点数解答 | 2025-10-25 11:34:26)117
- C语言实现句子反转:处理输入空格、内存管理与数字判断的要点(阿里通义 | 712点数解答 | 2025-10-25 11:35:19)95
- 代码逐行剖析:从数据读取到路网图创建的详细过程(字节豆包 | 421点数解答 | 2024-10-28 21:34:54)184
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)478
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362