·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]AS3基于flash做文件地址的加密和解密Des源代码范例

时间:2017-04-21 18:37csdn.net
[AS3]AS3基于flash做文件地址的加密和解密Des源代码范例

[AS3]AS3基于flash做文件地址的加密和解密Des源代码范例

使用as3加解密的开源项目as3crypto,下载地址:
http://download.csdn.net/detail/lang10217/7519497

官方下载地址:
http://code.google.com/p/as3crypto/

示例代码:

  1. package   
  2. {   
  3.     import com.hurlant.crypto.symmetric.DESKey;   
  4.     import com.hurlant.crypto.symmetric.ECBMode;   
  5.     import com.hurlant.util.Base64;   
  6.        
  7.     import flash.display.Sprite;   
  8.     import flash.utils.ByteArray;   
  9.        
  10.     /**  
  11.      * Author:大飞狼  
  12.      * Date: 2014-6-19 上午9:30:12  
  13.      * Synopsis:des加解密测试  
  14.      */   
  15.     public class DesTest extends Sprite   
  16.     {   
  17.         /**  
  18.          * key顾名思义就是加解密用的钥匙  
  19.          */        
  20.         private const KEY_STR:String="123123";   
  21.            
  22.         public function DesTest()   
  23.         {   
  24.             var b_ba:ByteArray=encrypt(KEY_STR,"www.dafeilang.com");   
  25.             //字符串形式的密文   
  26.             var b_ciphertext:String=Base64.encodeByteArray(b_ba);   
  27.             trace("密文:",b_ciphertext);   
  28.                
  29.             var b_ciphertext16:String=byteArrayTo16(b_ba);   
  30.             trace("十六进制形式密文:",b_ciphertext16);   
  31.                
  32.             //解密后的原文   
  33.             var b_originalText :String=decrypt(KEY_STR,Base64.decodeToByteArray(b_ciphertext));   
  34.             trace("解密得到:",b_originalText);   
  35.                
  36.             //解密16进制密文后的原文   
  37.             var b_originalText16 :String=decrypt(KEY_STR,from16ToByteArray(b_ciphertext16));   
  38.             trace("解密十六进制密文得到:",b_originalText16);   
  39.         }   
  40.         /**  
  41.          * 加密  
  42.          * @param key 加解密用的key  
  43.          * @param data 被加密的字符串  
  44.          * @return 加密后生成ByteArray数据  
  45.          */        
  46.         private function encrypt(key:String,data:String):ByteArray{   
  47.             trace("执行加密方法,key:",key,",被加密的字符串:",data);   
  48.                
  49.             //实验化key的Bytearray对象,给DESKey使用   
  50.             var b_keyByteArray:ByteArray=new ByteArray();    
  51.             b_keyByteArray.writeUTFBytes(key);   
  52.                
  53.             //实例化DESKey   
  54.             var b_desKey:DESKey=new DESKey(b_keyByteArray);   
  55.                
  56.             //不只是有ecb还有cbc,cfb等,有兴趣可以自己尝试   
  57.             var b_ecb:ECBMode=new ECBMode(b_desKey);   
  58.             //把需要加密字符串转换为ByteArray   
  59.             var b_byteArray:ByteArray=convertStringToByteArray(data);   
  60.                
  61.             //执行加密   
  62.             b_ecb.encrypt(b_byteArray);   
  63.                
  64.             return b_byteArray;   
  65.         }   
  66.         /**  
  67.          * 解密  
  68.          * @param key 解密用的key  
  69.          * @param data 需要解密的数据  
  70.          * @return 解密后的数据  
  71.          */        
  72.         private function decrypt(key:String,data:ByteArray):String{   
  73.             trace("\n执行解密方法,key:",key,",需要解密的字符串:",data);   
  74.                
  75.             //实验化key的Bytearray对象,给DESKey使用   
  76.             var b_keyByteArray:ByteArray=new ByteArray();    
  77.             b_keyByteArray.writeUTFBytes(key);   
  78.                
  79.             //实例化DESKey   
  80.             var b_desKey:DESKey=new DESKey(b_keyByteArray);   
  81.                
  82.             //   
  83.             var b_ecb:ECBMode=new ECBMode(b_desKey);   
  84.                
  85.             var b_byteArray:ByteArray=new ByteArray();   
  86.             b_byteArray.writeBytes(data);   
  87.                
  88.             //执行解密   
  89.             b_ecb.decrypt(b_byteArray);   
  90.                
  91.             return convertByteArrayToString(b_byteArray);   
  92.         }   
  93.            
  94.         /**  
  95.          * String转ByteArray   
  96.          * @param str  
  97.          * @return   
  98.          */        
  99.         public function convertStringToByteArray(str:String):ByteArray    
  100.         {    
  101.             var bytes:ByteArray;    
  102.             if (str)    
  103.             {    
  104.                 bytes=new ByteArray();    
  105.                 bytes.writeUTFBytes(str);    
  106.             }    
  107.             return bytes;    
  108.         }    
  109.         /**  
  110.          * ByteArray转String  
  111.          * @param bytes  
  112.          * @return   
  113.          */        
  114.         public function convertByteArrayToString(bytes:ByteArray):String    
  115.         {    
  116.             var str:String;    
  117.             if (bytes)    
  118.             {    
  119.                 bytes.position=0;    
  120.                 str=bytes.readUTFBytes(bytes.length);    
  121.             }    
  122.             return str;    
  123.         }   
  124.         /**  
  125.          * 把ByteArray转换为16进制的形式的字符串  
  126.          * @param ba  
  127.          * @param name  
  128.          * @return   
  129.          */        
  130.         private function byteArrayTo16(ba:ByteArray):String{   
  131.             ba.position=0;   
  132.             var b_str:String="";   
  133.             while (ba.bytesAvailable > 0) {   
  134.                 var b_s:String=ba.readUnsignedByte().toString(16);   
  135. //              trace("b_s:",b_s);   
  136.                 if(b_s.length<2b_s="0"+b_s;   
  137.                 b_str+=b_s;   
  138.             }   
  139.             return b_str;   
  140.         }   
  141.         private function from16ToByteArray(str:String):ByteArray{   
  142.             var b_ba:ByteArray=new ByteArray();   
  143.             var b_arr:Array=str.split("");   
  144.             var b_length:int=b_arr.length;   
  145.             for(var i:int=0;i<b_length-1;i+=2){   
  146.                 var b_char1:String=b_arr[i];   
  147.                 var b_char2:String=b_arr[i+1];   
  148.                 var b_byte:int=parseInt(b_char1+b_char2,16);   
  149. //              trace("b_byte:",b_byte);   
  150.                 b_ba.writeByte(b_byte);   
  151.             }   
  152.             return b_ba;   
  153.         }   
  154.            
  155.     }   
  156. }   

来源:http://blog.csdn.net/lang10217/article/details/32705667

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器