Converting bytes to string to bytes - Python Help - Discussions on Python.org
Converting bytes to string to bytes Python Help help krisrajzlive (Raj) October 27, 2021, 12:57pm 1 How can I convert bytes to string to bytes back? Here’s what I’m trying: from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding message = b"A message I want to sign" signature = private_key.sign( message, padding.PSS( mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH ), hashes.SHA256() ) converting the signature using base64 encode and decode methods to string.
This is a requires process in the application for serialization. But not sure how can I convert back to bytes to verify the signature. nachoXXL (Nacho Xxl) October 28, 2021, 7:43am 2 Hi Raj, I’m not sure what you want to do here, but know that you only can verify a (received) signature by generating it again using the same technique and then compare it with what you created. You cannot ‘decode’ a signature. N. cameron (Cameron Simpson) October 28, 2021, 8:51am 3 I expect Raj is talking about encoding the signature bytes in base64, and later decoding the base64 back into the bytes. krisrajzlive (Raj) October 28, 2021, 5:57pm 4 I am working on small blockchain assignment which requires me to cast the signature from bytes to string and the vice versa.
When I call the verify signature method I have only string representation of the string as per my statement in the original thread. steven.daprano (Steven D'Aprano) October 29, 2021, 3:13am 5 Hi Raj, What sort of results do you expect when you cast your bytes to strings? The bytes b’abc’ have a natural interpretation as ASCII text, ‘abc’, but what happens if your bytes include non-text bytes? b'\0\x03\xFF\xE1' What string do you want?
Normally we decode bytes to string using a text-based codec, such as ASCII, Latin-1, UTF-8, UTF-16 etc. But most of those will not work for arbitrary binary byte strings, and the few that do (such as Latin-1 and a few other legacy codecs) will give you meaningless mojibake as the result. So we need more information about what you expect to happen when you decode the bytes to a string. Do you also expect to encode the string back to the bytes, and do you expect it to losslessly roundtrip? krisrajzlive (Raj) October 29, 2021, 9:30am 6 File1: transaction_message = { ‘message’ : ‘Hello world’ } signature = b’\x80t\xd9\x17\xf8\xb1\x80\x86\xd7\xbfy\xaf\xfb>\xcb\xe4}W\xb6\xd2\x8a\x1c"\xd6\x1d\xde\xc3S)G\x1f.\xed\x01\x9c\xbbM\x9c.I\x89X1m\xf7\xfd\xa9\xcf~rz\x02QZ\x9fE\x9f-\xb6k\x99\xe0q\x19\xa2\x072\xa1\xcd\x10\x7f\xb8F`\xce\xc1|\x94B\x1b\xba\x9e\xad-\x98\xcd\xae\xee\xc9\xc4\xe5\x9c78\xd5%\xa6\xbf\x9b/\xa2\xaf9\x08\xe6\xae\x0e\xb77C\xcf\xa38\xc9\xf5C^*)$\x81\xcc\x19\xfe\xe0\xf5\xa1\xc7\x033\xc5\xc9\x05\x90=\xef^8eFI\x92u\xd6\xce\xb2\x9e\xc83\xf7\x83\x94i\x85\xf8\x11.c9\xa9:\xba\xc0\x8dP\xa4UI\x93\xbc\xe7S\x06\lx \x96cZ[,\x80\x969\xa7\xfb\x06s\xc3\x1e\xf1\xe4C\xac\xa4\xbb\x96\xfc\x14\x1a\x9a\xbd\xb5\xdf\xa6&\xf7\xd3\xbd==OX+\xb5\x00\xa7\xec\xd3\xd8\x08&\xc0\n\x0f\xf5\x81EZ\x04(*JUV\xb7\xf9w.6\x9b\xa5rN\xdd\x1c\x84\xb9\xe3\xb5\x1c5\xbc{l’ I want to convert this to string and vice versa and tried: encoded_signature = base64.b64encode(signature) decoded_signature = base64.b64decode(encoded_signature) Not sure this is the correct way to convert byte string to string. transaction = {‘message’: transaction_message, ‘signature’: decoded_signature } File 2: self._transactions = list of transaction hash_string = ‘-’.join([ str(self._index), json.dumps(self._transactions, sort_keys=True) ]) … verify signature goes here by iterating all transactions … json,dump fails if I do not convert signature bytes into string.
And want convert it back to its original form bytes to verify the signature using public_key.verify method. nachoXXL (Nacho Xxl) October 29, 2021, 10:33am 7 Hi Kraj, You’re almost there, you have to convert the bytes from base64 to a string: as_string = str(base64.b64encode(signature), 'utf8') And back: signature == base64.b64decode(bytes(enc, 'utf8')) N. fungi (fungi) October 29, 2021, 11:45am 8 b64encode will only ever output byte values in the ASCII range, so it’s probably more appropriate to use ‘ascii’ instead of ‘utf8’ when recasting to/from a str type.
Another simpler solution is to just use hexidecimal string representation: encoded_signature = signature.hex() decoded_signature = bytes.fromhex(encoded_signature) Hope that helps. krisrajzlive (Raj) October 29, 2021, 12:47pm 9 Plum! That’s working!! Thanks indeed @nachoXXL and @fungi