Python PyCrypto encrypt/decrypt text files with AES

后端 未结 2 462
遇见更好的自我
遇见更好的自我 2020-12-23 15:20

I already have a working program, but the only thing that doesn\'t work is the decrypt_file() function I have. I can still copy the encrypted text from the file

2条回答
  •  感情败类
    2020-12-23 16:14

    In Python 3 (which you are clearly using) the default mode for files you open is text, not binary. When you read from the file, you get strings rather than byte arrays. That does not go along with encryption.

    In your code, you should replace:

    open(file_name, 'r')
    

    with:

    open(file_name, 'rb')
    

    The same for when you open the file for writing. At that point, you can get rid of all the various occurrences where you convert from string to binary and vice versa.

    For instance, this can go away:

    plaintext = plaintext.encode('utf-8')
    

提交回复
热议问题