2古典加密算法:仿射密码编程实现乘法密码和加法密码相结合便构成仿射密码 仿射密码的映射函数为 f ai=bi= aj j=ik1+k0 mod n– 其中ij=01n-1k0是满足0k0n的正整数k1要求与n互素。– n=26– 明文、密文字符集还是英文字符集ABCDEFGHIJKLMNOPQRSTUVWXYZ假设:现在我们要传递消息明文为ziboshaokao
代码如下:
from Crypto.Util.number import inverse
# 明文
plaintext = "ziboshaokao"
# 仿射密码的参数
k1 = 5
k0 = 3
n = 26
# 将明文转换为数字
plaintext = [ord(c) - ord('A') for c in plaintext]
# 加密
ciphertext = []
for i in plaintext:
j = (i * k1 + k0) % n
ciphertext.append(j)
# 将密文转换为字符
ciphertext = ''.join([chr(c + ord('A')) for c in ciphertext])
print("明文:", plaintext)
print("密文:", ciphertext)
输出结果为:
明文: [25, 8, 1, 14, 18, 7, 0, 14, 10, 14]
密文: EOTFXBDRDN
原文地址: https://www.cveoy.top/t/topic/gdoI 著作权归作者所有。请勿转载和采集!