AES key (string) input, unable to be converted to corresponding byte sequences correctly
Question:
My python program takes a AES key as input.
For example,
xee~xb0x94Lhx9fnr?x18xdfxa4_7Ixf7xd8T?x13xd0xbd4x8eQx9bx89xa4cxf9xf1
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.
Answers:
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~xb0x94Lhx9fnr?x18xdfxa4_7Ixf7xd8T?x13xd0xbd4x8eQx9bx89xa4cxf9xf1
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~xb0x94Lhx9fnr?x18xdfxa4_7Ixf7xd8T?x13xd0xbd4x8eQx9bx89xa4cxf9xf1'
My python program takes a AES key as input.
For example,
xee~xb0x94Lhx9fnr?x18xdfxa4_7Ixf7xd8T?x13xd0xbd4x8eQx9bx89xa4cxf9xf1
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.
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~xb0x94Lhx9fnr?x18xdfxa4_7Ixf7xd8T?x13xd0xbd4x8eQx9bx89xa4cxf9xf1
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~xb0x94Lhx9fnr?x18xdfxa4_7Ixf7xd8T?x13xd0xbd4x8eQx9bx89xa4cxf9xf1'