[AS3]as3加密算法AES加密算法
[AS3]as3加密算法AES加密算法
[AS3]as3加密算法AES加密算法
- 1: package {
- 2: import com.hurlant.crypto.Crypto;
- 3: import com.hurlant.crypto.prng.Random;
- 4: import com.hurlant.crypto.symmetric.ICipher;
- 5: import com.hurlant.crypto.symmetric.IPad;
- 6: import com.hurlant.crypto.symmetric.IVMode;
- 7: import com.hurlant.crypto.symmetric.PKCS5;
- 8: import com.hurlant.util.Base64;
- 9: import com.hurlant.util.Hex;
- 10:
- 11: import flash.utils.ByteArray;
- 12:
- 13: /**
- 14: * AES 算法的加密解密工具类。
- 15: *
- 16: * @author Fuchun
- 17: * @langversion 3.0
- 18: * @playerversion Flash 11.1
- 19: * @productversion 1.0
- 20: */
- 21: public class AES {
- 22:
- 23: /**
- 24: * 默认的算法与模式名称。
- 25: */
- 26: public static const DEFAULT_CIPHER_NAME:String = "aes-128-cbc";
- 27:
- 28: /**
- 29: * 默认的填充模式。<code>pkcs5</code>
- 30: */
- 31: public static const DEFAULT_PADNAME:String = "pkcs5";
- 32:
- 33: /**
- 34: * 无填充。
- 35: */
- 36: public static const NULL_PADDING:String = "null";
- 37:
- 38: private static const RAND:Random = new Random();
- 39:
- 40: private var _name:String;
- 41: // 密钥
- 42: private var _key:ByteArray;
- 43: // 向量
- 44: private var _iv:ByteArray;
- 45: // 填充模式
- 46: private var _padName:String;
- 47: private var _enc:ICipher;
- 48: private var _dec:ICipher;
- 49:
- 50: public function AES(key:ByteArray, iv:ByteArray = null, name:String = DEFAULT_CIPHER_NAME, padName:String = DEFAULT_PADNAME) {
- 51: _name = name;
- 52: _key = key;
- 53: _iv = iv;
- 54: _padName = padName;
- 55: init();
- 56: }
- 57:
- 58: private function init():void {
- 59: var _pad:IPad = Crypto.getPad(_padName);
- 60: _enc = Crypto.getCipher(_name, _key, _pad);
- 61: _dec = Crypto.getCipher(_name, _key, _pad);
- 62: if (iv) {
- 63: if (_enc is IVMode) {
- 64: var encIvm:IVMode = _enc as IVMode;
- 65: encIvm.IV = iv;
- 66: }
- 67: if (_dec is IVMode) {
- 68: var decIvm:IVMode = _dec as IVMode;
- 69: decIvm.IV = iv;
- 70: }
- 71: }
- 72: }
- 73:
- 74: public static function generateKey(name:String):ByteArray {
- 75: var keyLength:uint = Crypto.getKeySize(name);
- 76: var key:ByteArray = new ByteArray();
- 77: RAND.nextBytes(key, keyLength);
- 78: return key;
- 79: }
- 80:
- 81: public static function generateIV(name:String, key:ByteArray):ByteArray {
- 82: var cipher:ICipher = Crypto.getCipher(name, key);
- 83: var iv:ByteArray = new ByteArray();
- 84: RAND.nextBytes(iv, cipher.getBlockSize());
- 85: return iv;
- 86: }
- 87:
- 88: public function encrypt(input:ByteArray):ByteArray {
- 89: var src:ByteArray = new ByteArray();
- 90: var result:ByteArray = new ByteArray();
- 91: src.writeBytes(input, 0, input.length);
- 92:
- 93: _enc.encrypt(input);
- 94: result.writeBytes(input, 0, input.length);
- 95: input.length = 0;
- 96: input.writeBytes(src, 0, src.length);
- 97:
- 98: src.clear();
- 99: return result;
- 100: }
- 101:
- 102: public function decrypt(input:ByteArray):ByteArray {
- 103: var src:ByteArray = new ByteArray();
- 104: var result:ByteArray = new ByteArray();
- 105: src.writeBytes(input, 0, input.length);
- 106:
- 107: _dec.decrypt(input);
- 108: result.writeBytes(input, 0, input.length);
- 109: input.length = 0;
- 110: input.writeBytes(src, 0, src.length);
- 111:
- 112: src.clear();
- 113: return result;
- 114: }
- 115:
- 116: public function encryptString(input:String):ByteArray {
- 117: if (!input || !input.length) {
- 118: return null;
- 119: }
- 120: var inputBytes:ByteArray = new ByteArray();
- 121: inputBytes.writeUTFBytes(input);
- 122: return encrypt(inputBytes);
- 123: }
- 124:
- 125: //无配对的解密方法,加密后的结果无法直接用decryptString2Hex解密
- 126: public function encryptString2Hex(input:String):String {
- 127: var result:ByteArray = encryptString(input);
- 128: return Hex.fromArray(result);
- 129: }
- 130:
- 131: //无配对的解密方法,加密后的结果无法直接用decryptString2Hex解密
- 132: public function encryptString2Base64(input:String):String {
- 133: var result:ByteArray = encryptString(input);
- 134: return Base64.encodeByteArray(result);
- 135: }
- 136:
- 137: //代码中我们一般习惯直接加密字符串,加密后直接得到密文字符串,这里没有提供这个方法
- 138:
- 139: public function decryptString(input:String):ByteArray {
- 140: if (!input || !input.length) {
- 141: return null;
- 142: }
- 143: var inputBytes:ByteArray = new ByteArray();
- 144: inputBytes.writeUTFBytes(input);
- 145: return decrypt(inputBytes);
- 146: }
- 147:
- 148: public function decryptString2Hex(input:String):String {
- 149: var result:ByteArray = decryptString(input);
- 150: return Hex.fromArray(result);
- 151: }
- 152:
- 153: public function decryptString2Base64(input:String):String {
- 154: var result:ByteArray = decryptString(input);
- 155: return Base64.encodeByteArray(result);
- 156: }
- 157:
- 158: public function set iv(value:ByteArray):void {
- 159: _iv = value;
- 160: }
- 161: public function get iv():ByteArray {
- 162: return _iv;
- 163: }
- 164: }
- 165: }
[AS3]as3加密算法AES加密算法
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...