0

My python program takes a AES key as input. For example,

\xee~\xb0\x94Lh\x9fn\r?\x18\xdf\xa4_7I\xf7\xd8T?\x13\xd0\xbd4\x8eQ\x9b\x89\xa4c\xf9\xf1

This string input must now be converted back to a byte sequences, so that the key can be used in decryption.

aes_key = str.encode(user_input) # user_input being the key is string format

When I print "aes_key" this is the output

\\xee~\\xb0\\x94Lh\\x9fn\\r?\\x18\\xdf\\xa4_7I\\xf7\\xd8T?\\x13\\xd0\\xbd4\\x8eQ\\x9b\\x89\\xa4c\\xf9\\xf1

Due to the addition "\" the key is incorrect and I can not use it for decryption.

I have tried

aes_key = aes_key.replace(b'\\\\', b'\\')

but this does not fix it. Please help.

  • What is your actual `user_input`? If I try `user_input = "\xee~\xb0\x94Lh\x9fn\r?\x18\xdf\xa4_7I\xf7\xd8T?\x13\xd0\xbd4\x8eQ\x9b\x89\xa4c\xf9\xf1";aes_key = str.encode(user_input);print(aes_key)` I get `b'\xc3\xae~\xc2\xb0\xc2\x94Lh\xc2\x9fn\r?\x18\xc3\x9f\xc2\xa4_7I\xc3\xb7\xc3\x98T?\x13\xc3\x90\xc2\xbd4\xc2\x8eQ\xc2\x9b\xc2\x89\xc2\xa4c\xc3\xb9\xc3\xb1'` instead of your output – qrsngky Oct 21 '22 at 04:40
  • Yeah that's strange, when I print(aes_key) I get an addition "\". Must be due to different python versions idk. I found a solution though, thanks for your help. – Christopher Rondeau Oct 21 '22 at 05:15

1 Answers1

0

After some guesswork of what your input might be, I got the following:

input_string = "\\xee~\\xb0\\x94Lh\\x9fn\\r?\\x18\\xdf\\xa4_7I\\xf7\\xd8T?\\x13\\xd0\\xbd4\\x8eQ\\x9b\\x89\\xa4c\\xf9\\xf1"

print(input_string) 
# \xee~\xb0\x94Lh\x9fn\r?\x18\xdf\xa4_7I\xf7\xd8T?\x13\xd0\xbd4\x8eQ\x9b\x89\xa4c\xf9\xf1

print(repr(input_string))
# '\\xee~\\xb0\\x94Lh\\x9fn\\r?\\x18\\xdf\\xa4_7I\\xf7\\xd8T?\\x13\\xd0\\xbd4\\x8eQ\\x9b\\x89\\xa4c\\xf9\\xf1'

output_bytes = bytes(input_string, "utf-8").decode("unicode_escape").encode("latin_1") 

print(output_bytes)
# b'\xee~\xb0\x94Lh\x9fn\r?\x18\xdf\xa4_7I\xf7\xd8T?\x13\xd0\xbd4\x8eQ\x9b\x89\xa4c\xf9\xf1'
qrsngky
  • 2,263
  • 2
  • 13
  • 10
  • Yes, that was the solution I was looking for, thank you very much! How do you guys just know this stuff xd. – Christopher Rondeau Oct 21 '22 at 05:12
  • Much of the idea came from https://stackoverflow.com/a/4020824/4225384 . The only significant thing I added was the last step using latin_1 encoding. I guessed it after I tried ascii and failed (`\xee` is outside ascii's 0-127), but I happened to know that it is ok in Latin1 (0-255). – qrsngky Oct 21 '22 at 05:49