The enumerate function can be used to XOR characters of text with characters of a key. The example below shows how this can be done. In my example, my text length matches my key length.
Example code:
obfuscated = “” clear_text = “” text = “Something to obfuscate.” key = “a key of some sort here”
# Enumerate our text to get the position of each character and the character itself for index, char in enumerate(text):
# Use the ord function to obtain unicode codes for the text and key characters # XOR those together, and then use the chr function to return the resultant character # Keep appending characters to obfuscated variable obfuscated += chr(ord(char) ^ ord(key[index]))
# Print a UTF-8 encoded result print(“Obfuscated text is:”, obfuscated.encode())
# To undo it, enumerate the obfuscated text for index, char in enumerate(obfuscated):
# Do the same steps as before, except this time append to clear_text variable clear_text += chr(ord(char) ^ ord(key[index]))
# Print our clear text to the screen print(“Original text was: “, clear_text)
Output:
Obfuscated text is: b’2O\x06\x00\rH\x06\x08GS\x1b\x02EO\x11\t\x07\x07C\t\x11\x17K’ Original text was: Something to obfuscate.