0
votes

I'm trying to decode the string but getting an error, and here is part of the code:

#!/usr/bin/env python
import rsa

def constLenBin(s):
    binary = "0"*(8-(len(bin(s))-2))+bin(s).replace('0b','')
    return binary

data = 'apple'
(pubkey, privkey) = rsa.newkeys(1024)
crypto = rsa.encrypt(data.encode(), pubkey)
crypto = crypto.decode()
binary = ''.join(map(constLenBin,bytearray(crypto, 'utf-8')))

Traceback (most recent call last):
File "stdin", line 1, in module
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 0:
invalid start byte

1
And what is the question? - RemcoGerlich
But the error is correct, \x99 isn't valid UTF8, why do you think it is? - RemcoGerlich
It was a complex problem. I was using rsa module - Bill
To be able to decode, you need to know what the encoding it is. And it's impossible to guess from just a single byte. - RemcoGerlich
I'm using rsa module. And I want to encrypt str by using rsa.encrypt(), it return bytes type. Then I hide it into a image. However, it must be str before. - Bill

1 Answers

3
votes

As Remco notes, \x99 is not valid UTF8 byte. You need to specify encoding name, for example:

a = b'\x99'; a = a.decode('latin-1'); print(a)