0

I am making an asymmetrical cryptography system.

I want to be able to paste the encrypted message into the python script and then unencrypt it.

Here's the problem.

The encrypted message contains escape characters and is in bytes.

Encrypted message: b'\xa0\xcc&g\x82\x86\xb6\x18\x8bm\xd1\xa2\xa9?\x97xp\xcd\xb2P\x06u\x07a\x169x\xe8\x9f~\x891\x98H\x80\x15\xcdq\xa9\xba\xe0\x07\x0c\x9b\xc9\xe8\xd6\xa80+o\x02c\x98\x1ct%R\xdf})\xe2\xa3\xad\x8a\x9aZ\xd1\xb0\xbfe\x85\xf3\x1a1 \xad\xeb+3Pa\xder\x8ex?\xf7+\xf14 \xa0\xff\x05\x93\xc4\x1c(\x99\x9e\xa3VS\xd2\xa2\xa7\xae/Q\'\xde\xa3\xad\xe3\xb4!\xa1\x8a\xc1\x10\xe7\xf0\xbd2\xd0\x0f5\xa4@\xca,^(\x16N\x16E\xb9\x85\xd7\xf4\x89\t\x8c\x8b\x1a\x9d*\x97\xe5\xf6\xae\x93\x81(\x9b\x94\tq\x01\x04f@W\x14\x97=\x89-v\xf6\\\x15\x97\xe2S\x93}\xf4\x97\x83@d\x08KT\\"n+\x1d\xa2I\xea\x00/\x1d\xc3\x0c\xa5\xb7D\x1d\xff\xfa\x99\x9b\x13\xf0\x9b8/\r\xe2\xc6\xcdHM\x9e\x9e5+\xc9)1\xc8\x01\xf5by\xa6\xf4\xff\x9e[\x9cL\x14\xe7\xaa;\xc8Y\xe53\xcb\xd0.\xd5\xcf\xed\x1d\xd0\xff\xd1'

So, if i get the user input and convert it into a byte string, this will happen.

# assume user input is \xa0
user_input = input()
print(user_input.encode())

# Prints out \\xa0

This treats the \ as a literal, but I want it to treat it as in escape character.

Or to put it simply, how can I take "hello\nworld" from the user using the input method and print out

hello
world

instead of hello\nworld

This is what I have tried

print(input_str.encode())
# I get b'hello\\nworld'

decoded_str = input_str.encode().decode('unicode_escape')
print(decoded_str)
# I get 'hello\nworld'

decoded_str = input_str.encode().decode('unicode_escape').encode()
print(decoded_str)
# I get b'hello\nworld'

Bryan
  • 25
  • 7
  • "decoded_str = input_str.encode().decode('unicode_escape')" - this one works for me, and is the expected way to solve the problem, as explained in the linked duplicate. I can't reproduce the supposed issue with it. Make sure the test input actually contains a backslash, and make sure that you understand what the test output actually contains. – Karl Knechtel Apr 12 '23 at 15:16

0 Answers0