/[drupal]/contributions/modules/coolfilter/xxtea.php
ViewVC logotype

Contents of /contributions/modules/coolfilter/xxtea.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Thu Feb 15 08:18:10 2007 UTC (2 years, 9 months ago) by lllkkk
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.2: +45 -29 lines
File MIME type: text/x-php
[coolcode] add 'actionscript',[coolplayer] add I'm vlog,RockYou,SevenLoad,MyVideo.de,Clipfish.deTuDou Video....
1 <?php
2 /**
3 * @author Ma Bingyao(andot@ujn.edu.cn)
4 * @copyright CoolCode.CN
5 * @package XXTEA
6 * @version 1.4
7 * @lastupdate 2006-07-23
8 * @link http://www.coolcode.cn/?p=128
9 */
10
11 function long2str($v, $w) {
12 $len = count($v);
13 $s = array();
14 for ($i = 0; $i < $len; $i++)
15 {
16 $s[$i] = pack("V", $v[$i]);
17 }
18 if ($w) {
19 return substr(join('', $s), 0, $v[$len - 1]);
20 }
21 else {
22 return join('', $s);
23 }
24 }
25
26 function str2long($s, $w) {
27 $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
28 $v = array_values($v);
29 if ($w) {
30 $v[count($v)] = strlen($s);
31 }
32 return $v;
33 }
34
35 function int32($n) {
36 while ($n >= 2147483648) $n -= 4294967296;
37 while ($n <= -2147483649) $n += 4294967296;
38 return (int)$n;
39 }
40
41 function xxtea_encrypt($str, $key) {
42 if ($str == "") {
43 return "";
44 }
45 $v = str2long($str, true);
46 $k = str2long($key, false);
47 if (count($k) < 4)
48 {
49 for ($i = count($k); $i < 4; $i++) {
50 $k[$i] = 0;
51 }
52 }
53 $n = count($v) - 1;
54
55 $z = $v[$n];
56 $y = $v[0];
57 $delta = 0x9E3779B9;
58 $q = floor(6 + 52 / ($n + 1));
59 $sum = 0;
60 while (0 < $q--) {
61 $sum = int32($sum + $delta);
62 $e = $sum >> 2 & 3;
63 for ($p = 0; $p < $n; $p++) {
64 $y = $v[$p + 1];
65 $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
66 $z = $v[$p] = int32($v[$p] + $mx);
67 }
68 $y = $v[0];
69 $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
70 $z = $v[$n] = int32($v[$n] + $mx);
71 }
72 return long2str($v, false);
73 }
74
75 function xxtea_decrypt($str, $key) {
76 if ($str == "") {
77 return "";
78 }
79 $v = str2long($str, false);
80 $k = str2long($key, false);
81 if (count($k) < 4)
82 {
83 for ($i = count($k); $i < 4; $i++) {
84 $k[$i] = 0;
85 }
86 }
87 $n = count($v) - 1;
88
89 $z = $v[$n];
90 $y = $v[0];
91 $delta = 0x9E3779B9;
92 $q = floor(6 + 52 / ($n + 1));
93 $sum = int32($q * $delta);
94 while ($sum != 0) {
95 $e = $sum >> 2 & 3;
96 for ($p = $n; $p > 0; $p--) {
97 $z = $v[$p - 1];
98 $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
99 $y = $v[$p] = int32($v[$p] - $mx);
100 }
101 $z = $v[$n];
102 $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
103 $y = $v[0] = int32($v[0] - $mx);
104 $sum = int32($sum - $delta);
105 }
106 return long2str($v, true);
107 }
108 ?>

  ViewVC Help
Powered by ViewVC 1.1.2