AES Cryptography for Actionscript PHP

Over the last couple of years I have used encryption on many Flash projects and until the other day, this was always a straight forward task. Cryptography has been particularly useful when to protect a server side script from fraudulent attacks. This has included things like a high score board or the progress of players through out a game.

In the past I had always used Hurlant’s crypto library which has quite a few cryptography options and includes my favorite algorithm AES. Advanced Encryption Standard is a quite secure cryptography solution and is supposedly the exact same level of encryption used by American Government’s NSA. I am not a server side guy typically but I can code C#. The nice thing about C# and AES when transacting to and from Flash is it works perfectly. PHP unfortunately does not have an official cryptography package, but thankfully there are many efforts to support this cryptography algorithm from within the community. This might sound OK, but the problem we had with this is Hurlant’s AS3 cryptography library doesn’t seem to be able to interface with the PHP AES options we tried (though it seems to work fine with other languages like C#). We did put in a good hour or so trying several mode, pad and encoding options – no dice. After a quick Google on this issue because usually if you are facing a weird issue there’s a damn good chance others have faced it already. So Google revealed to us this is a common problem and I was told maybe it’s better to find an alternative cryptographic solution. Me being, well me ? I don’t like being beaten on any technical challenge. During my Google link travels I came across this great Javascript implementation of AES which also has a PHP option. Knowing that Javascript and Actionscript fall under the same standards. I figured rather than find using a different encryption option which is less secure. I quickly ported the Javascript code to Actionscript which took about 15 mins and funnily this post took much longer than this to write ?

Actionscript AES Encryption

So here is a simple example demo with just pure Actionscript AES encryption and decryption with a block cipher of 256 bits.

PHP AES Encryption from Flash

And here is another example demonstrating Actionscript to PHP AES encryption and decryption with the same block cipher of 256 bits. Both demonstrations use the same key so if you change the keys so they do not match then they will not be able to decrypt between each other. Also in this demonstration I post the key with the text to either encrypt or decrypt, you would of course never do this when using AES normally.

I hope this is sunshine on a rainny day for someone out there, you can download the source code from the Lost In Actionscript repository or download an archived version.

Related Post