|   1:  | <?php | 
|   2:  |  | 
|   3:  |  | 
|   4:  |  | 
|   5:  |  | 
|   6:  |  | 
|   7:  |  | 
|   8:  | namespace Aurora\System\Utils; | 
|   9:  |  | 
|  10:  |  | 
|  11:  |  | 
|  12:  |  | 
|  13:  |  | 
|  14:  |  | 
|  15:  | class Crypt | 
|  16:  | { | 
|  17:  |      | 
|  18:  |  | 
|  19:  |  | 
|  20:  |  | 
|  21:  |  | 
|  22:  |  | 
|  23:  |     public static function XxteaEncrypt($sString, $sKey) | 
|  24:  |     { | 
|  25:  |         if (empty($sString)) { | 
|  26:  |             return ''; | 
|  27:  |         } | 
|  28:  |  | 
|  29:  |         $aV = self::str2long($sString, true); | 
|  30:  |         $aK = self::str2long($sKey, false); | 
|  31:  |         if (count($aK) < 4) { | 
|  32:  |             for ($iIndex = count($aK); $iIndex < 4; $iIndex++) { | 
|  33:  |                 $aK[$iIndex] = 0; | 
|  34:  |             } | 
|  35:  |         } | 
|  36:  |         $iN = count($aV) - 1; | 
|  37:  |  | 
|  38:  |         $iZ = $aV[$iN]; | 
|  39:  |         $iY = $aV[0]; | 
|  40:  |         $iDelta = 0x9E3779B9; | 
|  41:  |         $iQ = floor(6 + 52 / ($iN + 1)); | 
|  42:  |         $iSum = 0; | 
|  43:  |         while (0 < $iQ--) { | 
|  44:  |             $iSum = self::int32($iSum + $iDelta); | 
|  45:  |             $iE = $iSum >> 2 & 3; | 
|  46:  |             for ($iPIndex = 0; $iPIndex < $iN; $iPIndex++) { | 
|  47:  |                 $iY = $aV[$iPIndex + 1]; | 
|  48:  |                 $iMx = self::int32((($iZ >> 5 & 0x07ffffff) ^ $iY << 2) + | 
|  49:  |                     (($iY >> 3 & 0x1fffffff) ^ $iZ << 4)) ^ self::int32(($iSum ^ $iY) + ($aK[$iPIndex & 3 ^ $iE] ^ $iZ)); | 
|  50:  |                 $iZ = $aV[$iPIndex] = self::int32($aV[$iPIndex] + $iMx); | 
|  51:  |             } | 
|  52:  |             $iY = $aV[0]; | 
|  53:  |             $iMx = self::int32((($iZ >> 5 & 0x07ffffff) ^ $iY << 2) + | 
|  54:  |                 (($iY >> 3 & 0x1fffffff) ^ $iZ << 4)) ^ self::int32(($iSum ^ $iY) + ($aK[$iPIndex & 3 ^ $iE] ^ $iZ)); | 
|  55:  |             $iZ = $aV[$iN] = self::int32($aV[$iN] + $iMx); | 
|  56:  |         } | 
|  57:  |  | 
|  58:  |         return self::long2str($aV, false); | 
|  59:  |     } | 
|  60:  |  | 
|  61:  |      | 
|  62:  |  | 
|  63:  |  | 
|  64:  |  | 
|  65:  |  | 
|  66:  |     public static function XxteaDecrypt($sEncriptedString, $sKey) | 
|  67:  |     { | 
|  68:  |         if (empty($sEncriptedString)) { | 
|  69:  |             return ''; | 
|  70:  |         } | 
|  71:  |  | 
|  72:  |         $aV = self::str2long($sEncriptedString, false); | 
|  73:  |         $aK = self::str2long($sKey, false); | 
|  74:  |  | 
|  75:  |         if (count($aK) < 4) { | 
|  76:  |             for ($iIndex = count($aK); $iIndex < 4; $iIndex++) { | 
|  77:  |                 $aK[$iIndex] = 0; | 
|  78:  |             } | 
|  79:  |         } | 
|  80:  |  | 
|  81:  |         $iN = count($aV) - 1; | 
|  82:  |  | 
|  83:  |         $iZ = $aV[$iN]; | 
|  84:  |         $iY = $aV[0]; | 
|  85:  |         $iDelta = 0x9E3779B9; | 
|  86:  |         $iQ = floor(6 + 52 / ($iN + 1)); | 
|  87:  |         $iSum = self::int32($iQ * $iDelta); | 
|  88:  |         while ($iSum != 0) { | 
|  89:  |             $iE = $iSum >> 2 & 3; | 
|  90:  |             for ($iPIndex = $iN; $iPIndex > 0; $iPIndex--) { | 
|  91:  |                 $iZ = $aV[$iPIndex - 1]; | 
|  92:  |                 $iMx = self::int32((($iZ >> 5 & 0x07ffffff) ^ $iY << 2) + | 
|  93:  |                     (($iY >> 3 & 0x1fffffff) ^ $iZ << 4)) ^ self::int32(($iSum ^ $iY) + ($aK[$iPIndex & 3 ^ $iE] ^ $iZ)); | 
|  94:  |                 $iY = $aV[$iPIndex] = self::int32($aV[$iPIndex] - $iMx); | 
|  95:  |             } | 
|  96:  |             $iZ = $aV[$iN]; | 
|  97:  |             $iMx = self::int32((($iZ >> 5 & 0x07ffffff) ^ $iY << 2) + | 
|  98:  |                 (($iY >> 3 & 0x1fffffff) ^ $iZ << 4)) ^ self::int32(($iSum ^ $iY) + ($aK[$iPIndex & 3 ^ $iE] ^ $iZ)); | 
|  99:  |             $iY = $aV[0] = self::int32($aV[0] - $iMx); | 
| 100:  |             $iSum = self::int32($iSum - $iDelta); | 
| 101:  |         } | 
| 102:  |  | 
| 103:  |         return self::long2str($aV, true); | 
| 104:  |     } | 
| 105:  |  | 
| 106:  |      | 
| 107:  |  | 
| 108:  |  | 
| 109:  |  | 
| 110:  |  | 
| 111:  |     private static function long2str($aV, $aW) | 
| 112:  |     { | 
| 113:  |         $iLen = count($aV); | 
| 114:  |         $iN = ($iLen - 1) << 2; | 
| 115:  |         if ($aW) { | 
| 116:  |             $iM = $aV[$iLen - 1]; | 
| 117:  |             if (($iM < $iN - 3) || ($iM > $iN)) { | 
| 118:  |                 return false; | 
| 119:  |             } | 
| 120:  |             $iN = $iM; | 
| 121:  |         } | 
| 122:  |         $aS = array(); | 
| 123:  |         for ($iIndex = 0; $iIndex < $iLen; $iIndex++) { | 
| 124:  |             $aS[$iIndex] = pack('V', $aV[$iIndex]); | 
| 125:  |         } | 
| 126:  |         if ($aW) { | 
| 127:  |             return substr(join('', $aS), 0, $iN); | 
| 128:  |         } else { | 
| 129:  |             return join('', $aS); | 
| 130:  |         } | 
| 131:  |     } | 
| 132:  |  | 
| 133:  |     private static function str2long($sS, $sW) | 
| 134:  |     { | 
| 135:  |         $aV = unpack('V*', $sS . str_repeat("\0", (4 - strlen($sS) % 4) & 3)); | 
| 136:  |         $aV = array_values($aV); | 
| 137:  |         if ($sW) { | 
| 138:  |             $aV[count($aV)] = strlen($sS); | 
| 139:  |         } | 
| 140:  |         return $aV; | 
| 141:  |     } | 
| 142:  |  | 
| 143:  |      | 
| 144:  |  | 
| 145:  |  | 
| 146:  |  | 
| 147:  |     private static function int32($iN) | 
| 148:  |     { | 
| 149:  |         while ($iN >= 2147483648) { | 
| 150:  |             $iN -= 4294967296; | 
| 151:  |         } | 
| 152:  |         while ($iN <= -2147483649) { | 
| 153:  |             $iN += 4294967296; | 
| 154:  |         } | 
| 155:  |         return (int) $iN; | 
| 156:  |     } | 
| 157:  | } | 
| 158:  |  |