What is Code Obfuscation?

Code obfuscation is a way to disguise what code is actually doing while still allowing the code to be compiled or interpreted. Obfuscating code can be used to make it harder for someone else to understand and read the code. Imagine you’re writting code that you don’t want to be stolen, such as a proprietary project, you could obfuscate the source code which would make it more difficult for someone else to steal.

The below code snippet shows a function being declared in JavaScript and then called. All this code does is print the string value “Hello John” to the developer console. This code is not obfuscated, so it can be read and understood by anyone who understands JavaScript.

A simple way to obfuscate code would be to change the variable and function names to letters. This would make the code harder to read but not impossible. The code below the same as the above snippet but with the variable and function names changed. it is still quite easy to read and understand. Imagine applying this obfucation to a larger file of code, so that there are many different variables and functions to keep track of and follow.

We could also take this obfuscated code and put it all in one line and remove all the unneeded whitespace from the source code. This makes it slightly harder to read and track, as now you need to figure out where the brackets start and end, which is harder to do without the indentation and separation of lines. Again, with this small example even this obfuscated code is still quite easy to read and understand.

There are many different ways to obfuscate code. The important thing to remember with obfuscation is that the code is still executable, the above examples can all be executed and will all result in the same ouput to the console. String encryption could be used to hide the values of string within source code, so that they are not readable. Changing the control flow can make the code more complicated to follow but still result in the same output. You could also put in dummy code, that doesn’t do anything, or any other method that makes the code harder to read.

The below code is the same as all the other examples, it prints the string “Hello John” to the console, but a more complicated obfuscation has been applied to the source code. If you take this code and executed within a browser you will see that it results in the same output, as that is the point of obfuscation. It makes the code harder to read and understand but can still be executed and run without having the be changed beforehand.

Leave a Reply