Adobe AIR has a quick and safe way to store encrypted user data when building applications. Data such as login and password can be persisted in the application using the EncryptedLocalStore available for AIR applications.
To demonstrate this, I wrote a quick demo application that will store username and password after users login. When the application is launched again, the same username and password will be retrieved from the EncryptedLocalStore and propagate the login and password text boxes. User’s also have the option to reset the data and store a new username and password, removing the stored data from the EcryptedLocalStore.
This can be handy when you want your application to do auto-login, using the existing stored information from the user’s previous session, and of course, the stored data is encrypted.
Example:
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
3 width="410" height="260" creationComplete="initComponent()">
4 <mx:Script>
5 <![CDATA[
6 import mx.controls.Alert;
7
8 private function initComponent():void
9 {
10 var password:ByteArray = EncryptedLocalStore.getItem("password");
11 var username:ByteArray = EncryptedLocalStore.getItem("username");
12
13 try{
14 if(password.length && username.length){
15 passwordInput.text = password.readUTFBytes(password.length);
16 nameInput.text = username.readUTFBytes(username.length);
17 }
18 } catch (e:Error){Alert.show(e.message);}
19 }
20
21 private function onLogin(event:Event):void
22 {
23 if(nameInput.text == "" || passwordInput.text == ""){
24 Alert.show("Please enter username and password");
25 return;
26 }
27
28 try {
29 EncryptedLocalStore.reset();
30
31 var bytes:ByteArray = new ByteArray();
32 bytes.writeUTFBytes(nameInput.text);
33
34 EncryptedLocalStore.setItem("username", bytes);
35
36 bytes = new ByteArray();
37 bytes.writeUTFBytes(passwordInput.text);
38
39 EncryptedLocalStore.setItem("password", bytes);
40
41 Alert.show("Writing username and password to local store.");
42
43 } catch(e:Error){Alert.show("Error writing store: " + e.message)}
44 }
45
46 private function onReset(event:Event):void
47 {
48 passwordInput.text = "";
49 nameInput.text = "";
50 nameInput.setFocus();
51 EncryptedLocalStore.reset();
52 }
53
54 private function onKeyDown(event:KeyboardEvent):void
55 {
56 if ( event.charCode == Keyboard.ENTER) {
57 onLogin(null);
58 }
59 }
60 ]]>
61 </mx:Script>
62 <mx:Canvas width="380" height="240" horizontalCenter="0" verticalCenter="0">
63 <mx:VBox verticalAlign="middle" horizontalAlign="left" verticalGap="0" horizontalCenter="0" bottom="142">
64 <mx:Label id="userText" text="username" creationComplete="nameInput.setFocus()"/>
65 <mx:TextInput id="nameInput" creationComplete="{nameInput.setFocus()}" keyDown="onKeyDown(event)"/>
66 </mx:VBox>
67
68 <mx:VBox verticalAlign="middle" horizontalAlign="left" verticalGap="0" horizontalCenter="0" bottom="95">
69 <mx:Label text="password" />
70 <mx:TextInput id="passwordInput" displayAsPassword="true" keyDown="onKeyDown(event)"/>
71 </mx:VBox>
72
73 <mx:ControlBar bottom="16" width="161" height="35" horizontalCenter="0">
74 <mx:Button id="submitButton" label="submit" click="onLogin(event)" textAlign="center" />
75 <mx:Button id="resetButton" label="reset" click="onReset(event)" textAlign="center"/>
76 </mx:ControlBar>
77 </mx:Canvas>
78 </mx:WindowedApplication>
To use the example code, just create a new AIR application in Flex Builder and paste the code into your main MXML file.