Reputation: 3802
I have a collection of values as strings and a collection of potential keys. I am trying to make 256 potential ascii strings given all potential one character strings.
my_text = '253224233b32242479'
print(my_text)
for key in string.printable: #all one character ascii charecter
key = key.encode("hex")
key = int(key, 16)
key = key + 0x200
key = hex(key)
total_string = ""
for x in range(0, len(my_text)/2): #scroll through all pairs of hex numbers
original_letter = my_text[x*2:x*2+2]
original_letter = int(original_letter, 16)
original_letter = original_letter + 0x200
original_letter_as_hex = hex(original_letter)
xor_letter = original_letter_as_hex ^ key
total_string = total_string.append(str(xor_letter))
print(total_string)
print(total_string.decode("hex"))
My issue is that the xor_letter = original_letter_as_hex ^ key
gives: TypeError: unsupported operand type(s) for ^: 'str' and 'str'
So, how can I convert for instance '25' to something that can be 'XOR'ed as a hex?
This question is a more in depth question that I asked here: How to convert a hex number that is a string type to a hex type
In short:
>>> key = '0'
>>> key = key.encode('hex')
>>> key = int(key, 16)
>>> key = key + 0x200
>>> key = hex(key)
>>> letter = '0e'
>>> letter = int(letter, 16)
>>> letter = letter + 0x200
>>> letter = hex(letter)
>>> done = key^letter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: 'str' and 'str'
How do i do this? done
should be '3e' which should convert to '>' in ascii.
One hex input '0e'
XORed by one ascii input '0'
is one ascii output '>'
I want a function that can take 2 inputs of as tpye strings and treat one as hex and one as ascii and XOR them like this site: http://xor.pw/?
Upvotes: 0
Views: 324
Reputation: 95872
You seem to be very confused. There is no "hex type", there are int
s. You still have not answered the crucial question of which version you are on. In any event, in Python 3:
>>> keys = bytes(string.printable, 'ascii')
>>> keys
b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> keys[0]
48
>>> key = keys[0]
>>> letter = '0e'
>>> letter = int(letter, 16)
>>> key += 0x200
>>> letter += 0x200
>>> done = key^letter
>>> done
62
Now, we can put this into another bytes object:
>>> bytes([done])
b'>'
Note the b
prefix. Or, we can get a Python 3 str
:
>>> bytes([done]).decode('ascii')
'>'
Or by simply using chr
:
>>> chr(done)
'>'
Upvotes: 1