【发布时间】:2018-04-25 06:15:34
【问题描述】:
我已经进行了几周的编码,并且有一项任务需要将文本输入加密为 ascii,或解密消息。我不能使用两个代码来加密和解密,这可以通过输入密钥的负版本(因此是函数)来完成,我让加密自己工作,但很难让它作为一个包工作。程序需要从用户那里得到一个选项(加密或解密,然后使用从文本和密钥返回的值来转换主函数中的文本。 我已经在这里工作了 10 个小时,进行了大量的研究和更改元素,但似乎一直在下降。提供的任何建议都会很棒。运行时的错误我已经放在代码下了。
def main(function, message, passkey):
#takes value of mode and applies text. then uses key to encrypt or decrypt
encrypt = ""
for x in message:
if x == " ":
encrypt += " "
else:
encrypt += chr((((ord(x) - 65) + passkey % 26) + 65))
def mode():
# determines either encryption or decryption.
func = input("Which mode would you like? E for encryption or D for decryption.\n").upper()
if func[0] =='E': return 'E'
elif func[0] == 'D': return 'D'
else: print("Not a valid option. Please try again")
def text():
#depending on value of mode, either input a sentance to encrypt or an ecrypted message to decode.
if function == 'E':
sentance = input("Please enter a sentance to encrypt.\n").upper()
if all(x.isalpha or x.isspace() for x in sentance):
return text
else: sentance = input("Only uppercase alpha characters and spaces allowed. Try again.\n")
else:
return input("Enter coded message for decrypting:\n")
def key():
#depending on value of mode, enter positve key to encrypt or same key in negative form to decrypt.
if function == 'E':
return abs(int(input("Enter passkey: ")))
elif function == 'D':
return -abs(int(input("Enter passkey: ")))
function = mode()
message = text()
passkey = key()
print (message)
print(main(function, message, passkey))
""" 运行时: Python 3.6.2(v3.6.2:5fd33b5,2017 年 7 月 8 日,04:57:36)[MSC v.1900 64 位(AMD64)] 在 win32 上 输入“copyright”、“credits”或“license()”了解更多信息。
重启:ex6 test.py = 你想要哪种模式? E 表示加密或 D 表示解密。 e 请输入要加密的句子。 你好世界 输入密码:4 回溯(最近一次通话最后): 文件“C:\Users\ninja\AppData\Local\Programs\Python\Python36\ex6 test.py”,第 43 行,在 打印(主要(功能,消息,密码)) 文件“C:\Users\ninja\AppData\Local\Programs\Python\Python36\ex6 test.py”,第 6 行,在 main 对于消息中的 x: TypeError: 'function' 对象不可迭代
"""
【问题讨论】:
标签: python encryption