//===========================================================================
//
// File Name: XXTEA.rul
//
//
//===========================================================================
1prototype NUMBER str2long(STRING,BOOL) ;
2prototype STRING long2str(VARIANT,BOOL);
3prototype STRING xxtea_encrypt(STRING,STRING);
4prototype STRING xxtea_decrypt(STRING,STRING);
5
6function STRING long2str(v, w)
7 NUMBER vl,sl;
8 NUMBER i;
9 LIST listID;
10 STRING svStr,szStr;
11begin
12 vl = SizeOf(v);
13 sl = v(vl - 1) & 0xffffffff;
14 listID=ListCreate(STRINGLIST);
15 for i = 0 to vl-1
16 svStr[0]=v(i) & 0xff;
17 svStr[1]=v(i) >> 8 & 0xff;
18 svStr[2]=v(i) >> 16 & 0xff;
19 svStr[3]=v(i) >> 24 & 0xff;
20 ListAddString (listID,svStr,AFTER);
21 endfor;
22 StrPutTokens( listID, szStr, "", FALSE );
23 if (w) then
24 StrSub (szStr,szStr,0,sl);
25 else
26 endif;
27 return szStr;
28end;
29function NUMBER str2long(s, w)
30 NUMBER vl,sl;
31 NUMBER i,len,v(),x,n;
32begin
33 len = StrLength(s);
34 x=1;
35 Resize (v,x);
36 for i = 0 to len-1 step 4
37 v(i >> 2) = s[i]
38 | s[i + 1] << 8
39 | s[i + 2] << 16
40 | s[i + 3] << 24;
41 x=x+1;
42 Resize (v,x);
43 endfor;
44 if (w) then
45 n= SizeOf(v) ;
46 v(n-1) = len;
47 endif;
48 return v;
49end;
50
51function string xxtea_encrypt(str, key)
52 NUMBER v(),k(),temp;
53 NUMBER n,z,y,delta,mx,e,q,sum,p;
54begin
55 if (str == "") then
56 return "";
57 endif;
58 v = str2long(str, TRUE);
59 k = str2long(key, FALSE);
60 n = SizeOf(v) - 1;
61 temp=SizeOf(k);
62 z = v(n);
63 y = v(0);
64 delta = 0x9E3779B9;
65 q = 6 + 52 / (n + 1)-1;
66 sum = 0;
67 while (q> 0)
68 sum = sum + delta & 0xffffffff;
69 e = sum >> 2 & 3;
70 for p = 0 to n-1
71 y = v(p + 1);
72 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
73 z = v(p) = v(p) + mx & 0xffffffff;
74 endfor;
75 y = v(0);
76 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
77 v(n) = v(n) + mx & 0xffffffff;
78 z = v(n) ;
79 q=q-1;
80 endwhile;
81
82 return long2str(v, FALSE);
83end;
84
85function string xxtea_decrypt(str, key)
86 NUMBER v(),k();
87 NUMBER n,z,y,delta,mx,e,q,sum,p;
88begin
89 if (str == "") then
90 return "";
91 endif;
92 v = str2long(str, FALSE);
93 k = str2long(key, FALSE);
94 n = SizeOf(v) - 1;
95
96 z = v(n - 1);
97 y = v(0);
98 delta = 0x9E3779B9;
99 q = 6 + 52 / (n + 1);
100 sum = q * delta & 0xffffffff;
101 while (sum != 0)
102 e = sum >> 2 & 3;
103 for p = n downto 0 step 1
104 z = v(p - 1);
105 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
106 y = v(p) = v(p) - mx & 0xffffffff;
107 endfor;
108 z = v(n);
109 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
110 v(0) = v(0) - mx & 0xffffffff;
111 y =v(0) ;
112 sum = sum - delta & 0xffffffff;
113 endwhile;
114
115 return long2str(v, TRUE);
116end;
117
//
// File Name: XXTEA.rul
//
//
//===========================================================================
1prototype NUMBER str2long(STRING,BOOL) ;
2prototype STRING long2str(VARIANT,BOOL);
3prototype STRING xxtea_encrypt(STRING,STRING);
4prototype STRING xxtea_decrypt(STRING,STRING);
5
6function STRING long2str(v, w)
7 NUMBER vl,sl;
8 NUMBER i;
9 LIST listID;
10 STRING svStr,szStr;
11begin
12 vl = SizeOf(v);
13 sl = v(vl - 1) & 0xffffffff;
14 listID=ListCreate(STRINGLIST);
15 for i = 0 to vl-1
16 svStr[0]=v(i) & 0xff;
17 svStr[1]=v(i) >> 8 & 0xff;
18 svStr[2]=v(i) >> 16 & 0xff;
19 svStr[3]=v(i) >> 24 & 0xff;
20 ListAddString (listID,svStr,AFTER);
21 endfor;
22 StrPutTokens( listID, szStr, "", FALSE );
23 if (w) then
24 StrSub (szStr,szStr,0,sl);
25 else
26 endif;
27 return szStr;
28end;
29function NUMBER str2long(s, w)
30 NUMBER vl,sl;
31 NUMBER i,len,v(),x,n;
32begin
33 len = StrLength(s);
34 x=1;
35 Resize (v,x);
36 for i = 0 to len-1 step 4
37 v(i >> 2) = s[i]
38 | s[i + 1] << 8
39 | s[i + 2] << 16
40 | s[i + 3] << 24;
41 x=x+1;
42 Resize (v,x);
43 endfor;
44 if (w) then
45 n= SizeOf(v) ;
46 v(n-1) = len;
47 endif;
48 return v;
49end;
50
51function string xxtea_encrypt(str, key)
52 NUMBER v(),k(),temp;
53 NUMBER n,z,y,delta,mx,e,q,sum,p;
54begin
55 if (str == "") then
56 return "";
57 endif;
58 v = str2long(str, TRUE);
59 k = str2long(key, FALSE);
60 n = SizeOf(v) - 1;
61 temp=SizeOf(k);
62 z = v(n);
63 y = v(0);
64 delta = 0x9E3779B9;
65 q = 6 + 52 / (n + 1)-1;
66 sum = 0;
67 while (q> 0)
68 sum = sum + delta & 0xffffffff;
69 e = sum >> 2 & 3;
70 for p = 0 to n-1
71 y = v(p + 1);
72 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
73 z = v(p) = v(p) + mx & 0xffffffff;
74 endfor;
75 y = v(0);
76 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
77 v(n) = v(n) + mx & 0xffffffff;
78 z = v(n) ;
79 q=q-1;
80 endwhile;
81
82 return long2str(v, FALSE);
83end;
84
85function string xxtea_decrypt(str, key)
86 NUMBER v(),k();
87 NUMBER n,z,y,delta,mx,e,q,sum,p;
88begin
89 if (str == "") then
90 return "";
91 endif;
92 v = str2long(str, FALSE);
93 k = str2long(key, FALSE);
94 n = SizeOf(v) - 1;
95
96 z = v(n - 1);
97 y = v(0);
98 delta = 0x9E3779B9;
99 q = 6 + 52 / (n + 1);
100 sum = q * delta & 0xffffffff;
101 while (sum != 0)
102 e = sum >> 2 & 3;
103 for p = n downto 0 step 1
104 z = v(p - 1);
105 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
106 y = v(p) = v(p) - mx & 0xffffffff;
107 endfor;
108 z = v(n);
109 mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k(p & 3 ^ e) ^ z);
110 v(0) = v(0) - mx & 0xffffffff;
111 y =v(0) ;
112 sum = sum - delta & 0xffffffff;
113 endwhile;
114
115 return long2str(v, TRUE);
116end;
117
参考文献:
《XXTEA 加密算法的 JavaScript 和 PHP 实现》 http://www.coolcode.cn/?p=128