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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Working with local SQL databases in AIR<br />

var newKey:ByteArray = new ByteArray();<br />

// ... g<strong>en</strong>erate the new key and store it in newKey<br />

conn.addEv<strong>en</strong>tList<strong>en</strong>er(SQLEv<strong>en</strong>t.REENCRYPT, re<strong>en</strong>cryptHandler);<br />

conn.re<strong>en</strong>crypt(newKey);<br />

function re<strong>en</strong>cryptHandler(ev<strong>en</strong>t:SQLEv<strong>en</strong>t):void<br />

{<br />

// save the fact that the key changed<br />

}<br />

The re<strong>en</strong>crypt() operation runs in its own transaction. If the operation is interrupted or fails (for example, if the<br />

application is closed before the operation finishes) the transaction is rolled back. In that case, the original <strong>en</strong>cryption<br />

key is still the <strong>en</strong>cryption key for the database.<br />

The re<strong>en</strong>crypt() method can’t be used to remove <strong>en</strong>cryption from a database. Passing a null value or <strong>en</strong>cryption<br />

key that’s not a 16-byte ByteArray to the re<strong>en</strong>crypt() method results in an error.<br />

Considerations for using <strong>en</strong>cryption with a database<br />

Adobe AIR 1.5 and later<br />

The section “Uses for an <strong>en</strong>crypted database” on page 757 pres<strong>en</strong>ts several cases in which you would want to use an<br />

<strong>en</strong>crypted database. It’s obvious that the usage sc<strong>en</strong>arios of differ<strong>en</strong>t applications (including these and other sc<strong>en</strong>arios)<br />

have differ<strong>en</strong>t privacy requirem<strong>en</strong>ts. The way you architect the use of <strong>en</strong>cryption in your application plays an<br />

important part in controlling how private a database’s data is. For example, if you are using an <strong>en</strong>crypted database to<br />

keep personal data private, ev<strong>en</strong> from other users of the same machine, th<strong>en</strong> each user’s database needs its own<br />

<strong>en</strong>cryption key. For the greatest security, your application can g<strong>en</strong>erate the key from a user-<strong>en</strong>tered password. Basing<br />

the <strong>en</strong>cryption key on a password <strong>en</strong>sures that ev<strong>en</strong> if another person is able to impersonate the user’s account on the<br />

machine, the data still can’t be accessed. On the other <strong>en</strong>d of the privacy spectrum, suppose you want a database file to<br />

be readable by any user of your application but not to other applications. In that case every installed copy of the<br />

application needs access to a shared <strong>en</strong>cryption key.<br />

You can design your application, and in particular the technique used to g<strong>en</strong>erate the <strong>en</strong>cryption key, according to the<br />

level of privacy that you want for your application data. The following list provides design suggestions for various levels<br />

of data privacy:<br />

To make a database accessible to any user who has access to the application on any machine, use a single key that’s<br />

available to all instances of the application. For example, the first time an application runs it can download the<br />

shared <strong>en</strong>cryption key from a server using a secure protocol such as SSL. It can th<strong>en</strong> save the key in the <strong>en</strong>crypted<br />

local store for future use. As an alternative, <strong>en</strong>crypt the data per-user on the machine, and synchronize the data with<br />

a remote data store such as a server to make the data portable.<br />

To make a database accessible to a single user on any machine, g<strong>en</strong>erate the <strong>en</strong>cryption key from a user secret (such<br />

as a password). In particular, do not use any value that’s tied to a particular computer (such as a value stored in the<br />

<strong>en</strong>crypted local store) to g<strong>en</strong>erate the key. As an alternative, <strong>en</strong>crypt the data per-user on the machine, and<br />

synchronize the data with a remote data store such as a server to make the data portable.<br />

To make a database accessible only to a single individual on a single machine, g<strong>en</strong>erate the key from a password<br />

and a g<strong>en</strong>erated salt. For an example of this technique, see “Example: G<strong>en</strong>erating and using an <strong>en</strong>cryption key” on<br />

page 762.<br />

Last updated 6/6/2012<br />

761

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

Saved successfully!

Ooh no, something went wrong!