如何使用2键/键数加密(凯撒密码)

时间:2016-04-20 21:16:34

标签: python-3.x

如何使用2个密钥而不是1个密钥进行加密?这是为了学校。

因此,它会询问将代码移动多少,然后再询问多少次再次移动,这会进一步改变代码。

def runProgram():
    def choice():
        userChoice = input('Do you wish to Encrypt of Decrypt? Enter E or D: ').lower()
        if userChoice == "e":
            return userChoice
        elif userChoice == "d":
            return userChoice
        else:
            print("Invalid Response. Please try again.")
            choice()

    def getMessage():
        userMessage = input("Enter your message: ")
        return userMessage

    def getKey():
        try:
            userKey = int(input("Enter a key number (1-26): "))
        except:
            print("Invalid Option. Please try again.")
            getKey()
        else:
            if userKey < 1 or userKey > 26:
                print("Invalid Option. Please try again.")
                getKey()
            else:
                return userKey

    def getPees():
        try:
            userpees = int(input("Enter a key number (1-26): "))
        except:
            print("Invalid Option. Please try again.")
            getpees()
        else:
            if userpees < 1 or userpees > 26:
                print("Invalid Option. Please try again.")
                getpees()
            else:
                return userpees

    def getTranslated(userChoice, message, key):
        translated = ""
        if userChoice == "e":
            for character in message:
                num = ord(character)
                num += key
                translated += chr(num)

                savedFile = open('Encrypted.txt', 'w')
                savedFile.write(translated)
            savedFile.close()
            return translated
        else:
            for character in message:
                num = ord(character)
                num -= key
                translated += chr(num)
            return translated

        def getpranslated(userChoice, message, pees):
            pranslated = ""
        if userChoice == "e":
            for character in message:
                num = ord(character)
                num += key
                pranslated += chr(num)
        else:
            for character in message:
                num = ord(character)
                num -= key
                pranslated += chr(num)
            return pranslated

    userChoice = choice() #Runs function for encrypt/decrypt selection. Saves choice made.
    message = getMessage() #Run function for user to enter message. Saves message.
    key = getKey() #Runs function for user to select key. Saves key choice.
    pees= getPees()
    sqq= getTranslated(userChoice, message, key)#Runs function to translate message, using the choice, message and key variables)
    spp= getTranslated(userChoice, message, pees)
    dranslatedMessage = sqq + spp
    print( 'hi' + int(spp), int(sqq) )
runProgram()

0 个答案:

没有答案