as3corelib Tutorial:How to Use Crypto Class in Flex

fish-2-128x128 Since this article, I won’t post the language reference of such libraries as before. It’s really a boring thing to do repeated works. All the language reference you will find in the packaged file provided on the corelib project home http://code.google.com/p/as3corelib/ , or download from this link http://as3corelib.googlecode.com/files/as3corelib-.92.1.zip directly.

Thanks!

Search-256x256 Demo | DownloadDownload Full Project

Crypto-class

Package Crypto:

All the common crypto algorithms are packaged together here. It includes algorithms MD5, SHA1, SHA224, SHA256 and a special WSSEUsernameToken for Web Services Security. Take it easy, there is little to learn to take advantage of these stuffs. Just follow me!

CryptoDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import com.adobe.crypto.WSSEUsernameToken;
			import com.adobe.crypto.SHA256;
			import com.adobe.crypto.SHA224;
			import com.adobe.crypto.MD5;
			import com.adobe.crypto.SHA1;
			[Bindable]
			private var originString:String = "this is a string to be encrypted";

			[Bindable]
			private var userName:String = "thisIsAnUserName";

			[Bindable]
			private var password:String = "thisIsAPassword";

			private var _byteArray:ByteArray;

			private function get byteArray():ByteArray
			{
				if(!_byteArray)
				{
					_byteArray = new ByteArray();
					_byteArray.writeMultiByte(originString,"utf-8");
				}
				return _byteArray;
			}

			private function byteArrayToString(byteArray:ByteArray):String
			{
				byteArray.position = 0;
				var str:String = "";
				while(byteArray.position != byteArray.length - 1)
				{
					str += byteArray.readByte().toString(16).toUpperCase()+" ";
				}
				return str;
			}
		]]>
	</mx:Script>
	<mx:Panel width="100%" headerColors="100%" title="Demo of classes under crypto namespace included in corelib ">
		<mx:Form>
			<mx:FormItem label="origin string">
				<mx:TextInput editable="false" text="{originString}"/>
			</mx:FormItem>
			<mx:FormItem label="origin ByteArray">
				<mx:TextInput editable="false" text="{byteArrayToString(byteArray)}"/>
			</mx:FormItem>

			<mx:FormHeading label="MD5 algorithm"/>
			<mx:FormItem label="encrypt string">
				<mx:TextInput editable="false" text="{MD5.hash(originString)}"/>
			</mx:FormItem>

			<mx:FormHeading label="SHA1 algorithm"/>
			<mx:FormItem label="encrypt string">
				<mx:TextInput editable="false" text="{SHA1.hash(originString)}"/>
			</mx:FormItem>
			<mx:FormItem label="encrypt string(Base64)">
				<mx:TextInput editable="false" text="{SHA1.hashToBase64(originString)}"/>
			</mx:FormItem>
			<mx:FormItem label="encrypt ByteArray">
				<mx:TextInput editable="false" text="{SHA1.hashBytes(byteArray)}"/>
			</mx:FormItem>

			<mx:FormHeading label="SHA224 algorithm"/>
			<mx:FormItem label="encrypt string">
				<mx:TextInput editable="false" text="{SHA224.hash(originString)}"/>
			</mx:FormItem>
			<mx:FormItem label="encrypt string(Base64)">
				<mx:TextInput editable="false" text="{SHA224.hashToBase64(originString)}"/>
			</mx:FormItem>
			<mx:FormItem label="encrypt ByteArray">
				<mx:TextInput editable="false" text="{SHA224.hashBytes(byteArray)}"/>
			</mx:FormItem>

			<mx:FormHeading label="SHA256 algorithm"/>
			<mx:FormItem label="encrypt string">
				<mx:TextInput editable="false" text="{SHA256.hash(originString)}"/>
			</mx:FormItem>
			<mx:FormItem label="encrypt string(Base64)">
				<mx:TextInput editable="false" text="{SHA256.hashToBase64(originString)}"/>
			</mx:FormItem>
			<mx:FormItem label="encrypt ByteArray">
				<mx:TextInput editable="false" text="{SHA256.hashBytes(byteArray)}"/>
			</mx:FormItem>

			<mx:FormHeading label="	WSSEUsernameToken algorithm"/>
			<mx:FormItem label="username">
				<mx:TextInput editable="false" text="{ userName}"/>
			</mx:FormItem>
			<mx:FormItem label="password">
				<mx:TextInput editable="false" text="{ password}"/>
			</mx:FormItem>
			<mx:FormItem label="token">
				<mx:Text text="{WSSEUsernameToken.getUsernameToken(userName,password) }" width="400"/>
			</mx:FormItem>
		</mx:Form>
	</mx:Panel>
</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “as3corelib Tutorial:How to Use Crypto Class in Flex”

  1. damn says:

    SHA and MD5 are not *encrypting* things, they *hash* them.
    Your ‘encrypt’ labels are misleading for those looking for actual encryption/decryption.

Leave a Reply