0

我需要有关我的停止者密码的帮助。即使移位值大于 122,我也需要能够移位/解密一个字母。我的代码仅适用于小于 22 的移位值。当用户指定移位器的输入大于122 我该怎么做?对于带有移位器 123 的 a,输出应为 r

k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext:  ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array

for i in range(0,len(original)): ##Now seperating each character to add k
    char = original_as_array[i]
    charint = ord(char)

    if charint >= 65 and charint <=90:
        cipher_int = ((charint-65 + k) % 26)+65
        code_char = chr(cipher_int)
        print code_char,

    elif charint >= 97 and charint <=122:
        cipher_int = ((charint-97 + k) % 26)+97
        code_char = chr(cipher_int)
        print code_char,


    else:
        print char,
4

2 回答 2

0

您的代码失败,因为您没有在ord(char)> 122 时指定条件。相反,您将其扔给了 else 子句。

您可以只切割零件<= 122

for k in range(20):
    for i in range(0,len(original)): ##Now seperating each character to add k
        char = original[i]
        charint = ord(char)

        if charint >= 65 and charint <=90:
            cipher_int = ((charint-65 + k) % 26)+65
            code_char = chr(cipher_int)
            print code_char,

        elif charint >= 97:
            cipher_int = ((charint-97 + k) % 26)+97
            code_char = chr(cipher_int)
            print code_char,


        else:
            print char,

k设置为 18,这将产生r. 如果此解决方案不是您的意思,请随时在下面发表评论!

于 2013-11-15T02:49:10.213 回答
0
#Caesar Cipher

#Function that reads and writes data to the file, and calls the encryption and decryption methods.
#It isolates the key and choice of the user, and then calls the appropriate function.
def isolate():
    #Open input file in read mode
    fin = open('Input3.txt', 'r')
    #Open output file in write mode
    fout = open('Out1.txt', 'w')
    #Create a list that holds each line of the file as an element of the list
    container = fin.readlines()

    #For loop that goes through each line of the file (contianed in list) and isolates the choice and key of the user
    #Then it removes the key and choice from the split input list and calls the appropriate method.
    for i in container:
        # List of words, with whitespace and trailing '\n' removed
        words = i.split()

        # Interpret and remove the last two words
        key = int(words[len(words) - 1])
        choice = words[len(words) - 2]
        words.remove(str(key))
        words.remove(choice)

        # Re-join the words of the message. The message may differ from the original if there is consecutive whitespace.
        message = ' '.join(words)
        message = message.upper()

        # Encrypt or decrypt to fout
        if choice == 'e':

            fout.write(encrypt(message, key) + '\n')

        else:
            fout.write(decrypt(message, key) + '\n')

    #Close the file to make sure data is written properly to the file.
    fout.close()

#Encryption method, which takes two parameters, the users message and key, and returns the newly encrypted message.
def encrypt(message, key):
    #Empty string that will contain the encrypted message.
    encrypted_message = ""
    #Alphabet string, which contains all the letters of the alphabet at their index locations.
    alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    #For loop that goes through each character of the users message and, if a space, adds it straight to the encrypted message or encrypts it through modular division.
    for i in message:
        #If the character is a space, add it to the final message.
        if i == ' ':
            #Concatenate the space to the final message
            encrypted_message += ' '
        #If the character is not a space, determine the final locatin out of index range, % by 26 and re-add the character at this index to the encrypted_message.
        else:
            #Determine the final location of the character (out of index range)
            location = key + alpha.index(i)
            #% this location by 26 to determine the location within the proper index.
            location %= 26
            #Finally, concatenate the letter at this location to the final message.
            encrypted_message += alpha[location]

    #When the function is called, have it return the final encrypted message.
    return encrypted_message


#Decryption method that takes two parameters, the users message and key
def decrypt(message, key):
    #Create a string that reverses the alphabet, since we are moving backwards when we decrypt.
    reverse_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[::-1]
    decrypted_message = ""

    #For loop that runs through each element of the users message and, if space,  adds it to the message, or encrypts using % division, then concatenates to final message.
    for i in message:
        if i == ' ':
            decrypted_message += ' '
        else:
            #Determine the final location out of index range of the encrypted character
            location = key + reverse_alpha.index(i)
            #% this position by 26 to determine the index location in range of the letter.
            location %= 26
            #Finally, add the letter at this location to the decrypted message.
            decrypted_message += reverse_alpha[location]

    #Converts the decrypted message into lowercase form.
    decrypted_message = decrypted_message.lower()

    return decrypted_message

#Call the function isolate5(), which initiates the program.
isolate()
于 2014-01-18T03:26:50.567 回答