Receive data input as Python 3 literal value

Asked

Viewed 95 times

0

Developing an encryption algorithm, at some point the user will insert the message to be decrypted. The message will be inserted in bytes, in the format b'\xf4\x88A\x0f1\xab\x91\x93\xa7-\xc9\xe7\x99:\x0c\xa3'.

The problem is that by entering this value in input, it is received in the variable as if it were a string, which generates error. Likewise, if you make a new conversion to bytes, the value received as a string will also generate error, as you will be converting a message "already converted".

There is a way to make the data be received as bytes, so that I can simply send it to the function and decrypt it?

P.S.1 When I try through the msg = bytes(input("Mensagem a ser decifrada: ")) returns the error TypeError: string argument without an encoding

P.S.2 When I pass the message in direct bytes as argument the function works smoothly: ofb(chave, b'q\xbb3\xd8\xab\x1a2AM\xb0?\x8a\n\xc6\xd1\xce', op)

1 answer

1


If I understand correctly, you want the user to type xbb3 and Python to understand this as a special character, correct? Actually this is just one way to do it, you could adopt any other convention, like #(bb3) and decode the string yourself.

To use the Python format with minimal extra work, you could try using Eval(string_do_usuario), but it is extremely insecure because the string can contain code.

>>> x = eval("'bla\x88ible'")
>>> x
'bla\x88ible'

Example of how malicious content can "leak" from Eval():

>>> x = eval("print('raqueado')")
raqueado
>>> x

Browser other questions tagged

You are not signed in. Login or sign up in order to post.