Skip to content
Advertisement

Cannot decode encrypted rsa message python3

I’m encoding a message with rsa.encrypt but then I cannot convert the encrypted data to a str with .decode(). That’s a bit strange because the encrypted data is a bytes string and there shouldn’t be any problem converting that to a str.

data = [self.id, data, self.my_pubkey] # actually don't care about type of components, they are correct

My code:

JavaScript

But, I’m getting this error:

JavaScript

Advertisement

Answer

Decoding a byte string as utf-8 only makes sense if you know the bytes represent valid utf-8 data. utf-8 will not decode arbitrary byte strings: there’s a specific format (when bytes are >= 0x80, they are interpreted as either “start” or “continuation” bytes and must follow certain patterns; see the Wikipedia page for more information).

On the other hand, encrypting data (using almost any encryption algorithm) will generate random-looking byte strings that almost certainly will not be valid utf-8.

The solution is to treat the output of the encryption process as a byte string – do not attempt to decode it to a string, as it will not make sense as a string. Python provides the bytes/str distinction precisely for this kind of case: bytes are for binary data (e.g. encrypted data), strings are for textual data.

For dumping binary data (as byte strings) into JSON, I suggest using an encoding like Base64 to encode the bytes into ASCII, rather than trying to use a string. This will be more efficient and much easier to debug.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement