Flex——DES加密解密

使用开源项目as3crypto

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

为了方便调用,写个工具类CryptoUtil

package crypto

{

import com.hurlant.crypto.symmetric.DESKey;

import flash.utils.ByteArray;

import mx.utils.Base64Decoder;

import mx.utils.Base64Encoder;

public final class CryptoUtil

{

public static function DESEncrypt(src:String,k:String):String

{

var key:ByteArray = new ByteArray();

key.writeUTFBytes(k);

var bytes:ByteArray = new ByteArray();

bytes.writeUTFBytes(src);

var des:DESKey = new DESKey(key);

des.encrypt(bytes);

varbase64:Base64Encoder = newBase64Encoder();

base64.encodeBytes(bytes);

return base64.flush();

}

public static function DESDecrypt(src:String,k:String):String

{

var key:ByteArray = new ByteArray();

key.writeUTFBytes(k);

varbase64:Base64Decoder = newBase64Decoder();

base64.decode(src);

var bytes:ByteArray = base64.toByteArray();

var des:DESKey = new DESKey(key);

des.decrypt(bytes);

bytes.position = 0;

return bytes.readUTFBytes(bytes.length);

}

}

}

测试代码

var str:String = crypto.CryptoUtil.DESEncrypt("hlib收到了是浪费空间啊未来人口仅为上的浪费空间","hlib123");

trace(str);

str = crypto.CryptoUtil.DESDecrypt(str, "hlib123");

trace(str);

测试输出

jhkCY7Xlk+WIsOS6huaYr+a1qui0ueepuumXtOWViuacquadpeS6uuWPo+S7heS4uuS4iueahOa1

qui0ueepuumXtA==

hlib收到了是浪费空间啊未来人口仅为上的浪费空间

你可能感兴趣的:(Flex)