????????凯撒明文的要点在于循环移位,在计算机中实现利用ASCII码增加或者减少来对明文进行加密或者解密,凯撒加密类比于一次函数y=x+a,其中x代表字符的ASCII码值与a或者A的ASCII码值的差,a代表循环移位的位数。
????????
import time
def encryption(word,key):
list_text = list(word)
encrypt = []
for letter in list_text:
if letter.isalpha():#判断是否为英文字符
if letter.isupper():#判断是否为大写
text = letter.lower()#是大写就转化为小写
n = chr(((ord(text) - ord("a") + key) % 26) + ord("a"))
encrypt.append(n.upper())#加密后返还大写字符
elif letter == " ":#加入输入明文中的空格
encrypt.append(" ")
else:
n = chr(((ord(letter) - ord("a") + key) % 26) + ord("a"))
encrypt.append(n)
print(f"The Output is {''.join(encrypt)}!\n")
def decryption(word,key):#解密是加密的逆过程
list_text = list(word)
decrypt = []
for letter in list_text:
if letter.isalpha():
if letter.isupper():
text = letter.lower()
n = chr(((ord(text) - ord("a") - key) % 26) + ord("a"))
decrypt.append(n.upper())
elif letter == " ":
decrypt.append(" ")
else:
n = chr(((ord(letter) - ord("a") - key) % 26) + ord("a"))
decrypt.append(n)
print(f"The Output is {''.join(decrypt)}!\n")
type_writter = ['L','e','t',"'",'s',' ','P','l','a','y',' ','G','a','m','e','!']
for n in type_writter:
time.sleep(0.4)
print(n,end='')
time.sleep(1)
print("\n********凯撒明文********")
while True:
time.sleep(1)
choice = int(input('''Please enter your choice:
1.Encrypt The Word
2.Decipher The Word
3.End The program
Your Chioce:'''))
if choice == 1:
plaintext = input("Enter The Plaintext:")
key = int(input("Enter the Key:"))
time.sleep(1)
encryption(plaintext,key)
elif choice == 2:
ciphertext = input("Enter The Ciphertext:")
key = int(input("Enter the Key:"))
time.sleep(1)
decryption(ciphertext,key)
else:
time.sleep(1)
print("The process is DONE!!!")
break
|