Heim >Backend-Entwicklung >PHP-Tutorial > 测试XXTea算法,网上没有一个php版本能还原小弟我的字符串,太奇葩了
测试XXTea算法,网上没有一个php版本能还原我的字符串,太奇葩了。
原文="你是我的,sdfsdfsdfasd,error\4&:1234/?.,\3&%$#@@";
还原过的 =你是我的,sdfsdfsdfasd,error&:1234/?.,&%$#@@;
只要遇到 \3,就不行。还有个别的\4,5,6,7,8,9,有的能还原,有的不能。
网上的所有php版本的XXTea算法基本都测试过,全是同样的问题,很奇怪。有高手吗指点一二?
<?php <br /> Class Xxtea {<br> public function encrypt($s, $key) {<br> return self :: xxtea_encrypt($s, $key);<br> }<br> public function decrypt($e, $key) {<br> return self :: xxtea_decrypt($e, $key);<br> }<br> private function long2str($v, $w) {<br> $len = count($v);<br> $n = ($len -1) if ($w) {<br> $m = $v[$len -1];<br> if (($m $n))<br> return false;<br> $n = $m;<br> }<br> $s = array ();<br> for ($i = 0; $i $s[$i] = pack("V", $v[$i]);<br> }<br> if ($w) {<br> return substr(join('', $s), 0, $n);<br> } else {<br> return join('', $s);<br> }<br> }<br> private function str2long($s, $w) {<br> $v = unpack("V*", $s . str_repeat("\0", (4 - strlen($s) % 4) & 3));<br> $v = array_values($v);<br> if ($w) {<br> $v[count($v)] = strlen($s);<br> }<br> return $v;<br> }<br> private function int32($n) {<br> while ($n >= 2147483648)<br> $n -= 4294967296;<br> while ($n $n += 4294967296;<br> return (int) $n;<br> }<br> private function xxtea_encrypt($str, $key) {<br> if ($str == "") {<br> return "";<br> }<br> $v = self :: str2long($str, true);<br> $k = self :: str2long($key, false);<br> if (count($k) for ($i = count($k); $i $k[$i] = 0;<br> }<br> }<br> $n = count($v) - 1;<br> $z = $v[$n];<br> $y = $v[0];<br> $delta = 0x9E3779B9;<br> $q = floor(6 + 52 / ($n +1));<br> $sum = 0;<br> <br> while (0 $sum = self :: int32($sum + $delta);<br> $e = $sum >> 2 & 3;<br> for ($p = 0; $p $y = $v[$p +1];<br> $mx = self :: int32((($z >> 5 & 0x07ffffff) ^ $y > 3 & 0x1fffffff) ^ $z $z = $v[$p] = self :: int32($v[$p] + $mx);<br> }<br> $y = $v[0];<br> $mx = self :: int32((($z >> 5 & 0x07ffffff) ^ $y > 3 & 0x1fffffff) ^ $z $z = $v[$n] = self :: int32($v[$n] + $mx);<br> }<br> return self :: long2str($v, false); <div class="clear"> </div>