13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with local SQL databases in AIR<br />

Creating an <strong>en</strong>crypted database<br />

Adobe AIR 1.5 and later<br />

To use an <strong>en</strong>crypted database, the database file must be <strong>en</strong>crypted wh<strong>en</strong> it is created. Once a database is created as<br />

un<strong>en</strong>crypted, it can’t be <strong>en</strong>crypted later. Likewise, an <strong>en</strong>crypted database can’t be un<strong>en</strong>crypted later. If needed you can<br />

change the <strong>en</strong>cryption key of an <strong>en</strong>crypted database. For details, see “Changing the <strong>en</strong>cryption key of a database” on<br />

page 760. If you have an existing database that’s not <strong>en</strong>crypted and you want to use database <strong>en</strong>cryption, you can create<br />

a new <strong>en</strong>crypted database and copy the existing table structure and data to the new database.<br />

Creating an <strong>en</strong>crypted database is nearly id<strong>en</strong>tical to creating an un<strong>en</strong>crypted database, as described in “Creating a<br />

database” on page 718. You first create a SQLConnection instance that repres<strong>en</strong>ts the connection to the database. You<br />

create the database by calling the SQLConnection object’s op<strong>en</strong>() method or op<strong>en</strong>Async() method, specifying for the<br />

database location a file that doesn’t exist yet. The only differ<strong>en</strong>ce wh<strong>en</strong> creating an <strong>en</strong>crypted database is that you<br />

provide a value for the <strong>en</strong>cryptionKey parameter (the op<strong>en</strong>() method’s fifth parameter and the op<strong>en</strong>Async()<br />

method’s sixth parameter).<br />

A valid <strong>en</strong>cryptionKey parameter value is a ByteArray object containing exactly 16 bytes.<br />

The following examples demonstrate creating an <strong>en</strong>crypted database. For simplicity, in these examples the <strong>en</strong>cryption<br />

key is hard-coded in the application code. However, this technique is strongly discouraged because it is not secure.<br />

var conn:SQLConnection = new SQLConnection();<br />

var <strong>en</strong>cryptionKey:ByteArray = new ByteArray();<br />

<strong>en</strong>cryptionKey.writeUTFBytes("Some16ByteString"); // This technique is not secure!<br />

// Create an <strong>en</strong>crypted database in asynchronous mode<br />

conn.op<strong>en</strong>Async(dbFile, SQLMode.CREATE, null, false, 1024, <strong>en</strong>cryptionKey);<br />

// Create an <strong>en</strong>crypted database in synchronous mode<br />

conn.op<strong>en</strong>(dbFile, SQLMode.CREATE, false, 1024, <strong>en</strong>cryptionKey);<br />

For an example demonstrating a recomm<strong>en</strong>ded way to g<strong>en</strong>erate an <strong>en</strong>cryption key, see “Example: G<strong>en</strong>erating and<br />

using an <strong>en</strong>cryption key” on page 762.<br />

Connecting to an <strong>en</strong>crypted database<br />

Adobe AIR 1.5 and later<br />

Like creating an <strong>en</strong>crypted database, the procedure for op<strong>en</strong>ing a connection to an <strong>en</strong>crypted database is like<br />

connecting to an un<strong>en</strong>crypted database. That procedure is described in greater detail in “Connecting to a database” on<br />

page 725. You use the op<strong>en</strong>() method to op<strong>en</strong> a connection in synchronous execution mode, or the op<strong>en</strong>Async()<br />

method to op<strong>en</strong> a connection in asynchronous execution mode. The only differ<strong>en</strong>ce is that to op<strong>en</strong> an <strong>en</strong>crypted<br />

database, you specify the correct value for the <strong>en</strong>cryptionKey parameter (the op<strong>en</strong>() method’s fifth parameter and<br />

the op<strong>en</strong>Async() method’s sixth parameter).<br />

If the <strong>en</strong>cryption key that’s provided is not correct, an error occurs. For the op<strong>en</strong>() method, a SQLError exception is<br />

thrown. For the op<strong>en</strong>Async() method, the SQLConnection object dispatches a SQLErrorEv<strong>en</strong>t, whose error<br />

property contains a SQLError object. In either case, the SQLError object g<strong>en</strong>erated by the exception has the errorID<br />

property value 3138. That error ID corresponds to the error message “File op<strong>en</strong>ed is not a database file.”<br />

The following example demonstrates op<strong>en</strong>ing an <strong>en</strong>crypted database in asynchronous execution mode. For simplicity,<br />

in this example the <strong>en</strong>cryption key is hard-coded in the application code. However, this technique is strongly<br />

discouraged because it is not secure.<br />

Last updated 6/6/2012<br />

758

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!