TypeError涉及添加

时间:2015-02-15 01:34:38

标签: python

一次性垫(Polyalphabetic Sustcip Cipher)

import pyperclip, os.path

LETTERS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXY
Z[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""

def main():
    message = """Alan Mathison Turing was a British mathematician,
logician, cryptanalyst, and computer scientist. He was highly influential in
the development of computer science, providing a formalisation of the concepts
of "algorithm" and "computation" with the Turing machine."""
    mode = 'encrypt'
    key = os.urandom(len(message))
    if mode == 'encrypt':
        translated = encryptMessage(key, message)
    elif mode == 'decrypt':
        translated = decryptMessage(key, message)

    print('%sed message:' % (mode.title()))
    print(translated)
    pyperclip.copy(translated)
    print()

    print('The message has been copied to the clipboard.')

def encryptMessage(key, message):
    return translateMessage(key, message, 'encrypt')


def decryptMessage(key, message):
    return translateMessage(key, message, 'decrypt')


def translateMessage(key, message, mode):
    translated = [] # this is gonna store the message string

    keyIndex = 0
    key = key.upper()

    for symbol in message:
        num = LETTERS.find(symbol.upper())
        if num != -1: # -1 means symbol.upper() was not found in LETTERS
            if mode == 'encrypt':
                num += LETTERS.find(key[keyIndex]) # Add if encrypting
            elif mode == 'decrypt':
                num -= LETTERS.find(key[keyIndex]) # Subract if decrypting

            num %= len(LETTERS) # Handle the wrap-around

            # Add the encrypted/decrypted symbol to the end of translated
            if symbol.isupper():
                translated.append(LETTERS[num])
            elif symbol.islower():
                translated.append(LETTERS[num].lower())

            keyIndex += 1 # Move to  the next letter in the key
            if keyIndex == len(key):
                keyIndex = 0
        else:
            # The symbol was not is LETTERS, so add it to translated as it is
            translated.append(symbol)

    return ''.join(translated)

if __name__ == '__main__':
    main()

我正在创建一个通常称为One-Time Pad Cipher的程序,但是当我运行程序时,我得到错误:

TypeError: Can't convert 'int' object to str implicitly*.

程序在第70行停止:

num += LETTERS.find(key[keyIndex]) # Add if encrypting

我该如何解决这个问题?

注意:我知道困扰我的代码的许多其他问题。

2 个答案:

答案 0 :(得分:0)

更改if-else条件:

            if mode == 'encrypt':
                num += LETTERS.find(key[keyIndex]) # Add if encrypting
            elif mode == 'decrypt':
                num -= LETTERS.find(key[keyIndex]) # Subract if decrypting

            if mode == 'encrypt':
                num += int(LETTERS.find(key[keyIndex])) # Add if encrypting
            elif mode == 'decrypt':
                num -= int(LETTERS.find(key[keyIndex])) # Subract if decrypting

这是必需的,因为num类型为int,而find()上的LETTERS正在您的计划中返回str

答案 1 :(得分:0)

您的问题是key[keyIndex]不是字符串。 string.find方法只接受字符串。

这个程序在python 2.7中运行得很好,但在python 3.2中,这就是造成这个问题的原因。

解决方案A

key[keyIndex]中包裹str()

if mode == 'encrypt':
    num += LETTERS.find(str(key[keyIndex]))
elif mode == 'decrypt':
    num -= LETTERS.find(str(key[keyIndex]))

解决方案B

你甚至可以创造一种让生活更轻松的功能。

def findKeyValue(alphabet, key, index):
    return alphabet.find(key[index].__str__())

则...

if mode == 'encrypt':
    num += findKeyValue(LETTERS, key, keyIndex)
elif mode == 'decrypt':
    num -= findKeyValue(LETTERS, key, keyIndex)