while True:
choice = input("请输入操作编号(0-退出,1-加密,2-解密):")
if choice == "0":
break
elif choice == "1":
plaintext = input("请输入明文:")
shift = int(input("请输入位移量:"))
ciphertext = ""
for char in plaintext:
if char.isalpha():
if char.islower():
ciphertext += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
else:
ciphertext += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
else:
ciphertext += char
print("密文为:", ciphertext)
elif choice == "2":
ciphertext = input("请输入密文:")
shift = int(input("请输入位移量:"))
plaintext = ""
for char in ciphertext:
if char.isalpha():
if char.islower():
plaintext += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
else:
plaintext += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
else:
plaintext += char
print("明文为:", plaintext)
else:
print("无效的操作编号,请重新输入!")
凯撒密码代码python语言和运行操作
于 2023-06-23 12:03:33 首次发布