Frage

The ultimate objective is to flip the last bit of byte at position X. Let's say we start off with a string from a website and we base64 decode it so it's something we can work with:

IV = "2RDs+xm/AxtYj+XViJmBPQ=="
IV = base64.b64decode(IV)

That gives us something that looks like (����X��Ո��=) which looks like garbage but it's something we can work with. The best way to work with this would be to put it in a list.

woop = list(IV)

Let's say in this example that we're looking to flip the last bit at the 2nd byte. Python starts counting from zero so we need to reduce our counter by 1. Otherwise, we'll be pointing our program at the 3rd byte! To flip anything you XOR it by 1. Now here's my problem...

CounterX = CounterX - 1
woop[CounterX] = chr(ord(IV[CounterX]) ^ 1)
woop = "".join(woop)
IV = woop

I'm pretty sure that flips the entire byte. How can I tell it to flip only the last bit of the byte at IV[CounterX]?

War es hilfreich?

Lösung

  • base64.decode returns a string
  • strings in python2x are always bytes!
    • if you are using python3 you may want to call bytes(IV) to ensure 8 bit chunks ... but Im not sure I dont use python3 much
  • some_int XOR 1 will only flip the lowest byte
    • as demonstrated by print bin(0xff ^ 0x01)

so again we return to what makes you think that this is not the behavior you are seeing? can you give us an example that demonstrates the issue?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top