Home Crypto
Crypto

Encrypting flash content has been around for a while now, but normally it is because people being worried about their actionscript being stolen. Another valueble asset all games have are the graphics. Without these you basically don't have a game. So I figured that it might be nice if people could encrypt the images that came with their game and then decrypt them at run-time. This would have a number of advantages:

 

  • Anyone who'd decompile your swf would only see the encrypted assets and would have to spend time finding a way to decrypt these.
  • The decryption could depend on the url from where the game was played. If someone tried to host it elsewhere they'd just get a load of jumbled assets.
  • People can't crack open your swf and change the assets. If they put in new assets, then the decryption will just jumble them up. And because the encryption key is different from the decryption key they won't be able to add encrypted assets themselves!

 

I've written a AS2 class - Crypto - which enables such decrypting and encrypting. All you have to do is pass it a BitmapData object and it will return the encrypted bitmap. Then you can pass it this encrypted bitmap and it will decrypt it.

 

You can download the source files here.

 

Example 1:

import flash.display.BitmapData;
import Crypto;
var crypto:Crypto = new Crypto()
var mc0:MovieClip = this.createEmptyMovieClip("mc0",0)
var mc1:MovieClip = this.createEmptyMovieClip("mc1",1)
var mc2:MovieClip = this.createEmptyMovieClip("mc2",2)
//normal bitmap
var bmp1:BitmapData = BitmapData.loadBitmap("image2")
mc0.attachBitmap(bmp1,0)
//encrypted bitmap
var EncryptBmp:BitmapData = crypto.encryptBmp(bmp1)
mc1.attachBitmap(EncryptBmp,0)
//decrypted bitmap
var DecryptBmp:BitmapData = crypto.decryptBmp(EncryptBmp)
mc2.attachBitmap(DecryptBmp,0)
//move mc's
mc1._x=bmp1.width
mc2._x=2*bmp1.width

 

This code will create a bitmap from the symbol in the library with linkage "image2". It then creates an encrypted bitmap and saves it, and finally, decrypts the encrypted bitmap to a new bitmap. Then it displays all three:

 

 

As you can see the encrypted image is totally unrecognisable, with all the pixels jumbled up. But then it is reconstructed perfectly to the third image.

 

To use this class just create a new instance of the crypto class and then call the encryptBmp and decryptBmp methods. Both these methods return a bitmap.

 

WARNING: May take a while to process large images.

 


 

Example 2:

import flash.display.BitmapData;
import Crypto;
var crypto:Crypto = new Crypto()
var mc0:MovieClip = this.createEmptyMovieClip("mc0",0)
//normal bitmap
var bmp1:BitmapData = BitmapData.loadBitmap("image2")
mc0.attachBitmap(bmp1,0)
mc0.enc = false
mc0.onPress = function(){
if(!this.enc){
bmp1 = crypto.encryptBmp(bmp1)
mc0.attachBitmap(bmp1,0)
this.enc=true
}else{
bmp1 = crypto.decryptBmp(bmp1)
mc0.attachBitmap(bmp1,0)
this.enc=false
}
}
 

This example will create an image which will encrypt/decrypt when clicked. You can test it out below: