python

时间:2017-11-12 13:43:41

标签: python encryption

我需要几周的时间进行编码,并且需要进行分配才能将文本输入加密到ascii,或者解密消息。我不能使用两个代码来加密和解密,这可以通过输入密码的负面版本来完成(因此功能)我自己进行了加密工作,但很难将其作为一个包工作。程序需要从用户那里获取一个选项(加密或解密,然后使用从text和key返回的值来转换main函数中的文本。 我已经在这里工作了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))

""" 运行时: 在win32上的Python 3.6.2(v3.6.2:5fd33b5,2017年7月8日,04:57:36)[MSC v.1900 64位(AMD64)] 输入" copyright"," credit"或" license()"了解更多信息。

  
    
      

RESTART:ex6 test.py =       你想要哪种模式? E表示加密,D表示解密。       Ë       请输入要加密的句子。       你好,世界       输入密码:4              Traceback(最近一次调用最后一次):         文件" C:\ Users \ ninja \ AppData \ Local \ Programs \ Python \ Python36 \ ex6 test.py",第43行,in           打印(主要(功能,消息,密码))         文件" C:\ Users \ ninja \ AppData \ Local \ Programs \ Python \ Python36 \ ex6 test.py",第6行,主要           对于消息中的x:       TypeError:'功能'对象不可迭代

             

"""

    
  

1 个答案:

答案 0 :(得分:0)

在您的错误消息中,它表示'function' object is not iterable。那是因为你正在返回text这是一个函数。

要解决此问题,只需更改第28行中的return语句即可返回sentance。您还需要在main

中返回加密邮件
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))
    return encrypt


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 sentance
        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))