Calendar

November 2008
S M T W T F S
« Oct   Dec »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Tag Cloud

Categories

Archives

Recent Posts

Recent Comments


« 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, 2008Print This Post Print This Post
3,164 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:

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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:

  • On creationComplete of the application, the openDatabase() function is called.
  • The storedKey (dbKey) is retrieved from the encrypted local store.
  • If the storedKey does not exist, one is assigned by calling the crateRandomKey() function.
  • A new SQLConnection (conn) is created.
  • The database file (dbFile) is resolved.
  • The connection is opened and the storedKey is passed in which creates the database on first run or opens the database on future runs.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    <?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

    Share this Post


    Topics: Adobe AIR |

    3 Responses to “Using Encrypted SQLite database in AIR 1.5 (from the cookbook)”

    1. Steve Alter Says:
      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

    2. motorlu panjur Says:
      December 2nd, 2008 at 1:53 pm

      thanks..

    3. Rich Apps Consulting Says:
      December 26th, 2008 at 11:51 am

      Thanks for the valuable insight.

    Comments