As3crypto is a library for encryption under action script 3 (Flash). However I had major problems implemented it, until I found this
post. Here is the code I use with little modification, simple includes this file in your action script to encyrpt/decrypt data.
/*Modify by Kevin Chiang 2014/4/22*/
package
{
import com.hurlant.crypto.Crypto;
import com.hurlant.crypto.prng.Random;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.crypto.symmetric.IPad;
import com.hurlant.crypto.symmetric.IVMode;
import com.hurlant.crypto.symmetric.PKCS5;
import com.hurlant.util.Base64;
import com.hurlant.util.Hex;
import flash.utils.ByteArray;
public class Des
{
public static const DEFAULT_CIPHER_NAME:String = "des-cbc";
public static const DEFAULT_PADNAME:String = "pkcs5";
public static const NULL_PADDING:String = "null";
private static const RAND:Random = new Random();
private var _name:String;
private var _key:ByteArray;
private var _iv:ByteArray;
private var _padName:String;
private var _enc:ICipher;
private var _dec:ICipher;
public function Des(key:ByteArray, iv:ByteArray = null, name:String = DEFAULT_CIPHER_NAME, padName:String = DEFAULT_PADNAME)
{
_name = name;
_key = key;
_iv = iv;
_padName = padName;
init();
}
private function init():void
{
var _pad:IPad = Crypto.getPad(_padName);
_enc = Crypto.getCipher(_name, _key, _pad);
_dec = Crypto.getCipher(_name, _key, _pad);
if (iv)
{
if (_enc is IVMode)
{
var encIvm:IVMode = _enc as IVMode;
encIvm.IV = iv;
}
if (_dec is IVMode)
{
var decIvm:IVMode = _dec as IVMode;
decIvm.IV = iv;
}
}
}
public static function generateKey(name:String):ByteArray
{
var keyLength:uint = Crypto.getKeySize(name);
var key:ByteArray = new ByteArray();
RAND.nextBytes(key, keyLength);
return key;
}
public static function generateIV(name:String, key:ByteArray):ByteArray
{
var cipher:ICipher = Crypto.getCipher(name, key);
var iv:ByteArray = new ByteArray();
RAND.nextBytes(iv, cipher.getBlockSize());
return iv;
}
public function encrypt(input:ByteArray):ByteArray
{
var src:ByteArray = new ByteArray();
var result:ByteArray = new ByteArray();
src.writeBytes(input, 0, input.length);
_enc.encrypt(input);
result.writeBytes(input, 0, input.length);
input.length = 0;
input.writeBytes(src, 0, src.length);
src.clear();
return result;
}
public function decrypt(input:ByteArray):ByteArray
{
var src:ByteArray = new ByteArray();
var result:ByteArray = new ByteArray();
src.writeBytes(input, 0, input.length);
_dec.decrypt(input);
result.writeBytes(input, 0, input.length);
input.length = 0;
input.writeBytes(src, 0, src.length);
src.clear();
return result;
}
public function encryptString(input:String):ByteArray
{
if (!input || !input.length)
{
return null;
}
var inputBytes:ByteArray = new ByteArray();
inputBytes.writeUTFBytes(input);
return encrypt(inputBytes);
}
public function encryptString2Hex(input:String):String
{
var result:ByteArray = encryptString(input);
return Hex.fromArray(result);
}
public function encryptString2Base64(input:String):String
{
var result:ByteArray = encryptString(input);
return Base64.encodeByteArray(result);
}
public function decryptString(input:String):ByteArray
{
if (!input || !input.length)
{
return null;
}
var inputBytes:ByteArray = new ByteArray();
inputBytes.writeUTFBytes(input);
return decrypt(inputBytes);
}
public function decryptString2Hex(input:String):String
{
var result:ByteArray = decryptString(input);
return Hex.fromArray(result);
}
public function decryptString2Base64(input:String):String
{
var result:ByteArray = decryptString(input);
return Base64.encodeByteArray(result);
}
public function set iv(value:ByteArray):void
{
_iv = value;
}
public function get iv():ByteArray
{
return _iv;
}
}
}
Here is how to use it
private var des:Des;
private var key:ByteArray;
private var iv:ByteArray;
private var text:ByteArray;
public function Main():void {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
key = Hex.toArray("YOU KEY");
iv = Hex.toArray("YOUR IV USUALLY 16 digits of HexDecimal");
des = new Des(key, iv);
var encString:ByteArray = des.encryptString("ABCDEFG");
var b64String:String = Base64.encode(encString.toString());
trace("Encrypted String = " + b64String);
var byteArray:ByteArray = Base64.decodeToByteArray(b64String);
trace(des.decrypt(byteArray).toString());
that's it, hope it helps.