Avg. Rating 4.0

Problem

You want to protect files inside of an AIR application.

Solution

Using the Encrypted Local Store and the File APIs you can put any file you want in a safe, secure location.

Detailed explanation

Most people associate the Encrypted Local Store (ELS) with storing usernames and passwords or other bits of text. But thanks to the fact that the ELS uses ByteArrays, it's easy to throw all kinds of things in there. I've got two examples below for saving data into the ELS and loading it back out.

A couple of things to keep in mind (thanks to Flash Ripper) is that the Encrypted Local Store is supported only up to 10 megs of space. It can go higher but you may see performance problems. Also, the ELS isn't cleared out when the application is uninstalled. As a developer you have to manually clear it out using the reset method.

We'll start with saving data. I've got a basic function that takes a File object (which could be from a drag-and-drop operation or a file open dialogue) and I start by creating a ByteArray that will store my file data. Just like any other File API, I open up a stream and put the bytes into that ByteArray. When I'm finished I grab the name of the file so I can reference it later and then use EncryptedLocalStore.setItem to add the file data to the ELS:

 

public function saveFile(file:File) : void
{

var stream : FileStream = new FileStream();

// Create the ByteArray that we're going to use to store the file information
var filedataArray : ByteArray = new ByteArray();

stream.open(file, FileMode.READ);
stream.readBytes(filedataArray, 0, file.size);
stream.close();

// Set the file name so we can pull it out later.
var fileName : String = file.name;

// Set the item in the Encrypted Local Store
EncryptedLocalStore.setItem(fileName,filedataArray);

}

 

The next function is the load function for taking the data out of the Encrypted Local Store and saving it to the hard drive somewhere. It also takes a File object so I know where to store the file when I'm finished reading it. Just like with any other File API I create a ByteArray which will contain the file bits and then I call EncryptedLocalStore.getItem to grab my data out of the ELS. When I've done that, I just create a new FileStream and write that file data to my file:

 

public function loadFile(file:File) : void
{

// Create the ByteArray and pull it out of the local store
var byteArray : ByteArray = new ByteArray();
byteArray = EncryptedLocalStore.getItem(fileName);

// Open and write the file using the regular File APIs
var stream : FileStream = new FileStream();
stream.open(file, FileMode.WRITE)
stream.writeBytes(byteArray);
stream.close();

}

 

If we ever want to clear the Encrypted Local Store we just call EncryptedLocalStore.reset() and that will wipe away the data. The Encrypted Local Storage APIs are some of the easiest in the AIR Runtime but they allow for the ability to save and protect all kinds of data thanks to the ByteArray

Below I've attached a .zip file which contains a very simple application with buttons for saving and loading to and from the Encrypted Local Store.

Encrypted Local Store.zip
[Sample application which uses file dialogue's to save and load data from the Encrypted Local Store.]

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes