Well, since there was no option for "tutorial" in the type of post, it has been created as a simple thread...
Obfuscation - confusion resulting from failure to understand
obfuscated code - Code that is hard to read
In this tutorial, I will show you how to convert something readable, to something completely unreadable, but it still works, exactly the same. There are multiple thing you can do in Python to achieve obfuscation, one of the main ones being one-liners and exec(). I have not understood the lambda function yet, but you can still pull off loops and if statements that have one statement, by putting them on the same line; I.e.:
x = 1
# This:
if x == 1:
print("x is equal to 1")
# Becomes this:
if x == 1: print("x is equal to 1")
Both work exactly the same.
Next, we shall take a look at a string:
toexec = '''x = 0
if x == 0:
print("Hello world")
'''
You might be asking, well, what does this have to do with anything? Here's the answer, the exec()
can execute this string as actual code. If you type in toexec
in the interpreter, you're shown this: 'x = 0\nif x == 0:\n print("Hello world")\n'
Now, if you type this: exec(toexec)
You are now presented with this: 'Hello world'
Nifty, eh? Another way of obfuscating your code is through use of ord() and chr(). That toexec code can also be represented …