Matt Kenefick
Contact Me Experiments Blog Portfolio

AIR: Dynamically compiling a SWF file

Here’s something I was faced with and conquered:

I was compiling my AIR application and did not want a SWF file to be left in the directory. You could decompile it, you could try to open it on its own, and it just looked unprofessional. So in order to resolve this I thought, “Well I can just rename it to something else.” But that was not quite the effect I wanted.

What I ended up doing was dividing it up into multiple files, scrambling the data inside, encrypting each file, and spreading them out. This makes it so that no one can actually just throw your SWF into a decompiler and looks a little more legitimate.

The reason this can be done is because of ByteArray, File, Loader, and LoaderContext. What you do is:

  • Create a ByteArray
  • One-by-one write each file’s data to the ByteArray in the correct order
  • Use: loaderContext.allowLoadBytesCodeExecution
  • Create a Loader and load the Bytes in ByteArray:  loader.loadBytes( your_byte_array, loaderContext );

“allowLoadBytesCodeExecution” will actually let you load a file from its internals rather than creating an archive on the system first. If there is no archive, there is nothing to decompile.

With this method, you can split your file into multiple sections, encrypt the code in different ways, and more. Some possible uses:

  • Encrypt files using a personal salt (password) so it is password protected
  • Encrypt files using MAC address so it only works on a specified computer

I have it working well and all, but there is still more to experiment with. I just thought I would share the good news!

One Response to “AIR: Dynamically compiling a SWF file”

  1. ryan Says:

    thats wicked!!!! just the other day i converted bitmap->jpg->bytearray->jpg as a jpg preview in an app i was working on… i’m really in love with bytearrays these days and it blows my mind the seemingly endless possibilties one can achieve… thanks for the neat post…

    you could even distribute games with a licence using the above technique….

Leave a Reply