0

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/?

Community
  • 1
  • 1
Rorschach
  • 3,684
  • 7
  • 33
  • 77

1 Answers1

1

You seem to be very confused. There is no "hex type", there are ints. 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)
'>'
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • I'm pretty sure the OP is on Python 2 (otherwise `.encode("hex")` wouldn't have worked..) – DSM Feb 09 '17 at 03:48
  • @DSM maybe, I do think `.encode('hex')` existed on early versions of Python 3. I cannot recall. – juanpa.arrivillaga Feb 09 '17 at 03:50
  • Well, it's not in 3.1, which is the oldest I have at hand. Then how about this: if he's using 3, `range(0, len(my_text)/2)` would have failed too. :-) – DSM Feb 09 '17 at 04:02
  • Honestly, I run it using python3, python2, sudo python3, and sudo python2 till it works. – Rorschach Feb 09 '17 at 05:46