Caesar Cipher-Python,使用大数字进行轮班

时间:2013-11-15 02:29:33

标签: python

我的ceaser密码需要帮助。即使移位值大于122,我也需要能够移位/解密一个字母。我的代码仅适用于小于22的移位值。当用户指定移位器的输入大于122我该怎么办?对于具有移位器123的输出应该是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,

2 个答案:

答案 0 :(得分:0)

您的代码失败,因为您在ord(char)&gt;时没有指定条件相反,你把它扔给了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。如果这个解决方案不符合您的意思,请随时在下面进行评估!

答案 1 :(得分: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()