使用python程序诊断问题(基于关键字密码加密和解密字符串)
这是一个课堂作业,我必须创建一个程序,该程序能够使用关键字对消息进行加密和解密,该关键字将被重复和修剪,以匹配原始消息的长度。程序使用ASCII字符集,并相应地对值进行加减 我曾经尝试过这段代码,有两种变体,它们非常接近于工作状态,并且有一些问题我无法诊断,我将下面的代码包括在内以供参考,我想知道是否有人能够提出建议,以使程序更高效,并帮助我诊断问题 变更1:使用python程序诊断问题(基于关键字密码加密和解密字符串),python,debugging,python-3.x,ascii,Python,Debugging,Python 3.x,Ascii,这是一个课堂作业,我必须创建一个程序,该程序能够使用关键字对消息进行加密和解密,该关键字将被重复和修剪,以匹配原始消息的长度。程序使用ASCII字符集,并相应地对值进行加减 我曾经尝试过这段代码,有两种变体,它们非常接近于工作状态,并且有一些问题我无法诊断,我将下面的代码包括在内以供参考,我想知道是否有人能够提出建议,以使程序更高效,并帮助我诊断问题 变更1: def encrypt(message, keyWrd): characters = "abcdefghijklmnop
def encrypt(message, keyWrd):
characters = "abcdefghijklmnopqrstuvwxyz"
enc = ""
for x in message:
if x in characters:
for y in keyWrd:
crShift = ord(y)
num = ord(x)
num += crShift
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
enc = enc+chr(num)
else:
enc = enc + x
return enc
def encrypt(message, keyWrd):
characters = "abcdefghijklmnopqrstuvwxyz"
enc = ""
for x in message:
if x in characters:
for y in keyWrd:
crShift = -ord(y)
num = ord(x)
num += crShift
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
enc = enc+chr(num)
else:
enc = enc + x
return enc
def functHand(passThr, message, keyWrd):
message = message.lower()
if passThr == 1:
return encrypt(message, keyWrd)
elif passThr == 2:
return decrypt(message, keyWrd)
def error(errMSG):
if errMSG == 1:
print("---------------------\nInvalid Data entry")
start()
def keyPlaster(msg, keyWrd):
keyword = ''
while len(keyWrd) < len(msg):
keyword += keyWrd
keyword = keyword[:len(msg)]
return keyword
def start():
print("---------------------")
try:
option = int(input("What would you like to do?:\n1)Encrypt\n2)Decrypt\n3)Exit\n>> "))
except ValueError:
error(1)
if option == 1:
msg = input("What message would you like to encrypt?: ").replace(' ', '').lower()
keyWrd = input("What is the keyword?: ").replace(' ', '').lower()
keyword = ''
while len(keyWrd) < len(msg):
keyword += keyWrd
keyword = keyword[:len(msg)]
enc = functHand(1, msg, keyword)
print("The encrypted message is: ", enc)
start()
elif option == 2:
msg = input("What message would you like to decrypt?: ").replace(' ', '').lower()
keyWrd = input("What is the keyword?: ").replace(' ', '').lower()
keyword = ''
while len(keyWrd) < len(msg):
keyword += keyWrd
keyword = keyword[:len(msg)]
msg = functHand(2, msg, keyword)
print("The decrypted message is: ", msg)
start()
elif option == 3:
print("---------------------\nEXITING\n---------------------")
raise SystemExit
else:
error(1)
start()
变式2:
def menu():
try:
option = int(input("Please select an option:\n1)Encrypt\n2)Decrypt\n3)Exit\n>> "))
except ValueError:
print("-----ERROR-----")
################
raise SystemExit
while option >= 1 and option <= 3:
if option == 1:
menuOpt = 1
handler(menuOpt)
elif option == 2:
menuOpt = 2
handler(menuOpt)
elif option == 3:
menuOpt = 3
handler(menuOpt)
else:
print("-----ERROR-----")
################
raise SystemExit
def handler(menuOpt):
if menuOpt == 1 or menuOpt == 2:
origInput = input("Please enter a string: ").replace(' ', '').lower()
manipInput = input("Please enter a keyword: ").replace(' ', '').lower()
keyPlaster(manipInput, origInput)
if menuOpt == 1:
enc(menuOpt, origInput, keyword)
print("The encrypted message is: ", enc)
elif menuOpt == 2:
dec(menuOpt, origInput, keyword)
print("The decrypted message is: ", dec)
elif menuOpt == 3:
print("-----EXITING-----")
raise SystemExit
def keyPlaster(manipInput, origInput):
global keyword
keyword = ''
while len(keyword) < len(origInput):
keyword += manipInput
keyword = keyword[:len(origInput)]
return keyword
def enc(menuOpt, origInput, keyword):
characters = "abcdefghijklmnopqrstuvwxyz"
enc = ""
for x in origInput:
if x in characters:
for y in keyword:
crShift = ord(y)
num = ord(x)
num += crShift
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
enc = enc+ chr(num)
else:
enc = enc + x
return enc
def dec(menuOpt, origInput, keyword):
characters = "abcdefghijklmnopqrstuvwxyz"
dec = ""
for x in origInput:
if x in characters:
for y in keyword:
crShift = -ord(y)
num = ord(x)
num += crShift
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
dec = dec+ chr(num)
else:
dec = dec + x
return dec
menu()
提前感谢。您的预期产量是多少?您的实际输出是什么?@retarity这方面的一个示例输入是“Computingisfun”和“GCSE”的关键字,这应该给出一个“JRFUBWSNLLKBQ”的输出。<这是用于加密函数的,解密应该以JRFUBWSNLLKBQ作为消息,GCSE作为关键字的相反方式工作。但我遇到的问题是,程序似乎无法正确输出,并且我很难对其进行诊断,因此非常感谢您的帮助:另请参见: