Sunday, February 14, 2010

Flex Asynchronous Ciphering using as3crypto

Built myself a little password safe application based on Adobe AIR, and one of the features I wanted to put into it was to store encrypted files. I downloaded the as3crypto package from google code, and had my encryption/decryption functionality ready and working in an hour.

It was working smoothly right until I tries to use  it on a slightly larger file (a 6.7Mb image file) when the application froze and i got the “Not responding” message on the title of the application window. The application defrosted after a while and the file was processed properly, but it had the GUI locked for far too long.

No worries, I said to myself, all I need is some background process to do the dirty work while the GUI stays free for working with me.

Its a shame that Adobe Flex do not support multi threads.

Eventually I worked around this problem and wrote a component that interlaces calls to whatever function you give it, so the GUI wont freeze. (see my post on flex multi threading workaround).
Then I took to the task of making my ciphering class work a chunk at a time. After a superficial profiling session I came to the conclusion that the reading and writing of the files are by far cheaper operations then the ciphering, so the steps used in the solution are:

  • Initiate everything: read source file into memory
  • Interlace on encrypt and decrypt for a small chunk at a time
  • Finalize: write file 

Note: I made a lenient assumption that I wouldn't store a file I cant open all at once.

When I put everything together, it didn't work. I kept crashing on decryption and got an error saying: Error: PKCS#5:unpad: Invalid padding value. expected [252], found [152]
I had a vague  idea what this meant, but no idea what I was doing wrong. The answer came to me an hour after i posted a mayday question on stack overflow. so I posted the answer too:
the encryption and decryption actions change the actual chunk size. in the processChunck method, the ByteArray length before encryption was 16400 and after changed to 16416. using the decrypt changes the ByteArray length from 16416 back to 16400.
hence the solution i chose was to use two numeric values, one for each method used.
I have no idea why this happens, its not the padding (tested it with Crypto.getPad("none")).
So now I'm sharing it with the world (or rather with the 6 other people who viewed the question in stack overflow so far)
Requirements:

  • Download and add the as3crypto package from google code  into your project
  • Create an interlacing component that would call the async-crypto in pace with the GUI frame rate (or use what i wrote: here)
 
Download the source for the CryptoAsync and add it into your project.
Usage example:
var aCrypt:CryptoAsync = new CryptoAsync("C:/screen.jpg","C:/xxx.crp",key,CryptoAsync.ENCRYPT);
aCrypt.getSet();
asyncThread.exec(aCrypt.run,aCrypt);
var aCrypt:CryptoAsync = new CryptoAsync("C:/xxx.crp","C:/result.jpg",key,CryptoAsync.DECRYPT);
aCrypt.getSet();
asyncThread.exec(aCrypt.run,aCrypt);
When:
  • key is a string of your liking
  • asyncTread is an instance of AsyncThreadComponent you can find here

good luck

Friday, February 12, 2010

Flex AS3 Multi Treading Workaround

Its well known that the current versions of Flex (using either the flash or AIR players) do not provide support for multithreading. Everything you do is loaded on the same main thread that is running the GUI too.
This restriction means that if you have to do a dutiful task, the GUI actually stops responding to the user and  your app may even get the “Not Responding” label in the title bar while its off processing.
The way to bypass this issue is to follow two guidelines
  • Break down the big process in to smaller, manageable runs. this means that if you have a loop that each iteration is taxing, you would want to call that loop one iteration at a time.
  • Call the single iteration in a way that interlaces with the GUI refresh rate.
Here is a simple GUI component that you may place on your application, to invoke calls on some other process without hampering the responsiveness of the GUI.
The method you call by this component has to be in the nature of:
public function runFunction(data:Object):Boolean

Download zip file for AsyncThreadComponent

Usage:
Paste this on your app:
<view:AsyncThreadComponent id="asyncThread" />
When you want something to be invoked, create an object and pass the method:
asyncThread.exec(theObject.runFunction,theObject);

Independent on Sundays

Friday, December 18, 2009

The TimeLabel component

The time label component is something I slapped together to show accumulated time. Its an enhanced ActionScript3 Label with a simple API for making it tick.

Input a numeric value of time in milliseconds, and the TimeLabel will display it grouped nicely in days, hours minutes and seconds.

Invoke the time label’s start/stop to make it count seconds in any direction you like.

Untitled-1

This is the image of the TimeLabel, I surrounded with the controls that operate its API.

The API is as following:

Set and retrieve the numeric value representing time in milliseconds (note; there is no numeric validation, so its up to you):

  • function set data(value:Object):void
  • function get data():Object

Start and stop the timer:

  • function start():void
  • function stop():void

Determine if the timer goes up or down:

  • function increment():void
  • function decrement():void

download source code

Tuesday, November 17, 2009

Software Disclaimer Sample

I’ve found this text:

This SOFTWARE PRODUCT is provided by THE PROVIDER "as is" and "with all faults." THE PROVIDER makes no representations or warranties of any kind concerning the safety, suitability, lack of viruses, inaccuracies, typographical errors, or other harmful components of this SOFTWARE PRODUCT. There are inherent dangers in the use of any software, and you are solely responsible for determining whether this SOFTWARE PRODUCT is compatible with your equipment and other software installed on your equipment. You are also solely responsible for the protection of your equipment and backup of your data, and THE PROVIDER will not be liable for any damages you may suffer in connection with using, modifying, or distributing this SOFTWARE PRODUCT.

(taken from: www.deq.louisiana.gov)

just have to replace “THE PROVIDER”  with your name or company name, and hopefully it should be ok.

if anyone knows about a better generic disclaimer for software, i would really appreciate a tip.

Monday, November 16, 2009

Crafting blog

I've revived my old “practical art” blog with some things I've done lately.

Lately I have been spending my time either on the White Rabbit project or in my studio, making pots and bowls.

This is the link to my crafty blog: http://practicalart.blogspot.com/

And here is a screen shot from the new White Rabbit version:

wr

In this version I thought it nice to use the spring-graph by Mark Shepherd. I’m not sure its the best concept, but its lots of fun.

I’ve also been playing around with the skinning, but i haven't mastered it yet ( as one can see )

Saturday, September 12, 2009

Scala, the java of the future

The tug-o-war of software development languages gets really confusing; it’s hard to decide what language would prevail and become the new standard in application development. Why is it important? Because we are application developers, and our relevance in the business landscape is linked to the tools we are skilled in operating.

I have heard Professor Martin Odersky say that Scala is the java of the future. Although I cannot vogue for such a promise, I can certainly say that Scala is the most compelling language I have encountered.

Scala has incorporated many ideas, new and old from the other existing languages. Scala functional and object oriented. It has cool stuff like traits (aka mixins) and Actors (erlang style threads) and advanced pattern matching. Most of all, Scala is expressive and esthetic. Clean and comprehensible code means easy and efficient maintenance, and that is important.

Scala runs over the JVM. Hence Scala can call upon java functionality and vice versa. Scala also has a .net support but that is not my scope at this time. According to professor Odersky, the overall performance is not hampered and the results are usually as fast as Java. Michael Galpin posted some figures about Scala performance on his blog here and Nick Wiedenbrueck compared Scala, Java and Groovy here.

Following are some interesting links I found for getting into Scala top down:

Presentations:

Articles:

Downloads: