请求加密或多重加密,然后请求解密或退出

2024-04-19 01:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个程序,将文本加密到一个文件中,然后将该文件保存到python保存到的任何目录中。在这个过程中,我有一个好主意,让程序询问用户是否希望继续进行更多加密,或者停止。现在我有了一个更聪明的想法,那就是询问他们是否想要解密或退出,但是我对如何才能工作有疑问。我有大约一个半月的经验,所以请保持简单,不要使用def()或类似的东西

编辑:我现在已经让所有代码正常工作,除了else语句中的结尾。我想让用户选择重试拼写输入或退出程序。很难弄明白怎么做

#Input asking for encrypt or decrypt 
question = input("Are you Encrypting or Decrypting?: ")
encryptResult = ""

#While loop for multiple encrypts
while True:

#If statement for encrypt
    if question == 'Encrypting':

        #statements telling the user the choice they made
        print("So, you have chosen to encrpyt, very well then, lets hide some secrets.")

        #Asks user for phrase to encrypt
        encryptText = input("Enter the phrase you wish to encrypt, if you dare: ")

        #Asks user for file to save to
        createFile = input("Enter the file name you wish to save to: ")

        #For loop converting to ascii and adding 1
        for ch in encryptText:
            encryptValue = ord(ch) + 1
            bString = ""

            #While loop for conversion to binary
            while encryptValue > 0:
                remainder = encryptValue % 2
                encryptValue = encryptValue // 2
                bString = str(remainder) + bString

            #If statement moving characters to the left 1 time
            if len(bString) > 0:
                bString = bString[1:] + bString[0]
                encryptResult += bString + " "

        #File variables opening and creating a file, writing, then closing.
        filecontent = open(createFile, 'w')
        filecontent.write(encryptResult)
        filecontent.close()
        print("The deed is done, your secrets are now encrypted and safe from prying eyes.")

    #Ask the user if they would encrypt another message
        again = input("Would you like to encrypt another message? Yes or No? ")

    #If elif for another encrypt or stopping
        if again == 'Yes':
            continue
        elif again == 'No':              
            pass                      
        
        #Ask the user if they would like to decrypt now
        decryptNow = input("Would you like to decrypt now or exit the program? Decrypt or Exit? ")

        #if and elif for decrypting now
        if decryptNow == 'Decrypt':
            question = 'Decrypting'      

        elif decryptNow == 'Exit':
            break        
  
    #elif for decrypt    
    elif question == 'Decrypting':

            #Print statement after choosing decrypt
            print("Time to reveal some secrets, lets get decrypting.")

            #input varaible asking for the file to decrypt
            message = input("Enter the secret you wish to decrypt: ")

            #File varaibles opening the input, splitting, adding to list
                #and then plaintext

            decryptfile = open(message, 'r')
            code = decryptfile.read()
            wordList = code.split()
            plaintext = ""

            #For loop for shifting to the right 1 character, converting to decimal
            #removing 1 from ascii value, then converting to string and storing
            #to variable 
            for eachPass in wordList:
                eachPass = eachPass[-1] + eachPass[:-1]
                decimal = 0
                exponent = len(eachPass) - 1
                for digit in eachPass:
                    decimal = decimal + int(digit) * 2 ** exponent
                    exponent = exponent - 1

                decimal -= 1
                plaintext += chr(decimal)

            #Close file and print decoded message
            decryptfile.close()
            print(plaintext)
        
            #Ask if they wish to decrypt another message or exit
            decryptAgain = input("Please input Continue to continue decrypting, Exit to exit the program, or Encrypt to go back to encrypting secrets. Please use proper spelling.")

            if decryptAgain == 'Continue':
                continue
            elif decryptAgain == 'Exit':
                break
            elif decryptAgain == 'Encrypt':
                question = 'Encrypting'
    #If neither encrypt or decrypt is chosen, tell them to try again
    else:
        print("Error, please relaunch program and use 'Encrypting' or 'Decrypting' with proper spelling.")
        break
    
            #also would like to loop back to start if wrong button or spelling used
            #currently it just ends program
        
#Keep program open until button is pressed
print(input("Press the Alt + F4 key to exit the program."))

Tags: orandthetoyoumessageforinput
1条回答
网友
1楼 · 发布于 2024-04-19 01:35:29

这是您在编辑后检查拼写错误的代码,无需进行太多修改。它只需要将问题设置为Spelling,然后在下一轮检查它以获得新的输入。您还有一项检查是多余的,根本不需要,即:

elif again == 'No':
    pass
# Input asking for encrypt or decrypt
question = input("Are you Encrypting or Decrypting?: ")
encryptResult = ""

# While loop for multiple encrypts
while True:

    # If statement for encrypt
    if question == 'Encrypting':

        # statements telling the user the choice they made
        print("So, you have chosen to encrpyt, very well then, lets hide some secrets.")

        # Asks user for phrase to encrypt
        encryptText = input("Enter the phrase you wish to encrypt, if you dare: ")

        # Asks user for file to save to
        createFile = input("Enter the file name you wish to save to: ")

        # For loop converting to ascii and adding 1
        for ch in encryptText:
            encryptValue = ord(ch) + 1
            bString = ""

            # While loop for conversion to binary
            while encryptValue > 0:
                remainder = encryptValue % 2
                encryptValue = encryptValue // 2
                bString = str(remainder) + bString

            # If statement moving characters to the left 1 time
            if len(bString) > 0:
                bString = bString[1:] + bString[0]
                encryptResult += bString + " "

        # File variables opening and creating a file, writing, then closing.
        filecontent = open(createFile, 'w')
        filecontent.write(encryptResult)
        filecontent.close()
        print("The deed is done, your secrets are now encrypted and safe from prying eyes.")

        # Ask the user if they would encrypt another message
        again = input("Would you like to encrypt another message? Yes or No? ")

        # If elif for another encrypt or stopping
        if again == 'Yes':
            continue

        # This check is not needed
        # elif again == 'No':
        #     pass

            # Ask the user if they would like to decrypt now
        decryptNow = input("Would you like to decrypt now or exit the program? Decrypt or Exit? ")

        # if and elif for decrypting now
        if decryptNow == 'Decrypt':
            question = 'Decrypting'

        elif decryptNow == 'Exit':
            break

            # elif for decrypt
    elif question == 'Decrypting':

        # Print statement after choosing decrypt
        print("Time to reveal some secrets, lets get decrypting.")

        # input varaible asking for the file to decrypt
        message = input("Enter the secret you wish to decrypt: ")

        # File varaibles opening the input, splitting, adding to list
        # and then plaintext

        decryptfile = open(message, 'r')
        code = decryptfile.read()
        wordList = code.split()
        plaintext = ""

        # For loop for shifting to the right 1 character, converting to decimal
        # removing 1 from ascii value, then converting to string and storing
        # to variable
        for eachPass in wordList:
            eachPass = eachPass[-1] + eachPass[:-1]
            decimal = 0
            exponent = len(eachPass) - 1
            for digit in eachPass:
                decimal = decimal + int(digit) * 2 ** exponent
                exponent = exponent - 1

            decimal -= 1
            plaintext += chr(decimal)

        # Close file and print decoded message
        decryptfile.close()
        print(plaintext)

        # Ask if they wish to decrypt another message or exit
        decryptAgain = input(
            "Please input Continue to continue decrypting, Exit to exit the program, or Encrypt to go back to encrypting secrets. Please use proper spelling.")

        if decryptAgain == 'Continue':
            continue
        elif decryptAgain == 'Exit':
            break
        elif decryptAgain == 'Encrypt':
            question = 'Encrypting'

    # If neither encrypt or decrypt is chosen, tell them to try again
    elif question == "Spelling":
        question = input("Error, please relaunch program and use 'Encrypting' or 'Decrypting' with proper spelling.")
    else:
        question = "Spelling"
        # also would like to loop back to start if wrong button or spelling used
        # currently it just ends program
        
# Keep program open until button is pressed
# This should be any key not Alt + F4
print(input("Press any key to exit the program."))

相关问题 更多 >