0

i have a code that encrypts data and then i embed it into an image, so when I was checking the encryption and decryption code that worked fine, Also i used blowfish module for encryption.

now problem is that when I embed the data into the image and extract it, It's a bytesarray in plaintext form,

b'\x98\xac\xc3ymQ_\x80\xcb\xec\x9c\x04\xc3@\x88\x93`j\x05\x96\x9d\xcb\x0ec\xb2\x9b(\xd9@\x9fI\x00\xc7h\xe3\x83\xbd0\r\xad}*t'

the above is a bytesarray in plaintext form, So if I try to convert it to bytesarray again it will re-encode it and put the '\' between the characters that already have it and now this new bytesarray is not a normal bytesarray, and the data is corrupted.

bytearray(b"b\'\\x98\\xac\\xc3ymQ_\\x80\\xcb\\xec\\x9c\\x04\\xc3@\\x88\\x93`j\\x05\\x96\\x9d\\xcb\\x0ec\\xb2\\x9b(\\xd9@\\x9fI\\x00\\xc7h\\xe3\\x83\\xbd0\\r\\xad}*t\'")

So my question is that how do I typecast or convert the str to a bytesarray? without changing the data.

3
  • 1
    You can use eval(expr) to achieve that. Be warned, though, that this can by made to execute any code, so make sure you know what you submit to it. Also, I think you shold try to understand why you get the result as a string. If you can, it is better to fix this at the source.
    – JohanL
    Jun 26, 2021 at 7:04
  • ohh that, i purposefully made it to a string so that i can convert it to binary. Jun 26, 2021 at 8:36
  • Please edit your question to share a minimal reproducible example. Where your string comes from? Maybe it's a XY problem and could be prevented rather than solved?
    – JosefZ
    Jun 26, 2021 at 11:07

2 Answers 2

1

If I can understand your requirement then the following code snippet could help. Applied module ast — Abstract Syntax Trees:

import ast

bys=r"b'\x98\xac\xc3ymQ_\x80\xcb\xec\x9c\x04\xc3@\x88\x93`j\x05\x96\x9d\xcb\x0ec\xb2\x9b(\xd9@\x9fI\x00\xc7h\xe3\x83\xbd0\r\xad}*t'"
print( '↓↓↓', type(bys))
print( bys)
byb = ast.literal_eval(bys)
print( byb)
print( '↑↑↑', type(byb))

Result: .\SO\68139330.py

↓↓↓ <class 'str'>
b'\x98\xac\xc3ymQ_\x80\xcb\xec\x9c\x04\xc3@\x88\x93`j\x05\x96\x9d\xcb\x0ec\xb2\x9b(\xd9@\x9fI\x00\xc7h\xe3\x83\xbd0\r\xad}*t'
b'\x98\xac\xc3ymQ_\x80\xcb\xec\x9c\x04\xc3@\x88\x93`j\x05\x96\x9d\xcb\x0ec\xb2\x9b(\xd9@\x9fI\x00\xc7h\xe3\x83\xbd0\r\xad}*t'
↑↑↑ <class 'bytes'>
2
  • Ok so as the JohanL said i used eval now a new problem arose, apparently eval has some memory limitations and hence it breaks when the byte string is a certain length does literal_eval has that problem covered? Jun 26, 2021 at 10:38
  • It doesn't work, is there a manual way of doing it, i get eol error because the byte string is too long. Jun 26, 2021 at 10:46
0

Okay anyone else trying to achieve this the other answers are correct provided you are working with a small byte string.

But as I wasn't and needed the theoretical unlimited byte string size (I wanted it to be at-least 1000 lines of 25 words of 4 letters on average each).

So if you want to do the same then, convert the Byte string to it's equivalent binary by below code:

def bitstring_to_bytes(s):
    return int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')

Then reconstruct the byte string by

def bin2byte(bin_string):
    return bin(int.from_bytes(bit_string, byteorder="big"))

this can overcome the limitation with eval and can go way further than that.

I referred these posts from stack overflow: byte string to binary: How can I convert bytes object to decimal or binary representation in python?

binary to byte string :Convert binary string to bytearray in Python 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.