AES encryption sample code

4,254 views
Skip to first unread message

Wayne

unread,
Sep 18, 2008, 10:56:49 AM9/18/08
to As3Crypto Discussion List
Hi,

I urgently need to encrypt data using AES encryption. I have got all
the needed class files, but cant seem to find sample code of how to
implement this.

Can someone please PLEASE point me in the right direction. I have
searched every site out there and they all just point to
http://as3crypto.riaforge.org/ and http://crypto.hurlant.com/demo/
(which is great for comparing end results).

Please, anyone who has done AES encryption in AS3, please enlighten
me.

Many thanks in advance.

Wayne

MMAS

unread,
Sep 18, 2008, 11:04:09 AM9/18/08
to As3Crypto Discussion List
I used this:

http://crypto.hurlant.com/demo/srcview/

and specifically the file SecretTab.mxml to see how he implemented it
in the demo. I think once you check that out, you'll be able to figure
it out easily.

-MMAS

On Sep 18, 9:56 am, Wayne <wynand.mcc...@gmail.com> wrote:
> Hi,
>
> I urgently need to encrypt data using AES encryption. I have got all
> the needed class files, but cant seem to find sample code of how to
> implement this.
>
> Can someone please PLEASE point me in the right direction. I have
> searched every site out there and they all just point tohttp://as3crypto.riaforge.org/andhttp://crypto.hurlant.com/demo/

Jeesmon Jacob

unread,
Sep 18, 2008, 11:18:39 AM9/18/08
to as3c...@googlegroups.com
This is the code that I used successfully in my app:

public static function getAESKey():String {
var r:Random = new Random;
var b:ByteArray = new ByteArray
r.nextBytes(b, 256);
return Base64.encodeByteArray(b);
}

public static function encrypt(input:String, key:String,
algorithm:String = "aes-cbc", padding:String = "None"):String {
var kdata:ByteArray = Base64.decodeToByteArray(key);
var data:ByteArray = Hex.toArray(Hex.fromString(input));
var pad:IPad = padding == "pkcs5" ? new PKCS5 : new NullPad;
var mode:ICipher = Crypto.getCipher("simple-" + algorithm, kdata, pad);
pad.setBlockSize(mode.getBlockSize());
mode.encrypt(data);

return Base64.encodeByteArray(data);
}

encrypt("my string", getAESKey());

Wayne

unread,
Sep 22, 2008, 3:09:13 AM9/22/08
to As3Crypto Discussion List
Hi Guys,

Thanks a lot for the help, however I did encounter a few strange
issues and there was no time to figure them out because of deadline. I
ended up using AS2 encryption AES-Rijndael (http://www.pacem.it/
CMerighi/Posts/42,en-US/AES-
Rijndael_with_ActionScript_and_ASP.Net.aspx) and bridged that with the
AS3 file.

I would like to however figure this out for future reference. So here
goes if anyone wants to give me a hand:


Lets encrypt the string "string" using a public key (for example
purposes) of 12345678901234567890.
With the AS2 AES-Rijndael method, the encrypted string looks like:
f4c01b48669dc8be71dc53e4b5b89764

Doing the exact same thing with AS3Crypto, and using Jeesmon's
implementation method: iDHoEShPT4IpVMlWC6sTXQ==

I noticed that it's formatted completely differently, and using our
server side decryption also didn't work out.

Please find below the code I used, and how I changed it to use the
above public key. What bothers me is the double = sign at the end.

CODE:
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.crypto.symmetric.NullPad;
import com.hurlant.crypto.symmetric.PKCS5;
import com.hurlant.crypto.symmetric.IPad;
import com.hurlant.crypto.prng.Random;
import com.hurlant.util.Base64;
import com.hurlant.util.Hex;
import com.hurlant.crypto.Crypto;

function getAESKey ():String
{
var r:Random = new Random;
//var b:ByteArray = new ByteArray;
var pk:ByteArray = Hex.toArray("12345678901234567890");
//r.nextBytes (pk, 128);
return Base64.encodeByteArray(pk);
}
function encrypt (input:String, key:String, algorithm:String =
"aes-128-ecb", padding:String = "None"):String
{
var kdata:ByteArray = Base64.decodeToByteArray(key);
var data:ByteArray = Hex.toArray(Hex.fromString(input));
var pad:IPad = padding == "pkcs5" ? new PKCS5 : new NullPad;
var mode:ICipher = Crypto.getCipher(algorithm, kdata, pad);
pad.setBlockSize (mode.getBlockSize());
mode.encrypt (data);

return Base64.encodeByteArray(data);
}
mc_encrypt.addEventListener(MouseEvent.MOUSE_DOWN, doIt);

function doIt (e:Event)
{
trace("ENC: " + encrypt ("string", getAESKey()));
}

I also had a bit of a dig to see if I cant spot anything, and editing
com\hurlant\util\Base64.as shows the BASE64_CHARS string as:
BASE64_CHARS:String =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

I noticed that it has 65 characters and the extra one is in fact the =
sign. Correct me if I'm wrong, but should there not be just 64
characters?

Cheers,
Wayne

On Sep 18, 5:18 pm, "Jeesmon Jacob" <jees...@gmail.com> wrote:
> This is the code that I used successfully in my app:
>
> public static function getAESKey():String {
> var r:Random = new Random;
> var b:ByteArray = new ByteArray
> r.nextBytes(b, 256);
> return Base64.encodeByteArray(b);
> }
>
> public static function encrypt(input:String, key:String,
> algorithm:String = "aes-cbc", padding:String = "None"):String {
> var kdata:ByteArray = Base64.decodeToByteArray(key);
> var data:ByteArray = Hex.toArray(Hex.fromString(input));
> var pad:IPad = padding == "pkcs5" ? new PKCS5 : new NullPad;
> var mode:ICipher = Crypto.getCipher("simple-" + algorithm, kdata, pad);
> pad.setBlockSize(mode.getBlockSize());
> mode.encrypt(data);
>
> return Base64.encodeByteArray(data);
> }
>
> encrypt("my string", getAESKey());
>

Jeesmon Jacob

unread,
Sep 22, 2008, 11:38:18 AM9/22/08
to as3c...@googlegroups.com
Hi Wayne,

Please see the attached EncryptTest.mxml file that I used to encrypt
and decypt. My output is given below.

===================================

AES Key: EjRWeJASNFZ4kA==

AES Key (Hex): 12345678901234567890

Encrypted string: vJqGpg82S2I9V42jIon+xQx8hnj7zOMYG1qh6HcimFM=

Encrypted string (Hex):
bc9a86a60f364b623d578da32289fec50c7c8678fbcce3181b5aa1e877229853

Decrypted string: string

string to encrypt == decrypted string: true

===================================

EncryptTest.mxml

Wayne

unread,
Sep 25, 2008, 2:52:39 AM9/25/08
to As3Crypto Discussion List
Hi Jeesmon,

Thanks for this, I think the bit I missed out was
Hex.fromArray(Base64.decodeToByteArray(encryptedString));.

The hex encripted string seems correct, I will test and see if it
implements with our exisiting AS2 and backend decryption solution.

Thanks a lot for the help, I will be back if there are more issues,
but hopefully not.

Cheers,
Wayne

On Sep 22, 5:38 pm, "Jeesmon Jacob" <jees...@gmail.com> wrote:
> Hi Wayne,
>
> Please see the attached EncryptTest.mxml file that I used to encrypt
> and decypt. My output is given below.
>
> ===================================
>
> AES Key: EjRWeJASNFZ4kA==
>
> AES Key (Hex): 12345678901234567890
>
> Encrypted string: vJqGpg82S2I9V42jIon+xQx8hnj7zOMYG1qh6HcimFM=
>
> Encrypted string (Hex):
> bc9a86a60f364b623d578da32289fec50c7c8678fbcce3181b5aa1e877229853
>
> Decrypted string: string
>
> string to encrypt == decrypted string: true
>
> ===================================
>
> EncryptTest.mxml
> 4KViewDownload

Dave

unread,
Nov 19, 2008, 10:58:39 PM11/19/08
to As3Crypto Discussion List
Thank you very much for posting the sample!
Really do appreciate it.

On Sep 23, 2:38 am, "Jeesmon Jacob" <jees...@gmail.com> wrote:
> Hi Wayne,
>
> Please see the attached EncryptTest.mxml file that I used to encrypt
> and decypt. My output is given below.
>
> ===================================
>
> AES Key: EjRWeJASNFZ4kA==
>
> AES Key (Hex): 12345678901234567890
>
> Encrypted string: vJqGpg82S2I9V42jIon+xQx8hnj7zOMYG1qh6HcimFM=
>
> Encrypted string (Hex):
> bc9a86a60f364b623d578da32289fec50c7c8678fbcce3181b5aa1e877229853
>
> Decrypted string: string
>
> string to encrypt == decrypted string: true
>
> ===================================
>
>  EncryptTest.mxml
> 4KViewDownload
Reply all
Reply to author
Forward
0 new messages