<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23248-</id>
  <updated>2009-11-05T20:16:16Z</updated>
  <title>Comments for Encryption in Flex Applications 1 - Simulate EncryptedLocalStore (http://www.insideria.com/2008/03/encryption-in-flex-application.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23248</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23248" title="Encryption in Flex Applications 1 - Simulate EncryptedLocalStore" />
    <published>2008-03-31T23:00:00Z</published>
    <updated>2008-03-31T23:15:29Z</updated>
    <title>Encryption in Flex Applications 1 - Simulate EncryptedLocalStore</title>
    <summary>In the RIA world, Flex and AIR applications have really taken off.  With that has come increased adoption by Fortune 500 companies and new enterprise-level apps taking advantage of the Adobe Flash platform.  Application and data security should always be a concern of the Flex/AIR developer.  The level of paranoia the developer should implement must be weighed against the goals of the project.</summary>
    <author>
      <name>Andrew Westberg</name>
      <uri>http://www.flexjunk.com</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<p>In the RIA world, Flex and AIR applications have really taken off.  With that has come increased adoption by Fortune 500 companies and new enterprise-level apps taking advantage of the Adobe Flash platform.  Application and data security should always be a concern of the Flex/AIR developer.  The level of paranoia the developer should implement must be weighed against the goals of the project.  For example, if you&#8217;re developing an open source or advertising-supported application intended for a wide public audience, you probably want to implement fairly minimal security measures in order to reach the widest audience and limit the amount of time you spend managing users in the system.  On the other hand, if you&#8217;re being contracted to write a dashboard application for a large company or government for internal use, you&#8217;d probably want to implement security measures at the high-end of the spectrum.
</p>
<p>
In this series of three articles on the topic of Encryption in Flex Applications, we&#8217;ll first cover a basic data encryption and storage example in a Flex application.  In article two, we&#8217;ll look at using an interface and doing some minimal encryption on a SWC file to protect an example commercial library we want to sell.  In the final installment, we&#8217;ll take a look at using some of the features of NitroLM.com which is a commercial API for user registration, management, and entire application encryption.
</p>
<hr/>
<p>
In Adobe&#8217;s newly-released version of AIR 1.0, they provide an API for storing encrypted data to the hard drive.  The flash.data.EncryptedLocalStore class uses the Windows DPAPI or KeyChain on MacOS to store and retrieve encrypted data as a ByteArray.  Unfortunately, this capability isn&#8217;t available to us in a Flex application.  In this example, I&#8217;ll demonstrate creating similar functionality by encrypting data stored in a local SharedObject.
</p>
<p>
The first thing we need is to download an encryption library to use in our Flex application.  I&#8217;m using AS3Crypto (<a href="http://crypto.hurlant.com/">http://crypto.hurlant.com</a>) created by Henri Torgemane.  I recommend downloading the source code so you can debug easier and see how the encryption is working.
</p>
<p>
In this example (view-source enabled), the user can save a username and password between runs of the application to be used by a web service.  It&#8217;s not totally secure since the randomly generated key is stored along with the encrypted data.  I&#8217;ll leave it as an exercise for the reader to come up with clever ways to obfuscate the key or use alternative server-side repositories that are more secure.
</p>
<p>
<a href="http://flexjunk.com/examples/FlexEncryptionExample1/FlexEncryptionExample1.html">FlexEncryptionExample1 example</a>
</p>

<p>
Let&#8217;s walk through the code.  We have two main methods, encryptedLoad() and encryptedSave().  encryptedSave() generates a random 16 byte key, and runs the AES-128 encryption algorithm on our username and password that we&#8217;ve packaged into a ByteArray.
</p>

 <div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre> 

<span class="category1">private</span> <span class="category1">function</span> encryptedSave():<span class="category1">void</span>
{
 	<span class="linecomment">//create or retrieve the current shared object</span>
 	<span class="category1">var</span> so:<span class="category2">SharedObject</span> = <span class="category2">SharedObject</span>.<span class="category2">getLocal</span>("<span class="quote">encryptedStore</span>");
 				
 	<span class="linecomment">//generate a random key</span>
 	<span class="category1">var</span> key:ByteArray = <span class="category1">new</span> ByteArray();
 	<span class="category1">var</span> <span class="category2">random</span>:Random = <span class="category1">new</span> Random();
 	<span class="category2">random</span>.nextBytes(key, 16);
 				
 	<span class="linecomment">//store our data to encrypt into a ByteArray</span>
 	<span class="category1">var</span> cleartextBytes:ByteArray = <span class="category1">new</span> ByteArray();
 	cleartextBytes.writeUTF(username.<span class="category2">text</span>);
 	cleartextBytes.writeUTF(<span class="category2">password</span>.<span class="category2">text</span>);
 				
 	<span class="linecomment">//encrypt using 128b AES encryption using a random key</span>
 	<span class="category1">var</span> aes:ICipher = Crypto.getCipher("<span class="quote">aes-ecb</span>", key, Crypto.getPad("<span class="quote">pkcs5</span>"));
 	aes.encrypt(cleartextBytes);
 				
 	<span class="linecomment">//store key along with the data to decrypt</span>
 	<span class="linecomment">//Note: normally you'd never do this for security reasons,</span>
 	<span class="linecomment">//      but I'll leave it to the reader to handle additional</span>
 	<span class="linecomment">//      security and/or obvuscation.</span>
 	<span class="category1">var</span> dataToStore:ByteArray = <span class="category1">new</span> ByteArray();
 	dataToStore.writeBytes(key);
 	dataToStore.writeBytes(cleartextBytes);
 
 	<span class="linecomment">//save the blob of encrypted stuff in the SharedObject</span>
 	so.<span class="category2">data</span>.ws_creds = dataToStore;
 	so.<span class="category2">flush</span>();
 				
 	<span class="linecomment">//clear out the fields</span>
 	username.<span class="category2">text</span>="<span class="quote"></span>";
 	<span class="category2">password</span>.<span class="category2">text</span>="<span class="quote"></span>";
}</pre>
</code>
 
</div></div> 

<p>
encryptedLoad() reads in our key and uses it to decrypt the rest of the ByteArray.  The values are then loaded into their respective form fields from the decrypted ByteArray.
</p>

 <div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre> 

<span class="category1">private</span> <span class="category1">function</span> encryptedLoad():<span class="category1">void</span>
{
 	<span class="linecomment">//create or retrieve the current shared object</span>
 	<span class="category1">var</span> so:<span class="category2">SharedObject</span> = <span class="category2">SharedObject</span>.<span class="category2">getLocal</span>("<span class="quote">encryptedStore</span>");
 				
 	<span class="category1">var</span> dataToLoad:ByteArray = so.<span class="category2">data</span>.ws_creds;
 				
 	<span class="linecomment">//read in our key</span>
 	<span class="category1">var</span> key:ByteArray = <span class="category1">new</span> ByteArray();
 	dataToLoad.readBytes(key, 0, 16);
 				
 	<span class="linecomment">//read in our encryptedText</span>
 	<span class="category1">var</span> encryptedBytes:ByteArray = <span class="category1">new</span> ByteArray();
 	dataToLoad.readBytes(encryptedBytes);
 				
 	<span class="linecomment">//decrypt using 128b AES encryption</span>
 	<span class="category1">var</span> aes:ICipher = Crypto.getCipher("<span class="quote">aes-ecb</span>", key, Crypto.getPad("<span class="quote">pkcs5</span>"));
 	aes.decrypt(encryptedBytes);
 				
 	encryptedBytes.<span class="category2">position</span> = 0;
 				
 	username.<span class="category2">text</span> = encryptedBytes.readUTF();
 	<span class="category2">password</span>.<span class="category2">text</span> = encryptedBytes.readUTF();
}</pre>
</code>
 
</div></div> 

<p>
Hopefully with this example, you can start to see some of the possibilities for encrypting your data using Adobe Flex/AIR.
</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2016292</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2016292" />
    <title>Comment from Raul Riera on 2008-04-02</title>
    <author>
        <name>Raul Riera</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>With all the decompilers out there, I guess this would be usefull only to encrypted SWF as well right? (Since AIR apps can be decompiled as well)</p>

<p>(Wow the captcha was hard)</p>]]>
    </content>
    <published>2008-04-02T07:21:06Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2016296</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2016296" />
    <title>Comment from Andrew Westberg on 2008-04-02</title>
    <author>
        <name>Andrew Westberg</name>
        <uri>http://www.flexjunk.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.flexjunk.com">
        <![CDATA[<p>That's correct.  Since it can be decompiled to figure out where the key lives you'd either have to encrypt the swf, or store the key in a location secured by another mechanism.  I'll get into SWC and SWF encryption in the 2 follow-on articles.</p>]]>
    </content>
    <published>2008-04-02T12:15:05Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2017132</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2017132" />
    <title>Comment from mostefa on 2008-05-14</title>
    <author>
        <name>mostefa</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>tanks</p>]]>
    </content>
    <published>2008-05-14T22:54:15Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2052386</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2052386" />
    <title>Comment from demm on 2009-02-06</title>
    <author>
        <name>demm</name>
        <uri>http://www.insideria.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com">
        <![CDATA[<p>encryptedLoad() reads in our key and uses it to decrypt the rest of the ByteArray. The values are then loaded into their respective form fields from the decrypted ByteArray. </p>

<p>why?</p>]]>
    </content>
    <published>2009-02-06T11:18:27Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2052390</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2052390" />
    <title>Comment from Andrew Westberg on 2009-02-06</title>
    <author>
        <name>Andrew Westberg</name>
        <uri>http://www.flexjunk.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.flexjunk.com">
        <![CDATA[<p>@demm The purpose of this article was to simulate (not duplicate) the functionality of encryptedLocalStore.  For simplicity, the decryption key was stored alongside the encrypted payload.  The values are loaded into the form fields just so that you can verify that the decryption worked.</p>]]>
    </content>
    <published>2009-02-06T13:19:45Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2066231</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2066231" />
    <title>Comment from Clive on 2009-06-15</title>
    <author>
        <name>Clive</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Thanks for the example, but do you perhaps have an example that shows how to encrypt/decrypt a serialized AMF object?</p>]]>
    </content>
    <published>2009-06-15T16:52:37Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2075964</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2075964" />
    <title>Comment from Essay on 2009-08-31</title>
    <author>
        <name>Essay</name>
        <uri>http://gpalabs.com/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://gpalabs.com/">
        <![CDATA[<p>I want too example how to encrypt AMF objects</p>]]>
    </content>
    <published>2009-08-31T10:28:43Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23248-comment:2076047</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23248" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/encryption-in-flex-application.html#comment-2076047" />
    <title>Comment from Andrew Westberg on 2009-08-31</title>
    <author>
        <name>Andrew Westberg</name>
        <uri>http://www.nitrolm.com/blog</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.nitrolm.com/blog">
        <![CDATA[<p>@Essay,  Normally, you don't need to worry about encrypting AMF since it's used to send over a network connection.  Just make sure the receiving server is using https and you should be fine security-wise.</p>]]>
    </content>
    <published>2009-08-31T11:30:39Z</published>
  </entry>

</feed
