« Announcing FotoBooth 2 Pixel Bender Edition | Main | AIR Bootcamp »
Using Encrypted SQLite database in AIR 1.5 (from the cookbook)
| By Rich Tretola | November 18, 2008 | Print This Post
|
| 1,349 views |
The following example is covered within the new Adobe AIR 1.5 Cookbook Adobe AIR 1.5 now includes the ability to encrypt your SQLite database. The example below is a complete application. Be sure to use a compile with a minimum of the Flex 3.2.0.3266 SDK (or the latest stable build), set the application namespace to http://ns.adobe.com/air/application/1.5.
AIR 1.0 included SQLite as an embedded database within the runtime. To open a database when working with AIR 1.0 you would call the open() or openAsync() methods on the flash.data.SQLConnection whose signatures are as follows:
- open(reference:Object = null,
- openMode:String = "create",
- responder:Responder = null,
- autoCompact:Boolean = false,
- pageSize:int = 1024):void
- openAsync(reference:Object = null,
- openMode:String = "create",
- responder:Responder = null,
- autoCompact:Boolean = false,
- pageSize:int = 1024):void
AIR 1.5 adds an optional additional argument of type ByteArray. Here are the new signatures:
- open(reference:Object = null,
- openMode:String = "create",
- responder:Responder = null,
- autoCompact:Boolean = false,
- pageSize:int = 1024,
- key:ByteArray = null):void
- openAsync(reference:Object = null,
- openMode:String = "create",
- responder:Responder = null,
- autoCompact:Boolean = false,
- pageSize:int = 1024,
- key:ByteArray = null):void
OK, now lets talk about the example below. The only real difference is that we are going to supply a key when we open/create the database. The way the key is being created is through the use of a third party library called as3crypto. The key is then stored within the encrypted local store (ex: the keychain on Mac OS X).
Here is the chain of events:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:WindowedApplication
- xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="absolute"
- creationComplete="openDatabase()"
- height="150">
- <mx:Script>
- <![CDATA[
- import com.hurlant.crypto.prng.Random;
- private var storedKey:ByteArray;
- private var conn:SQLConnection;
- [Bindable]
- private var dbFile:File
- private function createRandomKey():ByteArray {
- var encryptionKey:ByteArray = new ByteArray();
- var random:Random = new Random();
- random.nextBytes(encryptionKey, 16);
- return encryptionKey;
- }
- private function openDatabase():void{
- storedKey = EncryptedLocalStore.getItem("dbKey");
- if(!storedKey) {
- storedKey = createRandomKey();
- EncryptedLocalStore.setItem("dbKey", storedKey, false);
- }
- conn = new SQLConnection();
- dbFile = File.applicationStorageDirectory.resolvePath("EncryptedDB.db");
- conn.addEventListener(SQLEvent.OPEN, openHandler);
- conn.openAsync(dbFile,SQLMode.CREATE,null,false,1024,storedKey);
- }
- private function openHandler(event:SQLEvent):void {
- traceTxt.text += "The database was opened successfully \n\n";
- traceTxt.text += dbFile.nativePath + "\n\n";
- }
- ]]>
- </mx:Script>
- <mx:TextArea width="100%" height="100%" id="traceTxt"/>
- </mx:WindowedApplication>
Here is the result:

Hope this helps. Be sure to check out the rest of the recipes in the new Adobe AIR 1.5 Cookbook
Topics: Adobe AIR |








November 20th, 2008 at 2:36 am
For about eight years we have been helping clients to market their properties via the internet. About seven years we’ve been building our clients from scrath a custom flash presentation. Now more and more clients are more web savvy so that they’re wanting a more do it yourself type solution that would let them just log onto our site and then maybe pick from a number of templates that we could pre-define for them but then they could use those templates to build their own flash presentation. Do you have a suitable application to make this happen or any recommendation maybe?
Thanks,
Steve Alter
December 2nd, 2008 at 1:53 pm
thanks..
December 26th, 2008 at 11:51 am
Thanks for the valuable insight.