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());
>