xxtea.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | The implementation of PHPRPC Protocol 3.0 |
  5. | |
  6. | xxtea.php |
  7. | |
  8. | Release 3.0.1 |
  9. | Copyright by Team-PHPRPC |
  10. | |
  11. | WebSite: http://www.phprpc.org/ |
  12. | http://www.phprpc.net/ |
  13. | http://www.phprpc.com/ |
  14. | http://sourceforge.net/projects/php-rpc/ |
  15. | |
  16. | Authors: Ma Bingyao <andot@ujn.edu.cn> |
  17. | |
  18. | This file may be distributed and/or modified under the |
  19. | terms of the GNU General Public License (GPL) version |
  20. | 2.0 as published by the Free Software Foundation and |
  21. | appearing in the included file LICENSE. |
  22. | |
  23. \**********************************************************/
  24. /* XXTEA encryption arithmetic library.
  25. *
  26. * Copyright: Ma Bingyao <andot@ujn.edu.cn>
  27. * Version: 1.6
  28. * LastModified: Apr 12, 2010
  29. * This library is free. You can redistribute it and/or modify it under GPL.
  30. */
  31. if (!extension_loaded('xxtea')) {
  32. function long2str($v, $w) {
  33. $len = count($v);
  34. $n = ($len - 1) << 2;
  35. if ($w) {
  36. $m = $v[$len - 1];
  37. if (($m < $n - 3) || ($m > $n)) return false;
  38. $n = $m;
  39. }
  40. $s = array();
  41. for ($i = 0; $i < $len; $i++) {
  42. $s[$i] = pack("V", $v[$i]);
  43. }
  44. if ($w) {
  45. return substr(join('', $s), 0, $n);
  46. }
  47. else {
  48. return join('', $s);
  49. }
  50. }
  51. function str2long($s, $w) {
  52. $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
  53. $v = array_values($v);
  54. if ($w) {
  55. $v[count($v)] = strlen($s);
  56. }
  57. return $v;
  58. }
  59. function int32($n) {
  60. while ($n >= 2147483648) $n -= 4294967296;
  61. while ($n <= -2147483649) $n += 4294967296;
  62. return (int)$n;
  63. }
  64. function xxtea_encrypt($str, $key) {
  65. if ($str == "") {
  66. return "";
  67. }
  68. $v = str2long($str, true);
  69. $k = str2long($key, false);
  70. if (count($k) < 4) {
  71. for ($i = count($k); $i < 4; $i++) {
  72. $k[$i] = 0;
  73. }
  74. }
  75. $n = count($v) - 1;
  76. $z = $v[$n];
  77. $y = $v[0];
  78. $delta = 0x9E3779B9;
  79. $q = floor(6 + 52 / ($n + 1));
  80. $sum = 0;
  81. while (0 < $q--) {
  82. $sum = int32($sum + $delta);
  83. $e = $sum >> 2 & 3;
  84. for ($p = 0; $p < $n; $p++) {
  85. $y = $v[$p + 1];
  86. $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  87. $z = $v[$p] = int32($v[$p] + $mx);
  88. }
  89. $y = $v[0];
  90. $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  91. $z = $v[$n] = int32($v[$n] + $mx);
  92. }
  93. return long2str($v, false);
  94. }
  95. function xxtea_decrypt($str, $key) {
  96. if ($str == "") {
  97. return "";
  98. }
  99. $v = str2long($str, false);
  100. $k = str2long($key, false);
  101. if (count($k) < 4) {
  102. for ($i = count($k); $i < 4; $i++) {
  103. $k[$i] = 0;
  104. }
  105. }
  106. $n = count($v) - 1;
  107. $z = $v[$n];
  108. $y = $v[0];
  109. $delta = 0x9E3779B9;
  110. $q = floor(6 + 52 / ($n + 1));
  111. $sum = int32($q * $delta);
  112. while ($sum != 0) {
  113. $e = $sum >> 2 & 3;
  114. for ($p = $n; $p > 0; $p--) {
  115. $z = $v[$p - 1];
  116. $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  117. $y = $v[$p] = int32($v[$p] - $mx);
  118. }
  119. $z = $v[$n];
  120. $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  121. $y = $v[0] = int32($v[0] - $mx);
  122. $sum = int32($sum - $delta);
  123. }
  124. return long2str($v, true);
  125. }
  126. }
  127. ?>