xxtea.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. class xxtea {
  3. function __construct(){
  4. }
  5. function __destruct(){
  6. }
  7. public function encrypt($s,$key){
  8. return str_replace(array('+','/','='), array('-','_','.'), base64_encode($this->xxtea_encrypt($s, $key)));
  9. }
  10. public function decrypt($e,$key){
  11. $c = str_replace(array('-','_','.'), array('+','/','='), $e);
  12. return $this->xxtea_decrypt(base64_decode($c), $key);
  13. }
  14. private function long2str($v, $w) {
  15. $len = count($v);
  16. $n = ($len - 1) << 2;
  17. if ($w) {
  18. $m = $v[$len - 1];
  19. if (($m < $n - 3) || ($m > $n)) return false;
  20. $n = $m;
  21. }
  22. $s = array();
  23. for ($i = 0; $i < $len; $i++) {
  24. $s[$i] = pack("V", $v[$i]);
  25. }
  26. if ($w) {
  27. return substr(join('', $s), 0, $n);
  28. } else {
  29. return join('', $s);
  30. }
  31. }
  32. private function str2long($s, $w) {
  33. $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
  34. $v = array_values($v);
  35. if ($w) {
  36. $v[count($v)] = strlen($s);
  37. }
  38. return $v;
  39. }
  40. private function int32($n) {
  41. while ($n >= 2147483648) $n -= 4294967296;
  42. while ($n <= -2147483649) $n += 4294967296;
  43. return (int)$n;
  44. }
  45. private function xxtea_encrypt($str, $key) {
  46. if ($str == "") {
  47. return "";
  48. }
  49. $v = $this->str2long($str, true);
  50. $k = $this->str2long($key, false);
  51. if (count($k) < 4) {
  52. for ($i = count($k); $i < 4; $i++) {
  53. $k[$i] = 0;
  54. }
  55. }
  56. $n = count($v) - 1;
  57. $z = $v[$n];
  58. $y = $v[0];
  59. $delta = 0x9E3779B9;
  60. $q = floor(6 + 52 / ($n + 1));
  61. $sum = 0;
  62. while (0 < $q--) {
  63. $sum = $this->int32($sum + $delta);
  64. $e = $sum >> 2 & 3;
  65. for ($p = 0; $p < $n; $p++) {
  66. $y = $v[$p + 1];
  67. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  68. $z = $v[$p] = $this->int32($v[$p] + $mx);
  69. }
  70. $y = $v[0];
  71. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  72. $z = $v[$n] = $this->int32($v[$n] + $mx);
  73. }
  74. return $this->long2str($v, false);
  75. }
  76. private function xxtea_decrypt($str, $key) {
  77. if ($str == "") {
  78. return "";
  79. }
  80. $v = $this->str2long($str, false);
  81. $k = $this->str2long($key, false);
  82. if (count($k) < 4) {
  83. for ($i = count($k); $i < 4; $i++) {
  84. $k[$i] = 0;
  85. }
  86. }
  87. $n = count($v) - 1;
  88. $z = $v[$n];
  89. $y = $v[0];
  90. $delta = 0x9E3779B9;
  91. $q = floor(6 + 52 / ($n + 1));
  92. $sum = $this->int32($q * $delta);
  93. while ($sum != 0) {
  94. $e = $sum >> 2 & 3;
  95. for ($p = $n; $p > 0; $p--) {
  96. $z = $v[$p - 1];
  97. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  98. $y = $v[$p] = $this->int32($v[$p] - $mx);
  99. }
  100. $z = $v[$n];
  101. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  102. $y = $v[0] = $this->int32($v[0] - $mx);
  103. $sum = $this->int32($sum - $delta);
  104. }
  105. return $this->long2str($v, true);
  106. }
  107. }
  108. ?>