// append the enciphered longs to the result $enc_data .= $this- _long2str ( $w [0] ); $enc_data .= $this- _long2str ( $w [1] ); }
return $enc_data; } public function decrypt($enc_data, $key) { // convert data to long $n_enc_data_long = $this- _str2long ( 0, $enc_data, $enc_data_long );
// resize key to a multiple of 128 bits (16 bytes) $this- _resize ( $key, 16, true ); if ('' == $key) $key = '0000000000000000';
// convert key to long $n_key_long = $this- _str2long ( 0, $key, $key_long );
// decrypt the long data with the key $data = ''; $w = array (0, 0 ); $j = 0; $len = 0; $k = array (0, 0, 0, 0 ); $pos = 0;
// do right shift if (0 $integer) { $integer = 0x7fffffff; // remove sign bit before shift $integer = $n; // right shift $integer |= 1 (31 - $n); // set shifted sign bit } else { $integer = $n; // use normal right shift }
$w [0] = $y; $w [1] = $z; } XXTEA的算法 核心为 #define MX (z 5^y 2) + (y 3^z 4)^(sum^y) + (k[p 3^e]^z); long btea(long* v, long n, long* k) { unsigned long z=v[n-1], y=v[0], sum=0, e, DELTA=0x9e3779b9; long p, q ; if (n 1) { /* Coding Part */ q = 6 + 52/n; while (q-- 0) { sum += DELTA; e = (sum 2) for (p=0; p p++) y = v[p+1], z = v[p] += MX; y = v[0]; z = v[n-1] += MX; } return 0 ; } else if (n -1) { /* Decoding Part */ n = -n; q = 6 + 52/n; sum = q*DELTA ; while (sum != 0) { e = (sum 2) for (p=n-1; p p--) z = v[p-1], y = v[p] -= MX; z = v[n-1]; y = v[0] -= MX; sum -= DELTA; } return 0; } return 1; }也是运算不一样,这个就不写了,有人已经写过这方面的代码了html教程