Xxtea.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Crypt\Driver;
  12. /**
  13. * Xxtea 加密实现类
  14. */
  15. class Xxtea {
  16. /**
  17. * 加密字符串
  18. * @param string $str 字符串
  19. * @param string $key 加密key
  20. * @param integer $expire 有效期(秒)
  21. * @return string
  22. */
  23. public static function encrypt($str, $key,$expire=0) {
  24. $expire = sprintf('%010d', $expire ? $expire + time():0);
  25. $str = $expire.$str;
  26. $v = self::str2long($str, true);
  27. $k = self::str2long($key, false);
  28. $n = count($v) - 1;
  29. $z = $v[$n];
  30. $y = $v[0];
  31. $delta = 0x9E3779B9;
  32. $q = floor(6 + 52 / ($n + 1));
  33. $sum = 0;
  34. while (0 < $q--) {
  35. $sum = self::int32($sum + $delta);
  36. $e = $sum >> 2 & 3;
  37. for ($p = 0; $p < $n; $p++) {
  38. $y = $v[$p + 1];
  39. $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  40. $z = $v[$p] = self::int32($v[$p] + $mx);
  41. }
  42. $y = $v[0];
  43. $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  44. $z = $v[$n] = self::int32($v[$n] + $mx);
  45. }
  46. return self::long2str($v, false);
  47. }
  48. /**
  49. * 解密字符串
  50. * @param string $str 字符串
  51. * @param string $key 加密key
  52. * @return string
  53. */
  54. public static function decrypt($str, $key) {
  55. $v = self::str2long($str, false);
  56. $k = self::str2long($key, false);
  57. $n = count($v) - 1;
  58. $z = $v[$n];
  59. $y = $v[0];
  60. $delta = 0x9E3779B9;
  61. $q = floor(6 + 52 / ($n + 1));
  62. $sum = self::int32($q * $delta);
  63. while ($sum != 0) {
  64. $e = $sum >> 2 & 3;
  65. for ($p = $n; $p > 0; $p--) {
  66. $z = $v[$p - 1];
  67. $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  68. $y = $v[$p] = self::int32($v[$p] - $mx);
  69. }
  70. $z = $v[$n];
  71. $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  72. $y = $v[0] = self::int32($v[0] - $mx);
  73. $sum = self::int32($sum - $delta);
  74. }
  75. $data = self::long2str($v, true);
  76. $expire = substr($data,0,10);
  77. if($expire > 0 && $expire < time()) {
  78. return '';
  79. }
  80. $data = substr($data,10);
  81. return $data;
  82. }
  83. private static function long2str($v, $w) {
  84. $len = count($v);
  85. $s = array();
  86. for ($i = 0; $i < $len; $i++) {
  87. $s[$i] = pack("V", $v[$i]);
  88. }
  89. if ($w) {
  90. return substr(join('', $s), 0, $v[$len - 1]);
  91. }else{
  92. return join('', $s);
  93. }
  94. }
  95. private static function str2long($s, $w) {
  96. $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
  97. $v = array_values($v);
  98. if ($w) {
  99. $v[count($v)] = strlen($s);
  100. }
  101. return $v;
  102. }
  103. private static function int32($n) {
  104. while ($n >= 2147483648) $n -= 4294967296;
  105. while ($n <= -2147483649) $n += 4294967296;
  106. return (int)$n;
  107. }
  108. }