AES Cryptography for Actionscript & PHP

enigmaOver 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 code.google repository or download an archived version.

Have your say | Filed Under: Source | The Post ›

FITC 2010

FITC_Am10_300x250-02It’s been a while between posts, I have plenty of excuses but shortly it’s been a really busy year and I have a lot of draft posts I must finish. After my first major conference talk at FITC last year I decided on my own regards that it might be the first and last time that I might do conferences of this calibre again. My reason for making this decision is complicated, but one major reason is because of the sheer amount of time and effort that go’s into pre-planning a conference talk and those who know me professionally I am just *slightly* the control freak and unfortunately time short! So after turning down many offers both local and abroad, I have decided to talk at FITC 2010 and resume conferencing mostly because the organizers are just so damn nice there and who can say no to a trip to Amsterdam – not me.

This year my talk will be much more exciting than last years, despite this the venue was p-a-c-k-e-d. If you missed it, Grant Skinner is pretty much covering a majority of this content in his FITC session Quick as a Flash. Though this year I am excited to say my talk is on Physics and Behavioral Character Motion. This talk will cover how to animate characters in a life like and improvisational using Actionscript and Flash. Though my talk will be best experienced with having a small amount of Actionscript knowledge. I will try to demonstrate tips and tricks to show how you can get these nice motion effects with very little maths ability and some experience in Flash alone.

My talk will begin by explaining how to look clever by making things move with super easy physics equations and how you can combine this knowledge with the new CS5 physics features and the existing IK stuff. Once we have a few things moving and colliding around properly I will then go on to show how to go about making things move under strategic behavioral motion constraints. This will then start to give some indication of AI with things like formation patterns. Lastly we will take the AI a step further by adding needs, desires and personality to our characters with creative and very simple application of complicated sounding things like finite state machines and decision trees so the behavioral motion makes sense.

I think the talk should be really exciting and hopefully inspiring for anyone attending, says me ‘I would go’. Also for free I will be giving attendees the source code from the conference so you can play around at home. I hope to see you there and maybe even have a drink together to talk geek. Mind you had better get in quick because if you are any where in Europe and slightly keen to attend, snatch a bargain with an early bird ticket for FITC Amsterdam because these tickets usually sell like hot cakes.

Custom blend modes for Flash 10

chameleonIn the last few months I have been working on a collection of blendmodes for Flash 10 which extend the limited set of blendmodes currently and inconsistently supplied by Adobe applications. This can help to achieve all kinds of motion effects which I won't go into detail here but if you know a little about After Effects you will know that some of these are rather exciting.

If you are familiar with the Adobe product line there is inconsistency between Photoshop/Illustrator/Flash/Fireworks/InDesign and support for various subsets of blendmodes which is rather annoying especially for us Flash guys. This project overzealously named the 'Ultimate Blend Mode Collection' is an attempt to standardize this unofficially on Adobes behalf. So marrying up Photoshop designs and Flash layouts with unsupported blendmodes won't be nearly as annoying as has been previously. Further to this, these custom blendmodes allow for control to exaggerate and undo the blendmode effects. What this means is you do not have to double up the blendmode effect by duplicating the DisplayObject instance with the same blendmode you can just multiply the single blendmode effect - simple and quite effecient.

So far I have completed the following custom blend modes :

  • Add
  • Hard Mix
  • Hardlight
  • Glow
  • Subtract
  • Stencil Luma
  • Extrapolate
  • Difference
  • Average
  • Lighter Color
  • Lighten
  • Color Dodge
  • Stencil Alpha
  • Stamp
  • Softlight
  • Silhouette Luma
  • Silhouette Alpha
  • Screen
  • PinLight
  • Phoenix
  • Overlay
  • Negation
  • Multiply
  • Linear Light
  • Linear Dodge
  • Linear Burn
  • Darken
  • Desaturate
  • Exclusion
  • Color Burn
  • Alpha
  • Darker Color

These are working though have issues of dirty-ness on alpha channels (explained below) for blending layers :

  • Reflect
  • Freeze
  • Heat
  • Vivid Light

These custom blend modes are currently in development :

  • Color
  • Hue
  • Luminosity
  • Saturation
  • Dissolve

How to use custom blend modes

I have only provided a method to use these custom blend modes included with the 'Ultimate Blendmode Collection' for Flex and CS4 users. This is because the PBJ files are compiled into a SWC so you will need to be able to reference SWCs from your FLA or SWF compiler to use the custom blend modes I have mentioned. The syntax to apply a custom blend mode can be achieved via any of the following approaches :

Actionscript:
  1. import flash.display.BlendModeShader;
  2.  
  3. myDisplayObject.blendShader = BlendModeShader.SOFTLIGHT;

or

Actionscript:
  1. import flash.display.BlendModeShader;
  2. import flash.display.shaders.*;
  3.  
  4. myDisplayObject.blendShader = new BlendModeSoftlight();

or you can manipulate the shader

Actionscript:
  1. import flash.display.BlendModeShader;
  2. import flash.display.shaders.*;
  3.  
  4. var customBlendMode: BlendModeSoftlight  = new BlendModeSoftlight();
  5. customBlendMode.multiply = 2;
  6. customBlendMode.alpha = 0.5;
  7. myDisplayObject.blendShader = customBlendMode;

Tips for writing your own custom blend modes

Here are some potentially huge gotchas and lovely time savers for those wanna be pixel shader writers (like myself) :

  1. Un-multiply alpha before carrying out your shader algorithm. You might already know that Flash pre-multiplies the alpha channel onto the rgb channels this means that if you want to know what the actual rgb components are you will need to do the following :
    Actionscript:
    1. void
    2. evaluatePixel()
    3. {
    4.         float2 uv = outCoord();
    5.        
    6.         float4 src1Rgba = sampleLinear(src1, uv);
    7.         float src1Alpha = src1Rgba.a;
    8.         if(src1Alpha>0.0) src1Rgba.rgb *= 1.0/src1Alpha; //This line un-multiplies the alpha from the rgb channels
    9. }

  2. When your done manipulating your blending layer against the base layer it's more than likely you will need to composite your new blending layer and base layers together while preserving the alpha components. The following should be useful for this :
    Actionscript:
    1. void
    2. evaluatePixel()
    3. {
    4.         ......
    5.  
    6.         interpolateAlpha = src2Rgba.a;
    7.         inverseAlpha = 1.0-interpolateAlpha;
    8.  
    9.         dst.rgb = src2Rgba.rgb+src1Rgba.rgb*inverseAlpha;
    10.         dst.a = min(1.0, src1Alpha+src2Alpha);
    11. }

  3. Make sure you clamp the components you are altering between 0.0 and 1.0 outside these will give you unusual results between tests made with the Pixel Bender Toolkit and from a published Flash movie. This bit of code ought to fix this :
    Actionscript:
    1. void
    2. evaluatePixel()
    3. {
    4.         float3 minRgb = float3(0.0, 0.0, 0.0);
    5.         float3 maxRgb = float3(1.0, 1.0, 1.0);
    6.        
    7.         ......
    8.  
    9.         dst.rgb = clamp(dst.rgb, minRgb, maxRgb);
    10. }

  4. Because Flash premultiplies alpha and I explained earlier about why you may need to un-multiply alpha from the rgb components. It's important to know from this there is a certain degree of losiness and this article by Mario is a great Actionscript explanation (completely unrelated to shaders) of this and is one among many others crying out for help (like me). You may notice in the example below which demonstrates these custom blend modes (or in your own tests) a certain degree of image dirty-ness and this dirty-ness is due to this. I am still working on a way to reduce or get around this - any ideas are certainly welcome - and I've highlighted the shaders above most affected by this.
  5. When applying a shader the input sources for the blending and base layer are automatically set. This is great but doesn't allow for setting any further image4 inputs. From my tests if I do try to do this then the Flash Player crashes every time. This is bug and generally bugging me because I can't complete other custom blendmodes based on random math like the Dissolve blendmode

How to get the custom blend modes

I have placed my PBJ, PBK and SWC files on a code.google project. I have also put together a brief getting started page. If you want to get an idea of what's possible using these custom blend modes the simple example below should help to demonstrate this. Also make sure you try out a few of the various base and blending layers I have set up to see what does what.

References

If you are interested in writing your own blend modes I used quite a few sources in order to collect the algorithms used in the examples seen above - here are the main sources :
http://www.pegtop.net/delphi/articles/blendmodes/
http://blog.mouaif.org/?p=94
http://www.nathanm.com/photoshop-blending-math/

Export Layers to SWF with JSFL

jsflIt's embarrassing but until the other week I hadn't a reason to do anything (beyond tinkering) with JSFL. JSFL can be especially useful for simplifying or automating tasks in the Flash IDE and that isn't breaking news. I think I understand why I haven't been using JSFL too much until recently. The major problem I discovered is the documentation is very poor, if you search JSFL you get exactly diddly squat on how to use JSFL. And alas Googling JSFL aint much better with the term "JSFL Reference" the only adobe link is this one. Considering this help reference is 5 years old doesn't inflate confidence in learning the latest and greatest JSFL has to offer - this is really pathetic Adobe. If someone can find an official decent resource on JSFL it would be appreciated, SURELY that isn't it :) So despite not being able to find a great JSFL reference Eventually I was referred to this great JSFL reference. The JSFL script I needed to write was really quite basic and for Actionscript developers writing JSFL it is very familiar territory so I was somehow blindly confident.

So I will describe the usefulness of this JSFL script by first explaining the task or problem at hand it solved for me and hopefully you find it useful also.

The Problem

I had a rather complex vector map (12 MB) in a SWF. Embedding the whole map would be obviously be a BAD idea for loading times but fortunately only very small portions of this map had to be used at any time. Though the suker punch to this idea is that the map isn't grouped to these portions - sigh.

The Solution

So I started with some unavoidable and rather laborious instance naming grinding which I did within Flash. Once I had completed this I had literally 100's of Movieclips associated to Classes that needed to be published and when I hit this I  realised - JSFL I love you! So I distributed these MovieClips to layers then ran this script and viola all the layers are published as swfs using the layer name as the published SWF name - ba da boom ching :)

The way this script works is that publishes any layer that isn't a folder or a guide on the main timeline. Then sets all the layers to guides, then loops through the layers desired to be outputted to SWF then changes them to a normal layer so that only this layer is the only one in the published SWF. Once the publish loop is completed all layers are then set back to their original states.

Rather simple I thought and a huge time saver for mindless SWF publishing. To run this JSFL simply double click it or drag it onto Flash then you will be prompted to select the output directory. Then sit back, relax or even take the evening off and tell your project manager you're hard at work!

You can view the source or download this script here - get it while it's hot!

FITC Pixel Bender Source Code

benderIn my efficiency FITC talk I covered some topics regarding programmatically rendering to the DisplayList - efficiently and practically. This covered some simple examples showing what can be done in PixelBender with Shaders including blendModes, lighting effects and transitions. If you would like the source code for these examples you can get them from my code.google repository.

These shader examples only represent a taste of what's possible using Shaders but I think what's most important is to note these shader examples are really practical unlike a lot of the psychedelic effects you may of seen. For me I especially found that introducing more blend modes has made the process of matching designs in Flash a whole lot easier. I will shortly post a complete list of brand spanking new blend modes for Flash via Shaders with open source code so that everyone can take advantage of these blend modes.




Me

I am Shane McCartney an Aussie Flash Developer who's currently working in London on a contractual basis. If you would like to check out some of the work I have done recently, go to my folio at shanemccartney.com.

Donate

If you find the source code or information provided on this site of such use that you would like to donate please do it is appreciated.

Your donation will help us to provide even more source code to the community and cover the cost of time to develop and maintain the source code we provide, thank you.


Meet me at

FITC Amsterdam