xmlns:mx=”library://ns.adobe.com/flex/halo” minWidth=”1024″ minHeight=”768″ xmlns:ns1=”*”>
import com.hurlant.crypto.Crypto;
import com.hurlant.crypto.prng.Random;
import com.hurlant.crypto.rsa.RSAKey;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.crypto.symmetric.IVMode;
import com.hurlant.util.Hex;
private var cipher:ICipher;
private var key:ByteArray;
private var rand:Random;
private var iv:ByteArray;
private var text:ByteArray;
private function init():void{
rand = new Random();
encryptHandler();
}
private function encryptHandler():void{
var keyLength:uint = Crypto.getKeySize("des-cbc");
trace(keyLength,"keyLength");
key = new ByteArray();
key.writeByte(41);
key.writeByte(70);
key.writeByte(218);
key.writeByte(223);
key.writeByte(50);
key.writeByte(14);
key.writeByte(158);
key.writeByte(110);
//rand.nextBytes(key, keyLength);
trace(key.length,"key.length");
cipher = Crypto.getCipher("des-cbc", key);
iv = new ByteArray();
trace(cipher.getBlockSize(),"cipher.getBlockSize()");
rand.nextBytes(iv, cipher.getBlockSize());
trace(Hex.fromArray(key)+" --------:");
if (cipher is IVMode) {
var ivm:IVMode = cipher as IVMode;
ivm.IV = iv;
}
text = new ByteArray;
var len:uint = rand.nextByte()+1; // between 1 and 256
//rand.nextBytes(text, len);
text.writeUTF("abcd");
trace(Hex.fromArray(text));
cipher.encrypt(text);
var as3:String = Hex.fromArray(text);
trace(as3);
decryptHandler();
//encryptHandler();
}
private function decryptHandler():void{
cipher = Crypto.getCipher("des-cbc", key);
if (cipher is IVMode) {
var ivm:IVMode = cipher as IVMode;
ivm.IV = iv;
}
cipher.decrypt(text);
var as3:String = Hex.fromArray(text);
trace(as3);
text.position = 0;
trace(text.readUTF());
trace("-------------------------");
}
]]>
RSA实例:
xmlns:mx=”library://ns.adobe.com/flex/halo” creationComplete=”init()”minWidth=”1024″ minHeight=”768″>
import com.hurlant.crypto.rsa.RSAKey;
import com.hurlant.util.Base64;
import com.hurlant.util.Hex;
private var currentModulus:String = "5a8eafa90fdf52
1bf8a3931837711db0e9fc9545485cbce8d5576cd9
a7ca6c003690b929873b2b49a2160cd
86b369a0b8b5c71349a933c80ed8ac3a81b012167";
private var currentExponent:String;
private var currentPrivate:String;
private var currentP:String;
private var currentQ:String;
private var currentDMP1:String;
private var currentDMQ1:String;
private var currentCoeff:String;
private var currentResult:String;
private var currentInput:String;
private function init():void{
var rsa:RSAKey = RSAKey.generate(512, "10001");
currentModulus = rsa.n.toString();
currentPrivate = rsa.d.toString();
currentP = rsa.p.toString();
currentQ = rsa.q.toString();
currentDMP1 = rsa.dmp1.toString();
currentDMQ1 = rsa.dmq1.toString();
currentCoeff = rsa.coeff.toString();
}
private function encryptHandler():void {
var rsa:RSAKey = RSAKey.parsePublicKey(currentModulus, "10001");
var data:ByteArray = Hex.toArray(Hex.fromString("test"));
var dst:ByteArray = new ByteArray;
rsa.encrypt(data, dst, data.length);
currentResult = Hex.fromArray(dst);
}
private function decryptHandler():void {
var rsa:RSAKey = RSAKey.parsePrivateKey(currentModulus, "10001", currentPrivate, currentP, currentQ, currentDMP1,currentDMQ1,currentCoeff);
var data:ByteArray = Hex.toArray(this.currentResult);
var dst:ByteArray = new ByteArray;
rsa.decrypt(data, dst, data.length);
currentInput = Hex.fromArray(dst);
trace("decrypt");
displayField(this.currentInput,"text");
}
private function displayField(storage:String, format:String):void {
if (storage==null) return;
var txt:String;
var format:String = String(format);
switch (format) {
case "hex": txt = storage; break;
case "b64": txt = linebrk(Base64.encodeByteArray(Hex.toArray(storage)), 60); break;
default:
txt = Hex.toString(storage);
}
trace("txt", txt);
}
private function linebrk(s:String,n:uint):String {
var ret:String = "";
var i:int = 0;
while(i + n < s.length) {
ret += s.substring(i,i+n) + "\n";
i += n;
}
return ret + s.substring(i,s.length);
}
]]>
crypto类库: http://code.google.com/p/as3crypto/