0

I'm trying to send a variable with a cypher which at the beginning is a byte type variable but then I have to convert it to string and then back to byte.

b'j\xd7cv2/\t\xb5\x9d0\x9bh\xf6\xbd\x08\xd4a\xc2)\x1d\xd4\xae\xa8\xfd\xe9\xe9\xb2\xe9\xa9\x87\x1e\x97.\xf1LT\xf9\xf6\xba[\xab\x10\xab\x98\xf8\x81\xd4\xd9!D\xe1.\x9a\xbcG\xab"\xec\x05}9{\xee\x82=\xb2\x1f5a\xb9\xfd\xcbE}\x1d[\xd3_z\xaf\x1a\xcc\xbc\xfdQ\xde\xbe\xbd\xe8\xe3\xcd\x0e\xa2\xc6\x1a\x9f\x18\x12G\xb2\xa8g\xbfK\x8a\x0e\xf6\xf5)y\xbc6\x03Z^|\xf5\xf1\t\x9dk\xefUo6\xd7\x05\xc7\xef\x941\r\x02\xfb\xc1_\x19/\xea\x81\xb64s\xc7\x92\xbf\x1aL\xff\'\xb6\x87o\xd0\\\x90\x98\xadn.\x02g\xa0\xde\x1b\xcem`)7Hf\x9fn*Kq\xa6ee\x8b\xfc\x88\xee+\xa6\x18#c\x1aE\xd3\xbcw\xabT\xa33*\xd8b\xc5\x90=H\xe0\x02\xb0>P=%\x91\xa5\x98M\xa5\x04\xfd\xa8\xa3R\xc0)+\xa6K\xf9\x9c\xb1\xd1\xf4\xf8J\xf5\xe1W7\xde\xbd7\xc59\xcc\xb3J\xe2\xd5l\xdbZ\xa3*\xa2Q4'

I already have tried to covert it to base64 and to utf-8

now i have this code:

type(cipher)
> bytes
cipher = str(cipher)
cipher.decode()
str.encode(cipher)

this returns me bytes but diferent from the original

b'b\'Z@\\xa0\\xa9\\x05\\xad"\\xa6,\\xe8\\xa4\\xa0*\\xbe\\x0e\\xd3Y\\xb9g\\xbb#\\x97\\xe2\\x9b&\\x8b\\xc0\\x8d\\xb8[Z\\xce\\rm\\xc7\\xe0^\\x0cJm\\xf0y?\\xd8\\xb2\\xc5+$\\x05_\\xa33\\xe7Q\\x82\\xa5E\\xb6\\x0f\\x18\\xd9\\xbf\\x08\\xe7\\xd0vG$}w\\xc4 \\xe4\\xf8*\\x0eR\\xd3\\xda\\x12\\x95\\xd2\\x1cJ3\\x02,#\\xfe\\x17\\x02\\xc9\\xb8\\xdcdt\\xfb\\xa1\\xc6\\xf6\\xe4\\x129\\xfd\\xe1U\\xf9\\xb9\\xa1j\\x08\\xb3\\xbe{\\xf9\\xfd\\x9bji\\xa7\\xea0\\xe8G\\xde_@\\x90\\x07Q\\x8b\\xfblQUd\\x1bhGv\\xbck\\x8bS\\xdc3\\x83L\\xa1dA\\xe6Mi\\n\\x92y\\xd4\\xe1\\xf9\\xe5\\x86\\x9cg\\xe7e \\x9bp\\x8f\\x1f\\xe9W|\\xf4\\xaf\\xcd\\xfa\\xd1\\xf3\\xdd\\xd5\\x83\\x87\\x04<\\xfc\\x1ch\\x99wG5\\xd2\\xb2\\xd8~\\xc7dd\\x9fZFGe\\xef\\xdf\\x90W\\xef\\x01\\x03\\xd7\\xc6=\\n\\xe3\\xdbQV2H\\xa57\\xa5\\xf6n`\\x0b\\xb4K\\x90g2\\xc6\\xb8P\\x12\\xce\\xff\\x9a\\xcb\\x94\\xae\\xf3\\x9a\\x8a\\x9e\\xb8^\\xa4\\xa1\\x84\\xfc\\x07$\''

converting from bytes to utf-8 gives me this error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 3: invalid continuation byte

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Casimiro
  • 25
  • 8
  • Possible duplicate of [Convert bytes to a string?](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – snakecharmerb Mar 29 '19 at 12:23
  • Calling `str` on a `bytes` instance doesn't decode it to `str` - you need to call the `bytes` instance's `decode` method - see the duplicate target. – snakecharmerb Mar 29 '19 at 12:46
  • @snakecharmerb it doesn't work decoding with utf-8 -> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 3: invalid continuation byte – Casimiro Mar 29 '19 at 12:51
  • 2
    If the bytes are the enciphered text then they will need to be deciphered before decoding. Or maybe they are encoded with some other encoding. There isn't enough information to know. – snakecharmerb Mar 29 '19 at 12:59
  • Echoing @snakecharmerb. You should not try to arbitrarily convert bytes to str without knowing and/or specifying an encoding. The bytes are just jumbled 1s and 0s without an encoding attached to them. – Brad Solomon Mar 29 '19 at 20:34

0 Answers0