为什么这种转换不起作用?序数值小于255

时间:2015-09-29 19:22:04

标签: python encryption unicode vigenere

我创建了下面的文件来加密我在文件中输入的邮件。加密工作直到将列表转换为字符串。

__author__ = 'Daniel'

KEYWORD_1 = 'KWIJUHRTYFGJD'
KEYWORD_2 = 'LSKJFSDKLFJSLF'

key = KEYWORD_1 + KEYWORD_2
print(key)

def get_message_to_encrypt():
    file = open   ("C:\\Users\\Daniel\\PycharmProjects\\Encryption\\TextFile.txt", 'r')
    message = file.read()
    file.close()
    return message


def convert_message(key, message, mode):
    key_index = 0
    transformed_message = []

    for letter in message:
        letter = letter.upper()
        if mode == 'encrypt':
            total_value = ord(letter) + ord(key[key_index])
        if mode == 'decrypt':
            total_value = ord(letter) - ord(key[key_index])
        transformed_message.append(chr(total_value))
        key_index += 1
        if key_index == len(key):
            key_index = 0

    return transformed_message


def convert_message_to_string(message_list):
    message_string = None
    for letter in message_list:
        message_string = ''.join(message_list)
    return message_string


    def output_message(message):
       file = open   ("C:\\Users\\Daniel\\PycharmProjects\\Encryption\\TextFile.txt", 'a')
       file.write(message)
       file.close()

   message_to_encrypt = get_message_to_encrypt()
   message = convert_message(key, message_to_encrypt, 'encrypt')
   converted_message = convert_message_to_string(message)
   print(converted_message)
   output_message(converted_message)
   print(message)

该文件的内容是:

  

你好我的名字是丹尼尔德夫林,我想跟你问好。我希望你有一个美好的一天。

运行程序时,错误消息为:

Traceback (most recent call last):
  File "C:/Users/Daniel/PycharmProjects/Encryption/Encryption.py", line 49, in <module>
    print(converted_message)
  File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>

正如我所说,我已经打印出所有字符的序数值,但没有超过255.我不确定问题是什么。

0 个答案:

没有答案