I want to encrypt texts using PyCrypto AES and write the output ciphertext to a text file. As you know the encrypt()
output returns bytes and so I have to decode it to unicode first for it to be accepted by write()
method. My problem is both str()
and decode()
methods throw the same error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 0: invalid continuation byte
Here's part of the encrypted text I'm trying to decode to utf-8:
...\xa5- \xd9\x14_\x02\x18\x96\xde\xbb\xad\xb57>\xe5i\x82H\x9b\xcc\x19y\x0f\x89\x0c~\x81\xb5(\xcc|6\x0b\x1c\xa3\x93E\x91d\xa4\x01\xb3\x98C\xb4,\x94@,\xb0\xb0\xd7\xe2\xf7\xf6U\x129B\xd6#u\x02\xc3\xe4l\xa3\x05V\x143\xe6\x85-\x88\x7f\x14\xc0\x1e\x1d\x19vQ\xbe\xc3\xda8\x06\xaf\xb9\xb7F\x91\xa6\xba&\xcb\xd7\xd0\x12\xed\xfd\xd3n\x06\xb6\x8fZ\xccpO\x05f\x
...
If you are writing to a binary file (something like)
binfile = open('bin.out', 'wb')
wb
is the key, then you can just call write
.
If you are writing to a text file, you'll need to use base64
or something similar to encode in a format that can be included in text. base64
and hex
are the common options.
To encode in base64 do something like
import base64
b64_string = str(base64.b64encode(bytes_obj),'utf-8')
Then use b64decode
to get a string back