<?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>zedia flash blog &#187; as3crypto</title>
	<atom:link href="http://www.zedia.net/tag/as3crypto/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Thu, 02 Feb 2012 17:58:56 +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>How to connect to Google Analytics Data API in AIR</title>
		<link>http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/</link>
		<comments>http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/#comments</comments>
		<pubDate>Fri, 22 May 2009 03:37:38 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[as3corelib]]></category>
		<category><![CDATA[as3crypto]]></category>
		<category><![CDATA[as3httpclientlib]]></category>
		<category><![CDATA[GData]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Google Analytics Data Api]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=415</guid>
		<description><![CDATA[I had a bit of trouble doing just this a while ago, ended up trying many different ways and finally a reader of this blog, Nakamachi, gave me the solution. Because I think many of you are trying to connect to Google Analytics Data API ( or any Google APIs for that matters) I will [...]]]></description>
			<content:encoded><![CDATA[<p>I had a bit of trouble doing <a title="Connecting to Google Analytics Data Api using AS3; my failures" href="http://www.zedia.net/2009/connecting-to-google-analytics-data-api-using-as3-my-failures/" target="_self">just this a while ago</a>, ended up trying many different ways and finally a reader of this blog, <span class="info">Nakamachi, gave me the solution. Because I think many of you are trying to connect to Google Analytics Data API ( or any Google APIs for that matters) I will give you here my solution. My solution only works in AIR; I don&#8217;t know why, but as soon as I put it on the web, it stops working. I have found a solution for that problem too but I will keep it for another post.</span><br />
Here is how you can connect to GData:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">HttpClient</span>;
<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">net</span>.<span style="color: #006600;">URI</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">HttpResponseEvent</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">HttpRequestEvent</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">HttpRequest</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">http</span>.<span style="color: #0066CC;">Get</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">HttpDataEvent</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> _firstBuffer:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> _secondBuffer:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//this first connection will get the authorization token by sending the user name and password for Google Analytics</span>
<span style="color: #000000; font-weight: bold;">function</span> submitForm<span style="color: #66cc66;">&#40;</span>email:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">password</span>:<span style="color: #0066CC;">String</span><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> client:HttpClient = <span style="color: #000000; font-weight: bold;">new</span> HttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> uri:URI = <span style="color: #000000; font-weight: bold;">new</span> URI<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.google.com/accounts/ClientLogin&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> variables:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;accountType&quot;</span>, value:<span style="color: #ff0000;">&quot;GOOGLE&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;Email&quot;</span>, value: email<span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;Passwd&quot;</span>, value: <span style="color: #0066CC;">password</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;service&quot;</span>, value: <span style="color: #ff0000;">&quot;analytics&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;source&quot;</span>, value: <span style="color: #ff0000;">&quot;your-application-identifier&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>;
  client.<span style="color: #006600;">listener</span>.<span style="color: #0066CC;">onData</span> = _onFirstData;
  client.<span style="color: #006600;">listener</span>.<span style="color: #006600;">onComplete</span> = _onLoaderComplete;
  client.<span style="color: #006600;">postFormData</span><span style="color: #66cc66;">&#40;</span>uri, variables<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> _onFirstData<span style="color: #66cc66;">&#40;</span>event:HttpDataEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  _firstBuffer += event.<span style="color: #006600;">readUTFBytes</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;">//this second connection will request information to GData</span>
<span style="color: #808080; font-style: italic;">//in this case account info for Google Analytics by</span>
<span style="color: #808080; font-style: italic;">//putting the auth token in the header of the request</span>
<span style="color: #000000; font-weight: bold;">function</span> _onLoaderComplete<span style="color: #66cc66;">&#40;</span>event:HttpResponseEvent<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> tempArray:<span style="color: #0066CC;">Array</span> = _firstBuffer.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Auth=&quot;</span><span style="color: #66cc66;">&#41;</span>;
 <span style="color: #000000; font-weight: bold;">var</span>  _authToken:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>tempArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> client:HttpClient = <span style="color: #000000; font-weight: bold;">new</span> HttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> uri:URI = <span style="color: #000000; font-weight: bold;">new</span> URI<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.google.com/analytics/feeds/accounts/default&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> request:HttpRequest = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Get</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  request.<span style="color: #006600;">addHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Authorization&quot;</span>, <span style="color: #ff0000;">&quot;GoogleLogin auth=&quot;</span> + _authToken<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  client.<span style="color: #006600;">listener</span>.<span style="color: #0066CC;">onData</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event:HttpDataEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    _secondBuffer += event.<span style="color: #006600;">readUTFBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
  client.<span style="color: #006600;">listener</span>.<span style="color: #006600;">onComplete</span> = _onSecondLoaderComplete;
  client.<span style="color: #006600;">request</span><span style="color: #66cc66;">&#40;</span>uri, request<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> _onSecondLoaderComplete<span style="color: #66cc66;">&#40;</span>event:HttpResponseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span>_secondBuffer<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see in order to circumvent the Authorization header restriction I am using the <a title="as3httpclientlib" href="http://code.google.com/p/as3httpclientlib/">as3httpclientlib</a> library which requires the <a title="as3corelib" href="http://code.google.com/p/as3corelib/" target="_blank">as3corelib</a> and <a title="as3crypto" href="http://code.google.com/p/as3crypto/" target="_blank">as3crypto</a> libraries to compile. Using the same principle you should be able to connect to Google APIs from your AIR application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>as3Crypto and php, what a fun ride!</title>
		<link>http://www.zedia.net/2009/as3crypto-and-php-what-a-fun-ride/</link>
		<comments>http://www.zedia.net/2009/as3crypto-and-php-what-a-fun-ride/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 03:08:33 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Advanced Encryption Standard]]></category>
		<category><![CDATA[AES]]></category>
		<category><![CDATA[as3corelib]]></category>
		<category><![CDATA[as3crypto]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[decrypt]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[mcrypt]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=393</guid>
		<description><![CDATA[Actually not so fun, but I did manage (I should say we because I wasn&#8217;t alone in this). Cryptography is not my thing, eh, not everything can be your thing so I accept it. There is just too much to learn: hash functions, public keys, symmetric ciphers, etc. Want we wanted to do was to [...]]]></description>
			<content:encoded><![CDATA[<p>Actually not so fun, but I did manage (I should say we because I wasn&#8217;t alone in this). Cryptography is not my thing, eh, not everything can be your thing so I accept it. There is just too much to learn: hash functions, public keys, symmetric ciphers, etc. Want we wanted to do was to encrypt data on the As3 side and decrypt it on the php side. I was aware that there was some cryptographic algorythms in the <a title="As3CoreLib" href="http://code.google.com/p/as3corelib/" target="_blank">as3corelib</a>, but none of them (MD5, SHA-1) fitted our needs. There is another great cryptography library out there and it is <a title="As3Crypto" href="http://code.google.com/p/as3crypto/" target="_blank">as3Crypto</a>; the problem is that it is a bit hard to get around, there is a lot to choose from. We settled on AES (Advanced Encryption Standard). After 2 hours of trying to get it to work, we found this great post on <a title="As3Crypto + PHP" href="http://groups.google.com/group/as3crypto/browse_thread/thread/eba4897dabc57cd6" target="_blank">Google groups</a> (about middle of the page). I am copying the content here to make it easier for people to find. I have to give all the credit for this post to Jason Foglia who posted his code.</p>
<p>Here is the As3 Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">&nbsp;
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;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">symmetric</span>.<span style="color: #006600;">ICipher</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">symmetric</span>.<span style="color: #006600;">IVMode</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">symmetric</span>.<span style="color: #006600;">IMode</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">symmetric</span>.<span style="color: #006600;">NullPad</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">symmetric</span>.<span style="color: #006600;">PKCS5</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">symmetric</span>.<span style="color: #006600;">IPad</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">util</span>.<span style="color: #006600;">Base64</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">util</span>.<span style="color: #006600;">Hex</span>;
        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">hurlant</span>.<span style="color: #006600;">crypto</span>.<span style="color: #006600;">Crypto</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CryptoCode <span style="color: #0066CC;">extends</span> Sprite
        <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">'simple-des-ecb'</span>;
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">key</span>:ByteArray;
&nbsp;
                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CryptoCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#123;</span>
                        init<span style="color: #66cc66;">&#40;</span><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: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
                <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #0066CC;">key</span> = Hex.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span>Hex.<span style="color: #006600;">fromString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'TESTTEST'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">// can only be 8 characters long</span>
&nbsp;
                        <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>encrypt<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'TEST TEST'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>decrypt<span style="color: #66cc66;">&#40;</span>encrypt<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'TEST TEST'</span><span style="color: #66cc66;">&#41;</span><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> encrypt<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
                <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = Hex.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span>Hex.<span style="color: #006600;">fromString</span><span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                        <span style="color: #000000; font-weight: bold;">var</span> pad:IPad = <span style="color: #000000; font-weight: bold;">new</span> PKCS5;
                        <span style="color: #000000; font-weight: bold;">var</span> mode:ICipher = Crypto.<span style="color: #006600;">getCipher</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, <span style="color: #0066CC;">key</span>, pad<span style="color: #66cc66;">&#41;</span>;
                        pad.<span style="color: #006600;">setBlockSize</span><span style="color: #66cc66;">&#40;</span>mode.<span style="color: #006600;">getBlockSize</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                        mode.<span style="color: #006600;">encrypt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #b1b100;">return</span> Base64.<span style="color: #006600;">encodeByteArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> decrypt<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
                <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = Base64.<span style="color: #006600;">decodeToByteArray</span><span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #000000; font-weight: bold;">var</span> pad:IPad = <span style="color: #000000; font-weight: bold;">new</span> PKCS5;
                        <span style="color: #000000; font-weight: bold;">var</span> mode:ICipher = Crypto.<span style="color: #006600;">getCipher</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, <span style="color: #0066CC;">key</span>, pad<span style="color: #66cc66;">&#41;</span>;
                        pad.<span style="color: #006600;">setBlockSize</span><span style="color: #66cc66;">&#40;</span>mode.<span style="color: #006600;">getBlockSize</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                        mode.<span style="color: #006600;">decrypt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #b1b100;">return</span> Hex.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span>Hex.<span style="color: #006600;">fromArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</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>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Here is the php class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">Crypt</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">Crypt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$key</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">algorithm</span> <span style="color: #339933;">=</span> MCRYPT_DES<span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mode</span> <span style="color: #339933;">=</span> MCRYPT_MODE_ECB<span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">iv_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_get_iv_size</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">algorithm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">iv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_create_iv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_get_block_size</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">algorithm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pkcs5_pad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mcrypt_encrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">algorithm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mode</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">iv</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pkcs5_unpad</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mcrypt_decrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">algorithm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span><span style="color: #339933;">,</span> <span style="color: #990000;">base64_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mode</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">iv</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> pkcs5_pad<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$blocksize</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$pad</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$blocksize</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$blocksize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pad</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pad</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> pkcs5_unpad<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$pad</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#123;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pad</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strspn</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pad</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$pad</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$pad</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$pad</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now if you use the encrypt method of one you can send the data to the other one and decrypt it in the other language if you use the same key. Works like a charm. There is a mention that the key can only be 8 characters long but I haven&#8217;t tested it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2009/as3crypto-and-php-what-a-fun-ride/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

