[Flash] as3crypto

  • as3crypto
  • as3crypto是由henrit等人合力完成的Actionscript 3.0 Cryptography Library,提供了RSA、Data Encryption、Message-Digest等功能,讓Flash使用者可以輕鬆完成加密的工作,你可以從下面的網頁下載到這個API

    http://code.google.com/p/as3crypto/

    官方提供了一個很好的範例如下:

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

    我則使用as3crypto簡單做了一個Data Encryption/Decryption的demo(理論上來說,如果要為了效能應該是要用C+OpenSSL),程式碼裡直接指定使用Triple-DES+ECB Mode,而Key的長度預設為128bits,原始碼如下:

    package{
    import flash.display.Sprite;
    import flash.net.FileReference;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.ByteArray;

    import fl.controls.Button;

    import com.hurlant.crypto.symmetric.ICipher;
    import com.hurlant.crypto.symmetric.IVMode;
    import com.hurlant.crypto.symmetric.IMode;
    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.crypto.hash.HMAC;
    import com.hurlant.util.Base64;
    import com.hurlant.util.Hex;
    import com.hurlant.crypto.Crypto;
    import com.hurlant.crypto.hash.IHash;

    public class CryptoDemo extends Sprite{
    private var loadFile:FileReference;
    private var saveFile:FileReference;

    private var plainData:ByteArray;
    private var cipherData:ByteArray;

    private var option:String;

    public function CryptoDemo(){
    encryBtn.label= "Encrypt";
    encryBtn.addEventListener(MouseEvent.CLICK, onClicked);

    decryBtn.label = "Decrypt";
    decryBtn.addEventListener(MouseEvent.CLICK, onClicked);

    keyLabel.text = "Key:";
    infoLabel.text = "Info:";

    genKeyBtn.label = "Generate 128 Bits";
    genKeyBtn.addEventListener(MouseEvent.CLICK, onClicked);

    loadFile = new FileReference();
    loadFile.addEventListener(Event.SELECT, onFileSelected);
    loadFile.addEventListener(Event.COMPLETE, onFileLoaded);

    saveFile = new FileReference();
    }

    private function onClicked(event:MouseEvent):Boolean{
    if(event.target.label == "Generate 128 Bits"){
    genKey(128);
    }else{
    if(keyIn.text == ""){
    infoTxt.text = "Please give a key!";
    return false;
    }
    option = event.target.label;
    loadFile.browse();
    }

    return true;
    }

    private function onFileSelected(event:Event):void{
    event.target.load();
    }

    private function onFileLoaded(event:Event):void{
    infoTxt.text = "Filename: "+event.target.name+"\nSize: "+event.target.size+" Bytes";

    switch(option){
    case "Encrypt":
    plainData = event.target.data;
    encrypt();
    saveFile.save(plainData, "encrypt.dat");
    break;
    case "Decrypt":
    cipherData = event.target.data;
    decrypt();
    saveFile.save(cipherData, "decrypt.dat");
    break;
    }

    plainData = cipherData = null;
    }

    private function genKey(v:int):void {
    var r:Random = new Random;
    var b:ByteArray = new ByteArray
    r.nextBytes(b, v/8);
    keyIn.text = Hex.fromArray(b);
    }

    private function encrypt():void {
    // key
    var k:String = keyIn.text;
    var kdata:ByteArray;
    kdata = Hex.toArray(Hex.fromString(k));

    // algorithm..
    var name:String = "simple-des3-ecb";

    // encryption
    var pad:IPad = new PKCS5;
    var mode:ICipher = Crypto.getCipher(name, kdata, pad);
    pad.setBlockSize(mode.getBlockSize());
    mode.encrypt(plainData);
    }

    private function decrypt():void {
    // key
    var k:String = keyIn.text;
    var kdata:ByteArray;
    kdata = Hex.toArray(Hex.fromString(k));

    // algorithm..
    var name:String = "simple-des3-ecb";

    // decryption
    var pad:IPad = new PKCS5;
    var mode:ICipher = Crypto.getCipher(name, kdata, pad);
    pad.setBlockSize(mode.getBlockSize());
    mode.decrypt(cipherData);
    }
    }
    }

    預覽畫面如下:

    由於Flash Player基於安全性的考量,FileReference物件的save()函式必須由mouse click等event來觸發,所以上面的範例將無法讓你正常儲存。

No comments:

Post a Comment

Orange - data analysis tool

Installation pip install orange3 Run orange python -m Orange.canvas