AS3 – Dynamic Variable Encryption

January 4, 2009

Same as before.

Simply we just add a variable in instead of a constant and then change the variable every frame or so.

Cons are that a variable can be changed easier than a constant.

Pros are that the protected variable is harder to hack because it changes more rapidly.


var j:int = 1;
function en(event:Event){
j/=num;
var num:Number = (Math.random()*5)+1;
j*=num;
}
stage.addEventListener(Event.ENTER_FRAME, en);

The code is exactly the same as before except encrypted differently every frame.

 

Also I noticed that on my previous post that there is actually a related article on exactly the same thing but slightly different.


AS3: Variable Encryption

December 29, 2008

AS3: Variable Encryption

What am I going to explain?

The concept and implementation of variable encryption. A simple concept
to prevent hacking of variables and variable tampering. This should be useful for games
involving currency and scores/highscores. It will prevent scoreboard hacking to an extent.
Note: This is only a minor, low security protection but it can be very useful.

How are we going to do it?

First we open up either a new .fla file or a .as file.
Then if you are using a .fla file open the actions panel.
We will now create a simple example of variable encryption, simple enough that you
can apply the same concept to any project.

Note: The following codes will work in or out of classes and their
will be no import statements as we will not need any classes other than the
base maths ones.

First we must set a constant.
Note: A constant is like a variable but it can only be set once.

const encryption = (Math.random()*5)+1;

The random function gives us a random number in order that the variable is harder to locate.
The reason we go “*5” is so that there’s a bigger number range and it just gives us some extra security.
Also the “+1” is in there is so that there is not a possibility of the encryption value being 1, if it was 1
the encryption would have no effect.

Now that’s simply the encryption set now to use it we have to multiply the variable by it.
For example.

var money:Number = 100;
money *= encryption;

Now we have the money variable encrypted. It is a practically random number because it was multiplied
by encryption.
The reason the variable is a number is because int values cannot store decimals. The variable must be
a number value for this to work properly.

Now say we wanted to make money = 500. We’d simply go.

money = 500*encryption;

Simple really, we just set the value again but with the “*encryption” to encrypt it.

Now say we wanted to add 5 to money.
We’d just go.

money += 5*encryption;

Simply we just add 5 on multiplied by the encryption number.

We can perform all normal maths functions on the variable as long as we unencrypt it then encrypt it again.
For example.

money = Math.cos(money/encryption)*encryption;

or…

money = Math.round(money/encryption)*encryption;

Then to multiply or divide money we would go.

money = ((money/encryption)*3)*encryption;
money = ((money/encryption)/5)*encryption;

Now say we want evaluate the number in an “if” statement then we just go.

if ((money/encryption)==-0.6) {
	money += -0.4*encryption;
}

All it does unencrypt the number and then evaluate it, simple.

To display money visually without encryption we go.

money_box.text = (money/encryption);

And that’s pretty much all I have to show you. Now for an example of pretty much all of those things except the
last textbox example, here they are all together and then traced.

const encryption = (Math.random()*5)+1;
var money:Number = 100;
money *= encryption;
money = 500*encryption;
money += 5*encryption;
money = Math.cos(money/encryption)*encryption;
money = Math.round(money/encryption)*encryption;
money = ((money/encryption)*3)*encryption;
money = ((money/encryption)/5)*encryption;
if ((money/encryption)==-0.6) {
	money += -0.4*encryption;
}
trace(money/encryption);

And that should trace -1.

And now for some final notes.
Don’t use this unless necessary or else you’re maths in your code gets long and complicated.
It is possible to encrypt multiple times and have dynamic encryptions through variables but that’s all
quite complicated and a bit advanced for this tutorial.
You could also to be on the safe side have different encryptions for different variables.

Thanks for reading this tutorial and I hope you find this useful.

-TutorialPoo