<?php /* XXTEA encryption arithmetic library. * * Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn> * Version: 1.5 * LastModified: Dec 5, 2006 * This library is free. You can redistribute it and/or modify it. */ class xxtea { function long2str($v, $w) { $len = count($v); $n = ($len - 1) << 2; if ($w) { $m = $v[$len - 1]; if (($m < $n - 3) || ($m > $n)) return false; $n = $m; } $s = array(); for ($i = 0; $i < $len; $i++) { $s[$i] = pack("V", $v[$i]); } if ($w) { return substr(join('', $s), 0, $n); } else { return join('', $s); } } function str2long($s, $w) { $v = unpack("V*", $s. str_repeat("/0", (4 - strlen($s) % 4) & 3)); $v = array_values($v); if ($w) { $v[count($v)] = strlen($s); } return $v; } function xxtea_encrypt($str, $key) { if ($str == "") { return ""; } $v = $this->str2long($str, true); $k = $this->str2long($key, false); $max=count($k); $i=0; $j=0; while(isset($v[$i])) { if($v[$i]==0) { $i++; continue; } $v[$i] = $v[$i] ^ $k[$j]; $i++; $j++; if($j==$max) { $i--; $v[$i] = $v[$i] ^ $i; $i++; $j=0; } } return $this->long2str($v, false); } function xxtea_decrypt($str, $key) { if ($str == "") { return ""; } $v = $this->str2long($str, false); $k = $this->str2long($key, false); $max=count($k); $i=0; $j=0; while(isset($v[$i])) { if($v[$i]==0) { $i++; continue; } $v[$i] = $v[$i] ^ $k[$j]; $i++; $j++; if($j==$max) { $i--; $j--; $v[$i] = $v[$i] ^ $k[$j]; $v[$i] = $v[$i] ^ $i; $v[$i] = $v[$i] ^ $k[$j]; $i++; $j=0; } } return $this->long2str($v, true); } }