Reputation: 1436
I was trying to work this crypto puzzle someone posted, and I was trying to automate it in python3, and hitting some trouble. It's driving me crazy I can't figure out how to do this.
It involves doing a base64 decode, which gives a bytes type.
I want to iterate over this collection of bytes, and do a bitwise negation of each byte. Which is to say, 10010001 (145) should become 01101110 (110), for example. The ~ operator is giving me two's compliment, though, and producing negative ints. I want to work with unsigned bytes.
How can I do this in python 3?
psudocode for what I want to do:
[ bitwise_negate(x) for x in my_byte_array ]
Upvotes: 1
Views: 1406
Reputation: 11992
you can use the python base64
module to first decode the string into its corresponding bytes then you can xor each byte against 255 (11111111) to get the resulting ascii pointcode and use chr
to get the actualy character
import base64
string = 'l4uLj8XQ0J2Wi9GThtDMzJGUmbKluw=='
decoded = base64.decodebytes(bytes(string, encoding='utf-8'))
print("".join([chr(item ^ 255) for item in decoded]))
Upvotes: 3