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

[AS3]AS3的加密算法(TEA)

时间:2012-09-04 13:489ria.com
[AS3]AS3的加密算法(TEA) ,微型加密算法,微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。

  微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。   
  TEA 算法最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计的。该算法使用 128 位的密钥为 64 位的信息块进行加密,它需要进行 64 轮迭代,尽管作者认为 32 轮已经足够了。该算法使用了一个神秘常数δ作为倍数,它来源于黄金比率,以保证每一轮加密都不相同。但δ的精确值似乎并不重要,这里 TEA 把它定义为 δ=「(√5 - 1)231」(也就是程序中的 0×9E3779B9)。   
  之后 TEA 算法被发现存在缺陷,作为回应,设计者提出了一个 TEA 的升级版本——XTEA(有时也被称为“tean”)。XTEA 跟 TEA 使用了相同的简单运算,但它采用了截然不同的顺序,为了阻止密钥表攻击,四个子密钥(在加密过程中,原 128 位的密钥被拆分为 4 个 32 位的子密钥)采用了一种不太正规的方式进行混合,但速度更慢了。
   在AS开发过程中,AS经常会和其他语言通信,通信过程中也会经常涉及到数据加密的问题,所以有必要改写一个AS3版本的TEA加密算法。

  1. /* TEA 对称性加密/解密函数    
  2. * @author cheche 2010-8  
  3. */  
  4.  
  5. package {  
  6.         import flash.utils.ByteArray;  
  7.         import flash.utils.Endian;  
  8.  
  9.         public class TeaEncrypt {  
  10.                   
  11.                 private static var round:uint=32;  
  12.                 private static var key:String="3A DA 75 21 DB E3 DB B3 62 B7 49 01 A5 C6 EA D5";  
  13.  
  14.                 public static function teaEncrypt(enData:ByteArray):ByteArray {  
  15.                         enData.position=0;  
  16.                           
  17.                         var len:uint=Math.ceil(enData.length/8);  
  18.                           
  19.                         var keyArr:Array=getKey(key);  
  20.  
  21.                         var keyBytes:ByteArray = new ByteArray();  
  22.                         keyBytes.position;  
  23.                         keyBytes.endian=Endian.LITTLE_ENDIAN;  
  24.                           
  25.                         for (i=0; i<keyArr.length; i++) {  
  26.                                 keyBytes.writeByte(keyArr[i]);  
  27.                         }  
  28.                           
  29.                         var resData:ByteArray = new ByteArray();  
  30.                         resData.position=0;  
  31.                         resData.endian=Endian.LITTLE_ENDIAN;  
  32.                           
  33.                         var nData:ByteArray = new ByteArray();  
  34.                                 nData.position=0;  
  35.                                 nData.endian=Endian.LITTLE_ENDIAN;  
  36.                           
  37.                           
  38.                         enData.position=0;  
  39.                         for (var i:uint=0; i<len; i++) {  
  40.                                 var enBytes:ByteArray = new ByteArray();  
  41.                                 enBytes.position=0;  
  42.                                 enBytes.endian=Endian.LITTLE_ENDIAN;  
  43.  
  44.                                 if (enData.bytesAvailable>=8) {  
  45.                                         enData.readBytes(enBytes,0,8);  
  46.                                         var encrypted:ByteArray =  new ByteArray();  
  47.                                         encrypted.position=0;  
  48.                                         encrypted.endian=Endian.LITTLE_ENDIAN;  
  49.                                           
  50.                                         encryptencrypted=encrypt(enBytes,keyBytes);  
  51.                                           
  52.                                         resData.writeBytes(encrypted,0,encrypted.length);  
  53.                                                                     encrypted.clear();  
  54.                                     encrypted=null;  
  55.                                 } else {  
  56.                                           
  57.                                         enData.readBytes(nData,0,enData.bytesAvailable);  
  58.                                 }                                  
  59.                                                                    enBytes.clear();  
  60.                         }  
  61.                                                           
  62.                         nData.position=0;  
  63.                         if (nData.length>0) {  
  64.                                   
  65.                                 resData.writeBytes(nData,0,nData.length);  
  66.                         }  
  67.                         keyBytes.clear();  
  68.                           
  69.                         return resData;  
  70.                                                 enData.clear();  
  71.                 }  
  72.                   
  73.                 public static function teaDecrypt(decData:ByteArray):ByteArray {  
  74.                         decData.position=0;  
  75.                         var len:uint=Math.ceil(decData.length/8);  
  76.  
  77.                         var keyArr:Array=getKey(key);  
  78.  
  79.                         var keyBytes:ByteArray = new ByteArray();  
  80.                         keyBytes.position;  
  81.                         keyBytes.endian=Endian.LITTLE_ENDIAN;  
  82.                         //将密钥写入缓冲区  
  83.                         for (i=0; i<keyArr.length; i++) {  
  84.                                 keyBytes.writeByte(keyArr[i]);  
  85.                         }  
  86.  
  87.                         var resData:ByteArray = new ByteArray();  
  88.                         resData.position=0;  
  89.                         resData.endian=Endian.LITTLE_ENDIAN;  
  90.                         decData.position=0;  
  91.                         for (var i:uint=0; i<len; i++) {  
  92.                                 var decBytes:ByteArray = new ByteArray();  
  93.                                 decBytes.position=0;  
  94.                                 decBytes.endian=Endian.LITTLE_ENDIAN;  
  95.                                 if (decData.bytesAvailable>=8) {  
  96.                                         decData.readBytes(decBytes,0,8);  
  97.                                     var decrypted:ByteArray =  new ByteArray();  
  98.                                     decrypted.position=0;  
  99.                                     decrypted.endian=Endian.LITTLE_ENDIAN;  
  100.                                   
  101.                                     decryptdecrypted=decrypt(decBytes,keyBytes);  
  102.                                   
  103.                                     resData.writeBytes(decrypted,0,decrypted.length);  
  104.                                     decrypted.clear();  
  105.                                 } else {  
  106.  
  107.                                         var nData:ByteArray = new ByteArray();  
  108.                                         nData.position=0;  
  109.                                         nData.endian=Endian.LITTLE_ENDIAN;  
  110.                                         decData.readBytes(nData,0,decData.bytesAvailable);  
  111.                                 }  
  112.                         }  
  113.                         keyBytes.clear();  
  114.                         decData.clear();  
  115.                         trace("解密后数据>>>>"+ByteArrayToLongArray(resData,false));  
  116.                         if (nData.length>0) {  
  117.                                 nData.readBytes(resData,0,nData.length);  
  118.                         }  
  119.                         trace("---->"+nData.length);  
  120.                         return resData;  
  121.                         resData.clear();  
  122.                 }  
  123.  
  124.  
  125.                 private static function LongArrayToByteArray(data : Array, includeLength : Boolean):ByteArray {  
  126.                         var length:uint=data.length;  
  127.  
  128.                         var n : uint = (length - 1) << 2;  
  129.                         if (includeLength) {  
  130.                                 var m:uint=data[length-1];  
  131.                                 if ((m < n - 3) || (m > n)) {  
  132.                                         return null;  
  133.                                 }  
  134.                                 n=m;  
  135.                         }  
  136.  
  137.                         var result : ByteArray = new ByteArray();  
  138.                         result.endian=Endian.LITTLE_ENDIAN;  
  139.                         for (var i : uint = 0; i < length; i++) {  
  140.                                 result.writeUnsignedInt(data[i]);  
  141.                         }  
  142.                         if (includeLength) {  
  143.  
  144.                                 result.length=n;  
  145.                                 return result;  
  146.                         } else {  
  147.                                 return result;  
  148.                         }  
  149.                 }  
  150.  
  151.                 private static function ByteArrayToLongArray(data : ByteArray, includeLength : Boolean):Array {  
  152.                         var length:uint=data.length;  
  153.                         var n:uint=length>>2;  
  154.                         if (length%4>0) {  
  155.                                 n++;  
  156.                                 data.length += (4 - (length % 4));  
  157.                         }  
  158.                         data.endian=Endian.LITTLE_ENDIAN;  
  159.                         data.position=0;  
  160.                         var result:Array=[];  
  161.                         for (var i : uint = 0; i < n; i++) {  
  162.                                 result[i]=data.readUnsignedInt();  
  163.                         }  
  164.                         if (includeLength) {  
  165.                                 result[n]=length;  
  166.                         }  
  167.                         data.length=length;  
  168.                         return result;  
  169.                 }  
  170.  
  171.                 private static function encrypt(data:ByteArray, key:ByteArray):ByteArray {  
  172.  
  173.                         if (data.length==0) {  
  174.                                 return new ByteArray();  
  175.                         }  
  176.                         var v:Array=ByteArrayToLongArray(data,false);  
  177.                         var k:Array=[987395361,3689143219,1656178945,2781276885];  
  178.                         if (k.length<4) {  
  179.                                 k.length=4;  
  180.                         }  
  181.                         var n:uint=v.length-1;  
  182.                         var y:uint=v[0];  
  183.                         var z:uint=v[1];  
  184.  
  185.                         var a:uint=k[0];  
  186.                         var b:uint=k[1];  
  187.                         var c:uint=k[2];  
  188.                         var d:uint=k[3];  
  189.                         var delta=0x9E3779B9;/* (sqrt(5)-1)/2*2^32 */  
  190.                         var sum:uint=0;  
  191.  
  192.                         for (var i:uint = 0; i < round; i++) {/* basic cycle start */  
  193.                                 sum+=delta;  
  194.                                 y += ((z<<4) + a) ^ (z + sum) ^ ((z>>>5) + b);  
  195.                                 z += ((y<<4) + c) ^ (y + sum) ^ ((y>>>5) + d);/* end cycle */  
  196.                         }  
  197.                         v[0]=y;  
  198.                         v[1]=z;  
  199.  
  200.                         return LongArrayToByteArray(v,false);  
  201.                 }  
  202.  
  203.                 private static function decrypt(data : ByteArray, key : ByteArray):ByteArray {  
  204.                         if (data.length==0) {  
  205.                                 return new ByteArray();  
  206.                         }  
  207.                         var v:Array=ByteArrayToLongArray(data,false);  
  208.                         var k:Array=[987395361,3689143219,1656178945,2781276885];//密钥写死  
  209.                         if (k.length<4) {  
  210.                                 k.length=4;  
  211.                         }  
  212.                         var n:uint=v.length-1;  
  213.                         var y:uint=v[0];  
  214.                         var z:uint=v[1];  
  215.  
  216.                         var a:uint=k[0];  
  217.                         var b:uint=k[1];  
  218.                         var c:uint=k[2];  
  219.                         var d:uint=k[3];  
  220.                         var delta=0x9E3779B9;/* (sqrt(5)-1)/2*2^32 */  
  221.                         var sum:uint=0xE3779B90;  
  222.  
  223.                         if (round==32) {  
  224.                                 sum=0xC6EF3720;//delta << 5 
  225.                         } else if (round == 16) {  
  226.                                 sum=0xE3779B90;  
  227.                         }//delta << 4 
  228.                   
  229.                 for (var i:uint = 0; i < round; i++) {/* basic cycle start */  
  230.                         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >>>5) + d);  
  231.                         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >>>5) + b);  
  232.                         sum-=delta;  
  233.                 }  
  234.                 v[0]=y;  
  235.                 v[1]=z;  
  236.                 return LongArrayToByteArray(v, false);  
  237.         }  
  238.         private static function getKey(keyStr:String):Array {  
  239.                 var key:String=keyStr;  
  240.                 var arr:Array = new Array();  
  241.                 var res:Array = new Array();  
  242.                 var target:Array = new Array();  
  243.                 arr=key.split(" ");  
  244.                 var curStr:String="";  
  245.                 var len:uint=arr.length;  
  246.                 for (var i:uint=0; i<arr.length; i++) {  
  247.                         var newnewArr:Array = new Array();  
  248.                         curStr=arr[i];  
  249.  
  250.                         for (var j:uint=0; j<2; j++) {  
  251.                                 var char:String=curStr.charAt(j);  
  252.                                 newArr.push(char);  
  253.                         }  
  254.                         target.push(newArr);  
  255.                 }  
  256.                 for (i = 0; i < target.length; i++) {  
  257.                         var a:int=isNumber(target[i][0]);  
  258.                         var b:int=isNumber(target[i][1]);  
  259.                         var resNum:int=a*16+b;  
  260.                         res.push(resNum);  
  261.                 }  
  262.                 return res;  
  263.         }  
  264.         private static function isNumber(char:String):int {  
  265.                 var arr:Array=["0","1","2","3","4","5","6","7","8","9"];  
  266.                 for (var i:uint=0; i<arr.length; i++) {  
  267.                         if (char==arr[i]) {  
  268.                                 return int(char);  
  269.                                 break;  
  270.                         }  
  271.                 }  
  272.                 var id:int=0;  
  273.                 switch (char) {  
  274.                         case "A" :  
  275.                                 id=10;  
  276.                                 break;  
  277.                         case "B" :  
  278.                                 id=11;  
  279.                                 break;  
  280.                         case "C" :  
  281.                                 id=12;  
  282.                                 break;  
  283.                         case "D" :  
  284.                                 id=13;  
  285.                                 break;  
  286.                         case "E" :  
  287.                                 id=14;  
  288.                                 break;  
  289.                         case "F" :  
  290.                                 id=15;  
  291.                                 break;  
  292.                         default :  
  293.                                 break;  
  294.                 }  
  295.                 return id;  
  296.         }  
  297.         private static function intToHexChar(index:int):String {  
  298.                 var hex:Array=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];  
  299.                 return hex[index];  
  300.         }  
  301.         private static function intToHexString(arr:Array):String {  
  302.                 var myStr:String="";  
  303.                 for (var i:uint=0; i<arr.length; ++i) {  
  304.                         var t:uint=arr[i];  
  305.                         var a:uint=Math.floor(t/16);  
  306.                         var b:uint=Math.floor(t%16);  
  307.                         myStr+=intToHexChar(a);  
  308.                         myStr+=intToHexChar(b);  
  309.                         myStr+=" ";  
  310.                 }  
  311.                 return myStr;  
  312.         }  
  313.    } 

 

热门文章推荐

请稍候...

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

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