<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GhostWire Studios &#187; Encryption</title>
	<atom:link href="http://www.ghostwire.com/blog/archives/tag/encryption/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghostwire.com/blog</link>
	<description>Flash UI Components</description>
	<lastBuildDate>Tue, 23 Aug 2011 03:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 11:05:12 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1381</guid>
		<description><![CDATA[This is Part III of our discussion on ROT128 Encryption. Part I: &#8220;Applying ROT128 Encryption On ByteArray&#8221; Part II: &#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221; In &#8220;Saving XML As Binary&#8221;, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable. In this post, [...]]]></description>
			<content:encoded><![CDATA[<p>This is Part III of our discussion on ROT128 Encryption.</p>
<p>Part I: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a><br />
Part II: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/">&#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221;</a></p>
<p>In <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable.  In this post, we look at how you can integrate ROT128 into the XML-to-ByteArray-to-XML routines.  Binary XML is used only as an example; you can definitely apply the same concept to other binary data.</p>
<p><span id="more-1381"></span><br />
<strong>XML-to-ByteArray:</strong><br />
ROT128 is applied after the XML is stored and compressed as ByteArray.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span>
<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bindata.xml&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;bindata.xml&quot;</span></pre></div></div>

<p><!-- --><br />
<strong>ByteArray-to-XML:</strong><br />
With ROT128 applied, the saved binary file is no longer a valid compressed ByteArray &#8211; it cannot be uncompressed and converted back to XML without first reversing the encryption on the raw data.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// event handler for Event.COMPLETE of URLLoader loading external XML</span>
<span style="color: #000000; font-weight: bold;">function</span> on_XML<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
			<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// END ROT128</span>
			<span style="color: #0066CC;">data</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// handle data as XML</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Partial Encryption</strong><br />
In order to make the process of reversing ROT128 encryption less predictable, you could vary the conditions under which the encryption is to be applied.</p>
<p>Here is an example (alternate bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	j--; <span style="color: #808080; font-style: italic;">// skip next byte</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Another example (first 1024 bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> : <span style="color: #cc66cc;">1024</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Correspondingly, your applications would reverse the encryption under the same conditions.  Effectively, these conditions determine the complexity of your cipher and become the &#8220;secret key&#8221; necessary to decrypt the data.</p>
<p><!-- --><br />
<strong>Conclusion</strong><br />
ROT128 is a weak encryption technique that you should not use to protect sensitive data.  However, the light-weight algorithm can be used to make binary blobs inaccessible to unauthorized parties while allowing your applications to easily restore the data when needed.</p>
<p>This method of encryption may seem amateurish, but it does what it is supposed to do well enough while remaining light-weight and flexible.</p>
<p>It worked well enough for Julius Caesar, after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Embedded/Module SWFs</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 04:02:43 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Copy Protection]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1382</guid>
		<description><![CDATA[This post is a supplement to &#8220;Applying ROT128 Encryption On ByteArray&#8221;. Some time back, we posted a simple technique for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a>.</p>
<p>Some time back, we posted <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">a simple technique</a> for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer of protection.</p>
<p><span id="more-1382"></span><br />
<strong>Step I: Apply ROT128 To Actual SWF</strong><br />
Using the following code, we will apply ROT128 to the SWF that is going to be embedded:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// assuming the raw data of the SWF has been stored in a variable swfBytes</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfBytes.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	swfBytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The following shows a simple tool you can create to load a SWF file, apply ROT128 to it, and save the encrypted file.</p>
<ul>
<li>Flash Player 10 is required.</li>
<li>Click the Browse button to bring up a native file dialog.</li>
<li>Select a Flash Movie (.swf) file.</li>
<li>Click the Save button that will be shown after loading the SWF file.</li>
</ul>
<p>(80&#215;80 SWF, 2KB)<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ROT128SWF_1824552056"
			class="flashmovie"
			width="80"
			height="80">
	<param name="movie" value="/swf/ROT128SWF.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/swf/ROT128SWF.swf"
			name="fm_ROT128SWF_1824552056"
			width="80"
			height="80">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>You can verify that encryption has been done by running the saved SWF file &#8220;ActualSWF.swf&#8221; &#8211; it should show a blank screen.</p>
<p>Remember, applying ROT128 twice restores the file, so the tool can also be used to restore a previously ROT128-encrypted file if the algorithm used was the same (same n increment, same conditional loop).</p>
<p>The ROT128SWF class:<br />
(please feel free to customize to fit your own requirements)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileFilter</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ROT128SWF <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist text button **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> opButton:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** browse/load/save **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swfFile:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ROT128SWF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// entry point</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span>			= StageAlign.<span style="color: #0066CC;">LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span>			= StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">showDefaultContextMenu</span>	= <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist text button **</span>
			opButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			opButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			opButton.<span style="color: #0066CC;">backgroundColor</span>	= 0x000000;
			opButton.<span style="color: #006600;">defaultTextFormat</span>	= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tahoma&quot;</span>, <span style="color: #cc66cc;">14</span>, 0xFFFFFF, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;BROWSE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			opButton.<span style="color: #006600;">y</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span> - opButton.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>opButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** button click listener **</span>
			opButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse or save
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">TextField</span> = evt.<span style="color: #0066CC;">target</span> as <span style="color: #0066CC;">TextField</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;BROWSE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swfFile = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Flash Movie&quot;</span>,<span style="color: #ff0000;">&quot;*.swf&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;SAVE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swfFile<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfFile.<span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
						<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#123;</span>
							swfFile.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
						<span style="color: #66cc66;">&#125;</span>
						<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>swfFile.<span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse, load swf file
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfSelect<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect<span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle load, change browse button to save button
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete<span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;SAVE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Step II: Embed Actual SWF In Shell SWF</strong><br />
If you have not done so already, please see <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">&#8220;Hiding Assets And Code By Embedding SWF Within Another SWF&#8221;</a> for the original discussion on how the technique works.</p>
<p>The following is a <code>MainShell</code> class you can use to embed the ROT128-encrypted SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainShell <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>		
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const bytes:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* REMINDER
		* 
		* MAKE SURE THAT THIS SHELL SWF IS PUBLISHED USING THE
		* ORIGINAL WIDTH/HEIGHT DIMENSIONS OF THE EMBEDDED SWF
		*/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainShell<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> swf:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> bytes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
				<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swf.<span style="color: #0066CC;">length</span>;
				<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swf<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
				Loader<span style="color: #66cc66;">&#40;</span>addChild<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">loadBytes</span><span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>With the above code, the &#8220;ActualSWF.swf&#8221; file is embedded into the shell SWF.  It is then instantiated as a ByteArray during run-time.  ROT128 is then applied to the ByteArray before it is loaded into a Loader object.</p>
<p><!-- --><br />
<strong>Why Do This At All?</strong><br />
Hiding assets and code by embedding one SWF within another SWF is simple to implement and so far, there has not been any report on any decompiler overcoming the protection.</p>
<p>In the original post on the technique, I suggested that you could, if you wish, add another layer of protection by encrypting the embedded SWF just in case a decompiler may in future automatically identify a binary blob and &#8220;guess&#8221; that it is an embedded SWF (and decompile it separately).</p>
<p>I feel that ROT128 is light-weight and simple enough that can achieve that objective.  Remember that the objective here is not to overcome meticulous hacking, but merely to thwart possible attempts to implement <em>automatic</em> extraction and identification of embedded binary blobs as separate SWFs.</p>
<p>The above is only an example of how the concept can be applied.  As mentioned in <a href=""http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">the previous post on ROT128</a>, you can also consider applying ROT128 only to part of the file so that the logic for reversing the encryption could be a little less predictable.</p>
<p><!-- --><br />
<strong>Module SWFs</strong><br />
Although the above mentioned only embedded SWFs, you can definitely apply the same concept to module SWFs that will be loaded into your application during run-time.  By applying ROT128 to module SWFs, even if they are obtained via unauthorized means such as via the web browser&#8217;s cache, they are non-executable (or will just show a blank screen when run) if the ROT128 encryption is not reversed first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On ByteArray</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 07:49:27 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1023</guid>
		<description><![CDATA[In this post, we will look at a very simple algorithm for weak encryption. You should not use this method for real cryptographic security. However, because it is so simple to implement, the light-weight algorithm could easily escape prying eyes and avoid being the target for decryption in the first place. I should also clarify [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we will look at a very simple algorithm for weak encryption.  You should not use this method for real cryptographic security.  However, because it is so simple to implement, the light-weight algorithm could easily escape prying eyes and avoid being the target for decryption in the first place.</p>
<p>I should also clarify that the term &#8220;ROT128&#8243; does not actually exist.  The original idea comes from ROT13, a variant of the Caesar Cipher (named after Julius Caesar of ancient Rome who used it to encrypt messages, but it is not clear who first invented or started using the cipher).</p>
<p><span id="more-1023"></span><br />
<strong>ROT13</strong><br />
With ROT13, you obfuscate a piece of text by substituting each character with one that is 13 positions higher up in the English alphabet (A-Z) sequence, with positions wrapping back to the beginning after Z. Therefore, you are <em>rotating</em> the positions (thus the name of the cipher).  The choice of 13 positions is used because there are 26 alphabets (positions), which means applying ROT13 twice restores the original text.  In other words, the exact same algorithm is used for both encoding and decoding. Effectively, the alphabet A becomes N and vice versa.  Likewise, M becomes Z and vice versa, etc.</p>
<p><!-- --><br />
<strong>ROT128</strong><br />
With ROT128, instead of obfuscating text, we will corrupt a <code>ByteArray</code> object by rotating all its byte values by 128 positions each.  A byte value has 256 possible positions, and so in the spirit of using the same algorithm for encoding and decoding, we will rotate values by 128 positions.  Therefore, 0 becomes 128 and vice versa, 1 becomes 129 and vice versa, 127 becomes 255 and vice versa, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> ROT128<span style="color: #66cc66;">&#40;</span>bytes:ByteArray<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// bytes is ByteArray object to encrypt/decrypt</span>
	<span style="color: #808080; font-style: italic;">// you are modifying the object directly, not a clone</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = bytes.<span style="color: #0066CC;">length</span>;
		<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			bytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see, the algorithm is very simple.  The ROT128 function listed above is intended for reference only.  You don&#8217;t even really need to create such a function since you can easily sneak the code in-line into the part(s) of your application code where it will actually be used.  After all, if your SWF is decompiled, having a function named ROT128() is more likely to alert the hacker to the usage of the cipher.</p>
<p><!-- --><br />
<strong>Partial ROT128</strong><br />
Sometimes, less is more.  Instead of applying ROT128 to every byte in the ByteArray object, you may choose to do a variant of that &#8211; apply to just the first 50%, last 50%, first and last 1024 bytes, every two bytes, etc.  Doing so would make the cipher slightly more complex, and yet requiring only a slight change to the code (the condition of the loop).</p>
<p><!-- --><br />
<strong>ROTn</strong><br />
Instead of rotating by 128 positions, you can choose to rotate by some other number between 1 to 127. Of course, 128 is the only number that will allow you to use the exact same code for encoding and decoding.  Using any other number would require opposite operations &#8211; if you increment for encoding, then you need to decrement for decoding.</p>
<p><!-- --><br />
<strong>Usage Examples</strong><br />
In the next couple of posts, we will revisit two topics discussed in this blog previously and see how ROT128 can be applied in those scenarios:<br />
(i) <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">Hiding Assets And Code By Embedding SWF Within Another SWF</a>; and<br />
(ii) <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">Saving XML As Binary</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[AS3] Truncating ByteArray Does Not Dispose Contents, Free Up Memory</title>
		<link>http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 06:35:51 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Flash Player Bug]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1097</guid>
		<description><![CDATA[When targeting Flash Player 10 or AIR 1.5, you can use the clear() method of the ByteArray class to explicitly clear the contents of the byte array and free up the memory otherwise used by the bytes. The length and position properties are reset to zero after calling the clear() method. Unfortunately, when targeting Flash [...]]]></description>
			<content:encoded><![CDATA[<p>When targeting Flash Player 10 or AIR 1.5, you can use the <code>clear()</code> method of the <code>ByteArray</code> class to explicitly clear the contents of the byte array and free up the memory otherwise used by the bytes.  The <code>length</code> and <code>position</code> properties are reset to zero after calling the <code>clear()</code> method.</p>
<p>Unfortunately, when targeting Flash Player 9, this <code>clear()</code> method is not available.  If you are using a <code>ByteArray</code> object as a data store, keeping a reference to the object and therefore not allowing the object to be garbage collected, do take note that there is no way to clear the contents. This means that the size of <code>ByteArray</code> objects can only be enlarged and never shrunk.</p>
<p>It is important to note that while you can truncate the <code>ByteArray</code> to zero byte by setting its <code>length</code> property to zero, this will not dispose the contents or free up the memory used.</p>
<p><span id="more-1097"></span>This is actually a rather odd implementation.  After you truncate a <code>ByteArray</code>, the bytes stay in memory and is in fact restorable simply by setting the <code>length</code> property back to a non-zero value.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ABCDE&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 65</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 66</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 67</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 68</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 69</span>
bytes.<span style="color: #0066CC;">length</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// undefined</span>
bytes.<span style="color: #0066CC;">length</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 65 ** what the ??? necromancy at work ??? **</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 66</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 67</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 68</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 69</span></pre></div></div>

<p>In my opinion, this implementation is flawed.  The Flash Player really should clear the data once the array of bytes is truncated; this should not require a new API method <code>clear()</code> to accomplish. To introduce a new API method that does what setting the <code>length</code> property to zero <em>should</em> have done in the first place really baffles me.</p>
<p>Anyway, rant aside, so that is that &#8211; when you truncate a <code>ByteArray</code>, remember that the data is not lost.  When targeting Flash Player 10 or AIR 1.5, do remember to call the <code>clear()</code> method instead of attempting to clear the contents by truncating.</p>
<p><!-- --><br />
<strong>Zerofying Bytes</strong><br />
Since the data stays in memory, I reckon it could sometimes be useful to erase the data explicitly, especially if it contains sensitive data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> zerofy<span style="color: #66cc66;">&#40;</span>bytes:ByteArray<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = bytes.<span style="color: #0066CC;">length</span>;
		<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			bytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I am not sure if the <code>clear()</code> method in FP10 API does this (or something similar to erase the data in memory) &#8211; I sure hope it does.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Hiding Assets And Code By Embedding SWF Within Another SWF</title>
		<link>http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 03:11:59 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Copy Protection]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[FlashDevelop]]></category>
		<category><![CDATA[Loader]]></category>

		<guid isPermaLink="false">http://ghostwire.com/blog/?p=709</guid>
		<description><![CDATA[The technique discussed below is fairly easy to implement and will cost nothing other than a few minutes of your time. You can use this method in conjunction with code obfuscation, encryption or whatever other protection methods &#8211; this just adds another layer of protection. While this isn&#8217;t going to be a 100% foolproof protection, [...]]]></description>
			<content:encoded><![CDATA[<p>The technique discussed below is fairly easy to implement and will cost nothing other than a few minutes of your time.  You can use this method in conjunction with code obfuscation, encryption or whatever other protection methods &#8211; this just adds another layer of protection.  While this isn&#8217;t going to be a 100% foolproof protection, it is nevertheless better than no protection at all, and should help to deter most if not all casual decompiling.</p>
<p><span id="more-709"></span>As indicated in the title of this post, the basic idea here is simply to embed your actual SWF within another &#8220;shell&#8221; SWF.  It is this &#8220;shell&#8221; SWF that you will then deploy/distribute.</p>
<p><!-- --><br />
<strong>MainShell Class</strong><br />
To embed a SWF within another SWF, you can use a document class like the one below:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainShell <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>		
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const bytes:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainShell<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			Loader<span style="color: #66cc66;">&#40;</span>addChild<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">loadBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> bytes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When this &#8220;shell&#8221; SWF is run, it will<br />
(i) create an instance of <code>flash.display.Loader</code>;<br />
(ii) add that <code>Loader</code> instance to the display list;<br />
(iii) instantiate an instance of the embedded SWF as a <code>ByteArray</code>; and<br />
(iv) load that <code>ByteArray</code> into the <code>Loader</code> instance.</p>
<p>This will work in the same way as loading an external module SWF into a host SWF, but in this case the module SWF is embedded inside the host SWF.  This is different from loading external SWFs because any external SWF can still be easily obtained from the browser&#8217;s cache and decompiled separately.</p>
<p>This set up will be completely transparent to the end-users &#8211; it will look as if the actual SWF has been run.</p>
<p><!-- --><br />
<strong>How This Works Against Decompilers</strong><br />
This protection method assumes that decompilers cannot <em>automatically</em> identify, extract and decompile the embedded binary object as a SWF.  Therefore, the assets (classes and symbols) in the &#8220;ActualSWF.swf&#8221; will no longer be directly accessible by decompilers.</p>
<p>I tried this embedding technique against the trial versions of a couple of decompilers (Sothink and Trillix) and the assets of the embedded SWF (ActionScript classes, artwork, movieclip symbols, etc.) are safely hidden from view.</p>
<p>Since this cost nothing and is so easy to implement, I would suggest you try it out and decide on your own the effectiveness of this.  Wrap one of your SWFs in a &#8220;shell&#8221; SWF as described above, and try to decompile it using one of the decompilers out there. Perhaps the non-trial versions and/or other decompilers I have not tried may be able to defeat this simple layer of protection, but even if they do&#8230;</p>
<p><!-- --><br />
<strong>Taking Things Further</strong><br />
While it is possible that makers of decompilers could eventually implement a feature to properly identify, extract and decompile embedded SWFs, you can take things further and make the protection more difficult to overcome.</p>
<p>Instead of embedding the SWF directly, you could run it through some encryption and embed the encrypted SWF &#8211; it is mime type &#8220;application/octet-stream&#8221;, so you can really embed any binary file (even invalid file types).  Subsequently, the &#8220;shell&#8221; SWF will decrypt the <code>ByteArray</code> before feeding it to the <code>loadBytes()</code> method of the <code>Loader</code> instance.</p>
<p>To be very clear though, the intention here is not exactly encryption.  The real objective here is to intentionally &#8220;corrupt&#8221; the embedded SWF, so that even if extracted, decompilers cannot easily identify and run/decompile it as a SWF standalone.</p>
<p>For the purpose of corrupting the SWF, there are countless ways &#8211; using simple encryption, bytes transpositions, append/prepend useless bytes, etc. well, you get the idea&#8230; basically anything that make the SWF file invalid, no longer executable as a SWF.  You can even split the SWF into two or more binary blobs and join them back during run-time.</p>
<p>Since there are so many different possible algorithms to corrupt the SWF, it is then practically impossible for decompilers to <em>automatically</em> identify the way to reconstruct the corrupted embedded SWF.</p>
<p><!-- --><br />
<strong>Disclaimer</strong><br />
Needless to say, if you decide to corrupt/encrypt the SWF, you must be able to reconstruct, in the &#8220;shell&#8221; SWF, the embedded binary object into a working SWF.  As a result, while we may be able to prevent automated decryption (primary objective here), do take note that this provides no protection against determined hacking (decompiling the &#8220;shell&#8221; SWF, getting the decryption logic, extracting the embedded SWF manually, decrypting it and then running the reconstructed SWF through the decompiler).</p>
<p><!-- --><br />
<strong>FlashDevelop</strong><br />
For users of the <a href="http://www.flashdevelop.org">FlashDevelop</a> IDE, you are probably aware of its SWF assets browser (unlike decompilers, SWF sniffing is used here ethically, and beautifully, for the purpose of implementing code completion, etc.).  If you attempt to browse the &#8220;shell&#8221; SWF within the FlashDevelop&#8217;s Project Manager panel, you will not see the assets (classes and symbols) in the embedded SWF.  This can be used as a way to quickly verify if a SWF is protected using the &#8220;embedding technique&#8221; discussed above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

