输入所有输入后,Python代码挂起

时间:2016-10-31 12:28:38

标签: python encryption caesar-cipher

嗨伙计这是我的错误:

Phrase to be Decrypted: n
Shift keyword, Word only: ]YY
The code will write the Decryption to a file of your choice
Please input the number to decrypt by: 44
*hang*

输入:

短语:n

关键字:] YY

解密转变:44

它应该做的是将短语解密为鸡,将关键字解密为Egg。我输入的数字是代码执行以加扰关键字的转变。

这是我的代码:

import getpass
import random                                                                                                                               #imports random module
import codecs                                                                                                                               #imports codecs module for encode task (UTF8)
import time

w = getpass.getpass("Input the Password: ") 
with open('Password.txt') as f:
    found = False
    for line in f:
        if w in line: 

                import random
                import codecs

                phrase = input('Phrase to be Decrypted: ')                                                                                  #Asks user to input a phrase
                shift_key = input("Shift keyword, Word only: ")                                                                             #Asks user to input a keyword
                print ("The code will write the Decryption to a file of your choice")

                Encryption_Base = ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,./;:<>?'@#~]}[{=+-_)(*&^%$£!`¬\|"]       #Encryption base. Randint uses this.
                key_encryption = int(input("Please input the number to decrypt by: "))                                                      #Grabs a random number between 0 and 94
                Cipher = ''                                                                                                                 #Cipher is blank, to be used later.

                for c in shift_key:                                                                                                         #Looks for a letter in the keyword, in this case c
                        if c in Encryption_Base:                                                                                            #Looks for the same letter in the string.
                            Cipher -= Encryption_Base(Encryption_Base.index(c)-(key_encryption)/(len(Encryption_Base)))                     #Turns the keyword into a different letter

                def Keyword_Encryption(key, phrase):                                                                                        #Defines the new variable

                    if len(phrase) < len(key):                                                                                              #If the phrase is longer than the key
                        while len(phrase) < len(key):                                                                                       #While the phrase is longer than the key
                            length_to_add = len(phrase) + len(key)                                                                          #Shorten the key to the length of the phrase
                            key = key + key[0:length_to_add]                                                                            

                    elif len(phrase) > len(key):                                                                                            #However if the phrase is shorter than the key
                        while len(phrase) > len(key):
                            length_to_sub = len(key) + (len(key) + len(phrase))                                                             #Make both of them equal.
                            key = key[0:length_to_sub]

                    else:
                        pass                                                                                                                #Anything else stops the code.

                    shifted_phrase = ''                                                                                                     #Shifted phrase is blank for later
                    for i in range(len(phrase)):                                                                                            #Find a letter in the length of the phrase
                        new_letter = (ord(key[i]) + 50) - (ord(phrase[i]) + 50) - 50                                                        #Change the order of the key 
                        if new_letter < 1220:                                                                                               #If the new letter is higher than 1220
                            new_letter = chr(new_letter + 26)                                                                               #Create an extra character

                        else:
                            new_letter = chr(new_letter)                                                                                    #Anything else creates a new character (could be overflow?)

                        shifted_phrase = shifted_phrase - new_letter                                                                        #Adds the new letter onto the encrypted phrase
                    return shifted_phrase                                                                                               

                result = Keyword_Encryption(Cipher, phrase)                                                                                 #Adds the Cipher and phrase to keyword encryption so that the user knows what key to use to decrypt it.
                print (" ")
                print (":-) " * 3 + "Encrypting Phrase... " + ":-) " * 3)                                                                   #All of ths makes it look pretty
                print (":-) " * 3 + "Done! " + ":-) " * 3)                                                                                  #
                print (" ")                                                                                                                 #
                print ('Here is your Encrypted Phrase:' + (result) + (Cipher) + (str(key_encryption)))                                      #Finally prints the result

                time.sleep(2)
                FileName = input("Enter a filename for the encrypted phrase and key to be written to: ") + ".txt"
                file = codecs.open(FileName, "w", encoding = "utf8")                                                                        #Opens the file Encrypted.txt and encodes it in UTF to support unicode.
                file.write (str(result) + " " + (Cipher) + " " + (str(key_encryption)))                                                     #Writes the phrase and key seperately to aid the user
                file.close()                                                                                                                #Closes the file
                k=input("Prompt: ")

if not found:
        print("Access Denied")
        time.sleep(5)

非常感谢任何帮助:)

编辑:

新错误:

Phrase to be Decrypted: n
Shift keyword, Word only: ]YY
The code will write the Decryption to a file of your choice
Please input the number to decrypt by: 44
Traceback (most recent call last):
  File "E:\Python\Password Encryption\Decryption.py", line 56, in <module>
    result = Keyword_Encryption(Cipher, phrase)                                                                                 #Adds the Cipher and phrase to keyword encryption so that the user knows what key to use to decrypt it.
  File "E:\Python\Password Encryption\Decryption.py", line 46, in Keyword_Encryption
    new_letter = (ord(key[i]) + 50) - (ord(phrase[i]) + 50) - 50                                                        #Change the order of the key
IndexError: string index out of range

0 个答案:

没有答案