InBlock.gif<?php
InBlock.gif /*
InBlock.gif * xtea加密算法
InBlock.gif */

InBlock.gif
class XxTea {
InBlock.gif
   /**
InBlock.gif    * 加密方法
InBlock.gif    *
InBlock.gif    * @param string $str    需要加密的内容
InBlock.gif    * @param string $key    密钥
InBlock.gif    * @param bool $toBase64  是否base64(最好true吧,比如cookie加密长度有限制的)
InBlock.gif    * return string
InBlock.gif    */

InBlock.gif   public function encrypt($str, $key, $toBase64= true) {
InBlock.gif     if ($str == "") {
InBlock.gif       return "";
InBlock.gif    }
InBlock.gif    $v = $ this->_str2long ( $str, true );
InBlock.gif    $k = $ this->_str2long ( $key, false );
InBlock.gif     if (count ( $k ) < 4) {
InBlock.gif       for($i = count ( $k ); $i < 4; $i ++) {
InBlock.gif        $k [$i] = 0;
InBlock.gif      }
InBlock.gif    }
InBlock.gif    $n = count ( $v ) - 1;
InBlock.gif    
InBlock.gif    $z = $v [$n];
InBlock.gif    $y = $v [0];
InBlock.gif    $delta = 0x9E3779B9;
InBlock.gif    $q = floor ( 6 + 52 / ($n + 1) );
InBlock.gif    $sum = 0;
InBlock.gif     while ( 0 < $q -- ) {
InBlock.gif      $sum = $ this->_int32 ( $sum + $delta );
InBlock.gif      $e = $sum >> 2 & 3;
InBlock.gif       for($p = 0; $p < $n; $p ++) {
InBlock.gif        $y = $v [$p + 1];
InBlock.gif        $mx = $ this->_int32 ( (($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4) ) ^ $ this->_int32 ( ($sum ^ $y) + ($k [$p & 3 ^ $e] ^ $z) );
InBlock.gif        $z = $v [$p] = $ this->_int32 ( $v [$p] + $mx );
InBlock.gif      }
InBlock.gif      $y = $v [0];
InBlock.gif      $mx = $ this->_int32 ( (($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4) ) ^ $ this->_int32 ( ($sum ^ $y) + ($k [$p & 3 ^ $e] ^ $z) );
InBlock.gif      $z = $v [$n] = $ this->_int32 ( $v [$n] + $mx );
InBlock.gif    }
InBlock.gif     if ($toBase64) {
InBlock.gif       return base64_encode($ this->_long2str ( $v, false ));
InBlock.gif    }
InBlock.gif     return $ this->_long2str ( $v, false );
InBlock.gif  }
InBlock.gif
   /**
InBlock.gif    * 解密方法
InBlock.gif    *
InBlock.gif    * @param string $str    加密后的内容
InBlock.gif    * @param string $key    密钥
InBlock.gif    * @param bool $toBase64  
InBlock.gif    * return string
InBlock.gif    */

InBlock.gif   public function decrypt($str, $key, $toBase64= true) {
InBlock.gif     if ($str == "") {
InBlock.gif       return "";
InBlock.gif    }
InBlock.gif    $toBase64 && $str = base64_decode($str);
InBlock.gif    $v = $ this->_str2long ( $str, false );
InBlock.gif    $k = $ this->_str2long ( $key, false );
InBlock.gif     if (count ( $k ) < 4) {
InBlock.gif       for($i = count ( $k ); $i < 4; $i ++) {
InBlock.gif        $k [$i] = 0;
InBlock.gif      }
InBlock.gif    }
InBlock.gif    $n = count ( $v ) - 1;
InBlock.gif    
InBlock.gif    $z = $v [$n];
InBlock.gif    $y = $v [0];
InBlock.gif    $delta = 0x9E3779B9;
InBlock.gif    $q = floor ( 6 + 52 / ($n + 1) );
InBlock.gif    $sum = $ this->_int32 ( $q * $delta );
InBlock.gif     while ( $sum != 0 ) {
InBlock.gif      $e = $sum >> 2 & 3;
InBlock.gif       for($p = $n; $p > 0; $p --) {
InBlock.gif        $z = $v [$p - 1];
InBlock.gif        $mx = $ this->_int32 ( (($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4) ) ^ $ this->_int32 ( ($sum ^ $y) + ($k [$p & 3 ^ $e] ^ $z) );
InBlock.gif        $y = $v [$p] = $ this->_int32 ( $v [$p] - $mx );
InBlock.gif      }
InBlock.gif      $z = $v [$n];
InBlock.gif      $mx = $ this->_int32 ( (($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4) ) ^ $ this->_int32 ( ($sum ^ $y) + ($k [$p & 3 ^ $e] ^ $z) );
InBlock.gif      $y = $v [0] = $ this->_int32 ( $v [0] - $mx );
InBlock.gif      $sum = $ this->_int32 ( $sum - $delta );
InBlock.gif    }
InBlock.gif     return $ this->_long2str ( $v, true );
InBlock.gif  }
InBlock.gif
   /**
InBlock.gif    * 长整型转为字符串
InBlock.gif    *
InBlock.gif    * @param long $v
InBlock.gif    * @param boolean $w
InBlock.gif    * @return string
InBlock.gif    */

InBlock.gif   private function _long2str($v, $w) {
InBlock.gif    $len = count ( $v );
InBlock.gif    $n = ($len - 1) << 2;
InBlock.gif     if ($w) {
InBlock.gif      $m = $v [$len - 1];
InBlock.gif       if (($m < $n - 3) || ($m > $n))
InBlock.gif         return false;
InBlock.gif      $n = $m;
InBlock.gif    }
InBlock.gif    $s = array ();
InBlock.gif     for($i = 0; $i < $len; $i ++) {
InBlock.gif      $s [$i] = pack ( "V", $v [$i] );
InBlock.gif    }
InBlock.gif     if ($w) {
InBlock.gif       return substr ( join ( '', $s ), 0, $n );
InBlock.gif    } else {
InBlock.gif       return join ( '', $s );
InBlock.gif    }
InBlock.gif  }
InBlock.gif
   /**
InBlock.gif    * 字符串转为长整型
InBlock.gif    *
InBlock.gif    * @param string $s
InBlock.gif    * @param boolean $w
InBlock.gif    * @return Ambigous <multitype:, number>
InBlock.gif    */

InBlock.gif   private function _str2long($s, $w) {
InBlock.gif    $v = unpack ( "V*", $s . str_repeat ( "\0", (4 - strlen ( $s ) % 4) & 3 ) );
InBlock.gif    $v = array_values ( $v );
InBlock.gif     if ($w) {
InBlock.gif      $v [count ( $v )] = strlen ( $s );
InBlock.gif    }
InBlock.gif     return $v;
InBlock.gif  }
InBlock.gif  
InBlock.gif   private function _int32($n) {
InBlock.gif     while ( $n >= 2147483648 )
InBlock.gif      $n -= 4294967296;
InBlock.gif     while ( $n <= - 2147483649 )
InBlock.gif      $n += 4294967296;
InBlock.gif     return ( int ) $n;
InBlock.gif  }
InBlock.gif}
InBlock.gif
// 使用方式
InBlock.gif$xxtea = new XxTea();
InBlock.gif$ string = 'hello leven';
InBlock.gif$key = '123456';
InBlock.gif$encode = $xxtea->encrypt($ string,$key, true);
InBlock.gif$decode = $xxtea->decrypt($encode,$key, true);
InBlock.gifecho $encode;
InBlock.gifecho "<br />";
InBlock.gifecho $decode;