# 模26加法
def encrypt(plaintext, key):
# 将明文和密钥转换为大写字母
plaintext = plaintext.upper()
key = key.upper()
# 将明文中的非字母字符去除
plaintext = "".join([i for i in plaintext if i.isalpha()])
# 生成密文
ciphertext = ""
for i in range(len(plaintext)):
# 计算移位量
shift = ord(key[i % len(key)]) - 65
# 对当前字符进行移位并添加到密文中
ciphertext += chr((ord(plaintext[i]) - 65 + shift) % 26 + 65)
return ciphertext
# 提示用户输入明文和密钥
plaintext = input("请输入明文:")
key = input("请输入密钥:")
# 加密并输出密文
ciphertext = encrypt(plaintext, key)
print("密文:", ciphertext)