xxtea的PHP类函数文件代码
xxtea的PHP测试文件代码
PHP实现的xxtea程序实例
xxtea的Javascript类函数文件代码
xxtea的Javascript代码使用的编码转换函数文件代码
xxtea的Javascript测试文件代码
Javascirpt实现xxtea的程序实例
<?
php
/* *
* @author Ma Bingyao(andot@ujn.edu.cn)
* @copyright CoolCode.CN
* @package XXTEA
* @version 1.3
* @lastupdate 2006-06-27
* @link http://www.coolcode.cn/?p=128
*/
// ****************************** 包装函数 **********************************************//
/*
* 传输和编码
*/
function _XXTEA_transform_encode( $str ) {
$tmp = base64_encode ( $str );
$tmp = iconv ( " GBK " , " UTF-8 " , $tmp );
$newstr = xxtea_encrypt( $tmp , ' 1234567890abcdef ' );
$newstr = bin2hex ( $newstr );
return $newstr ;
}
/*
* 解码并传输
*/
function _XXTEA_decode_transform( $str ) {
$tmp = xxtea_decrypt( $str , ' 1234567890abcdef ' );
$tmp = iconv ( " UTF-8 " , " GBK " , $tmp );
$newstr = base64_decode ( $tmp );
return $newstr ;
}
// ********************************* 原始函数 ******************************************//
function long2str( $v , $w ) {
$len = count ( $v );
$s = array ();
for ( $i = 0 ; $i < $len ; $i ++ )
{
$s [ $i ] = pack ( " V " , $v [ $i ]);
}
if ( $w ) {
return substr ( join ( '' , $s ) , 0 , $v [ $len - 1 ]);
}
else {
return join ( '' , $s );
}
}
function str2long( $s , $w ) {
$v = unpack ( " V* " , $s . str_repeat ( "
/* *
* @author Ma Bingyao(andot@ujn.edu.cn)
* @copyright CoolCode.CN
* @package XXTEA
* @version 1.3
* @lastupdate 2006-06-27
* @link http://www.coolcode.cn/?p=128
*/
// ****************************** 包装函数 **********************************************//
/*
* 传输和编码
*/
function _XXTEA_transform_encode( $str ) {
$tmp = base64_encode ( $str );
$tmp = iconv ( " GBK " , " UTF-8 " , $tmp );
$newstr = xxtea_encrypt( $tmp , ' 1234567890abcdef ' );
$newstr = bin2hex ( $newstr );
return $newstr ;
}
/*
* 解码并传输
*/
function _XXTEA_decode_transform( $str ) {
$tmp = xxtea_decrypt( $str , ' 1234567890abcdef ' );
$tmp = iconv ( " UTF-8 " , " GBK " , $tmp );
$newstr = base64_decode ( $tmp );
return $newstr ;
}
// ********************************* 原始函数 ******************************************//
function long2str( $v , $w ) {
$len = count ( $v );
$s = array ();
for ( $i = 0 ; $i < $len ; $i ++ )
{
$s [ $i ] = pack ( " V " , $v [ $i ]);
}
if ( $w ) {
return substr ( join ( '' , $s ) , 0 , $v [ $len - 1 ]);
}
else {
return join ( '' , $s );
}
}
function str2long( $s , $w ) {
$v = unpack ( " V* " , $s . str_repeat ( "
<?
require_once " ./xxtea.php " ;
$detail = " 咿咿 ﹎冇點笨﹉冇點尐脾氣﹎ゞ` ﹫_.侑祢們在..俄詪快楽 " ;
echo " <U><B>原始信息:</B></U><BR><TEXTAREA NAME=v1 ROWS=20 COLS=80>$detail</TEXTAREA> " ;
echo " <BR> " ;
$detail = base64_encode ( $detail );
$tmp = iconv ( " GBK " , " UTF-8 " , $detail );
$newstr = xxtea_encrypt( $tmp , ' 1234567890abcdef ' );
echo " <U><B>编码后信息:</B></U><BR><TEXTAREA NAME=v2 ROWS=20 COLS=80>$newstr</TEXTAREA> " ;
echo " <BR> " ;
$tmp = xxtea_decrypt( $newstr , ' 1234567890abcdef ' );
$tmp = iconv ( " UTF-8 " , " GBK " , $tmp );
$newstr2 = base64_decode ( $tmp );
echo " <U><B>解码后信息:</B></U><BR><TEXTAREA NAME=v2 ROWS=20 COLS=80>$newstr2</TEXTAREA> " ;
?>
require_once " ./xxtea.php " ;
$detail = " 咿咿 ﹎冇點笨﹉冇點尐脾氣﹎ゞ` ﹫_.侑祢們在..俄詪快楽 " ;
echo " <U><B>原始信息:</B></U><BR><TEXTAREA NAME=v1 ROWS=20 COLS=80>$detail</TEXTAREA> " ;
echo " <BR> " ;
$detail = base64_encode ( $detail );
$tmp = iconv ( " GBK " , " UTF-8 " , $detail );
$newstr = xxtea_encrypt( $tmp , ' 1234567890abcdef ' );
echo " <U><B>编码后信息:</B></U><BR><TEXTAREA NAME=v2 ROWS=20 COLS=80>$newstr</TEXTAREA> " ;
echo " <BR> " ;
$tmp = xxtea_decrypt( $newstr , ' 1234567890abcdef ' );
$tmp = iconv ( " UTF-8 " , " GBK " , $tmp );
$newstr2 = base64_decode ( $tmp );
echo " <U><B>解码后信息:</B></U><BR><TEXTAREA NAME=v2 ROWS=20 COLS=80>$newstr2</TEXTAREA> " ;
?>
xxtea的Javascript类函数文件代码
/**/
/* xxtea.js
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 1.2
* LastModified: 2006-05-02
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=128
*/
function long2str(v, w) ... {
var vl = v.length;
var sl = v[vl - 1] & 0xffffffff;
for (var i = 0; i < vl; i++)
...{
v[i] = String.fromCharCode(v[i] & 0xff,
v[i] >>> 8 & 0xff,
v[i] >>> 16 & 0xff,
v[i] >>> 24 & 0xff);
}
if (w) ...{
return v.join('').substring(0, sl);
}
else ...{
return v.join('');
}
}
function str2long(s, w) ... {
var len = s.length;
var v = [];
for (var i = 0; i < len; i += 4)
...{
v[i >> 2] = s.charCodeAt(i)
| s.charCodeAt(i + 1) << 8
| s.charCodeAt(i + 2) << 16
| s.charCodeAt(i + 3) << 24;
}
if (w) ...{
v[v.length] = len;
}
return v;
}
function xxtea_encrypt(str, key) ... {
if (str == "") ...{
return "";
}
var v = str2long(str, true);
var k = str2long(key, false);
var n = v.length - 1;
var z = v[n], y = v[0], delta = 0x9E3779B9;
var mx, e, q = Math.floor(6 + 52 / (n + 1)), sum = 0;
while (q-- > 0) ...{
sum = sum + delta & 0xffffffff;
e = sum >>> 2 & 3;
for (var p = 0; p < n; p++) ...{
y = v[p + 1];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
z = v[p] = v[p] + mx & 0xffffffff;
}
y = v[0];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
z = v[n] = v[n] + mx & 0xffffffff;
}
return long2str(v, false);
}
function xxtea_decrypt(str, key) ... {
if (str == "") ...{
return "";
}
var v = str2long(str, false);
var k = str2long(key, false);
var n = v.length - 1;
var z = v[n - 1], y = v[0], delta = 0x9E3779B9;
var mx, e, q = Math.floor(6 + 52 / (n + 1)), sum = q * delta & 0xffffffff;
while (sum != 0) ...{
e = sum >>> 2 & 3;
for (var p = n; p > 0; p--) ...{
z = v[p - 1];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
y = v[p] = v[p] - mx & 0xffffffff;
}
z = v[n];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
y = v[0] = v[0] - mx & 0xffffffff;
sum = sum - delta & 0xffffffff;
}
return long2str(v, true);
}
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 1.2
* LastModified: 2006-05-02
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=128
*/
function long2str(v, w) ... {
var vl = v.length;
var sl = v[vl - 1] & 0xffffffff;
for (var i = 0; i < vl; i++)
...{
v[i] = String.fromCharCode(v[i] & 0xff,
v[i] >>> 8 & 0xff,
v[i] >>> 16 & 0xff,
v[i] >>> 24 & 0xff);
}
if (w) ...{
return v.join('').substring(0, sl);
}
else ...{
return v.join('');
}
}
function str2long(s, w) ... {
var len = s.length;
var v = [];
for (var i = 0; i < len; i += 4)
...{
v[i >> 2] = s.charCodeAt(i)
| s.charCodeAt(i + 1) << 8
| s.charCodeAt(i + 2) << 16
| s.charCodeAt(i + 3) << 24;
}
if (w) ...{
v[v.length] = len;
}
return v;
}
function xxtea_encrypt(str, key) ... {
if (str == "") ...{
return "";
}
var v = str2long(str, true);
var k = str2long(key, false);
var n = v.length - 1;
var z = v[n], y = v[0], delta = 0x9E3779B9;
var mx, e, q = Math.floor(6 + 52 / (n + 1)), sum = 0;
while (q-- > 0) ...{
sum = sum + delta & 0xffffffff;
e = sum >>> 2 & 3;
for (var p = 0; p < n; p++) ...{
y = v[p + 1];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
z = v[p] = v[p] + mx & 0xffffffff;
}
y = v[0];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
z = v[n] = v[n] + mx & 0xffffffff;
}
return long2str(v, false);
}
function xxtea_decrypt(str, key) ... {
if (str == "") ...{
return "";
}
var v = str2long(str, false);
var k = str2long(key, false);
var n = v.length - 1;
var z = v[n - 1], y = v[0], delta = 0x9E3779B9;
var mx, e, q = Math.floor(6 + 52 / (n + 1)), sum = q * delta & 0xffffffff;
while (sum != 0) ...{
e = sum >>> 2 & 3;
for (var p = n; p > 0; p--) ...{
z = v[p - 1];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
y = v[p] = v[p] - mx & 0xffffffff;
}
z = v[n];
mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
y = v[0] = v[0] - mx & 0xffffffff;
sum = sum - delta & 0xffffffff;
}
return long2str(v, true);
}
/**/
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp> & 2006 Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.1
* LastModified: Feb 17 2006
* This library is free. You can redistribute it and/or modify it.
*/
/**/ /*
* Interfaces:
* utf8 = utf16to8(utf16);
* utf16 = utf16to8(utf8);
*/
function utf16to8(str) ... {
var out, i, len, c;
out = [];
len = str.length;
for(i = 0; i < len; i++) ...{
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) ...{
out[i] = str.charAt(i);
} else if (c > 0x07FF) ...{
out[i] = String.fromCharCode(0xE0 | ((c >> 12) & 0x0F),
0x80 | ((c >> 6) & 0x3F),
0x80 | ((c >> 0) & 0x3F));
} else ...{
out[i] = String.fromCharCode(0xC0 | ((c >> 6) & 0x1F),
0x80 | ((c >> 0) & 0x3F));
}
}
return out.join('');
}
function utf8to16(str) ... {
var out, i, len, c;
var char2, char3;
out = [];
len = str.length;
i = 0;
while(i < len) ...{
c = str.charCodeAt(i++);
switch(c >> 4)
...{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out[out.length] = str.charAt(i-1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out[out.length] = String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out[out.length] = String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
}
}
return out.join('');
}
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp> & 2006 Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.1
* LastModified: Feb 17 2006
* This library is free. You can redistribute it and/or modify it.
*/
/**/ /*
* Interfaces:
* utf8 = utf16to8(utf16);
* utf16 = utf16to8(utf8);
*/
function utf16to8(str) ... {
var out, i, len, c;
out = [];
len = str.length;
for(i = 0; i < len; i++) ...{
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) ...{
out[i] = str.charAt(i);
} else if (c > 0x07FF) ...{
out[i] = String.fromCharCode(0xE0 | ((c >> 12) & 0x0F),
0x80 | ((c >> 6) & 0x3F),
0x80 | ((c >> 0) & 0x3F));
} else ...{
out[i] = String.fromCharCode(0xC0 | ((c >> 6) & 0x1F),
0x80 | ((c >> 0) & 0x3F));
}
}
return out.join('');
}
function utf8to16(str) ... {
var out, i, len, c;
var char2, char3;
out = [];
len = str.length;
i = 0;
while(i < len) ...{
c = str.charCodeAt(i++);
switch(c >> 4)
...{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out[out.length] = str.charAt(i-1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out[out.length] = String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out[out.length] = String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
}
}
return out.join('');
}
<
html
>
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" />
< title > XXTEA 测试 </ title >
< script type ="text/javascript" > ...
var data;
function encrypt() ...{
start = (new Date()).getTime();
data = document.forms['f'].encrypt_text.value = xxtea_encrypt(utf16to8(document.forms['f'].plain_text.value), '1234567890abcdef');
end = (new Date()).getTime();
alert(end - start);
}
function decrypt() ...{
start = (new Date()).getTime();
document.forms['f'].decrypt_text.value = utf8to16(xxtea_decrypt(data, '1234567890abcdef'));
end = (new Date()).getTime();
alert(end - start);
}
</ script >
</ head >
< body >
< script type ="text/javascript" src ="utf.js" ></ script >
< script type ="text/javascript" src ="xxtea.js" ></ script >
< form name ="f" >
< textarea id ="plain_text" name ="plain_text" cols ="80" rows ="25" ></ textarea >
< input type ="button" onclick ="encrypt();" value ="加密" />< br />
< textarea id ="encrypt_text" name ="encrypt_text" cols ="80" rows ="25" ></ textarea >
< input type ="button" onclick ="decrypt();" value ="解密" />< br />
< textarea id ="decrypt_text" name ="decrypt_text" cols ="80" rows ="25" ></ textarea >
</ form >
</ body >
</ html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" />
< title > XXTEA 测试 </ title >
< script type ="text/javascript" > ...
var data;
function encrypt() ...{
start = (new Date()).getTime();
data = document.forms['f'].encrypt_text.value = xxtea_encrypt(utf16to8(document.forms['f'].plain_text.value), '1234567890abcdef');
end = (new Date()).getTime();
alert(end - start);
}
function decrypt() ...{
start = (new Date()).getTime();
document.forms['f'].decrypt_text.value = utf8to16(xxtea_decrypt(data, '1234567890abcdef'));
end = (new Date()).getTime();
alert(end - start);
}
</ script >
</ head >
< body >
< script type ="text/javascript" src ="utf.js" ></ script >
< script type ="text/javascript" src ="xxtea.js" ></ script >
< form name ="f" >
< textarea id ="plain_text" name ="plain_text" cols ="80" rows ="25" ></ textarea >
< input type ="button" onclick ="encrypt();" value ="加密" />< br />
< textarea id ="encrypt_text" name ="encrypt_text" cols ="80" rows ="25" ></ textarea >
< input type ="button" onclick ="decrypt();" value ="解密" />< br />
< textarea id ="decrypt_text" name ="decrypt_text" cols ="80" rows ="25" ></ textarea >
</ form >
</ body >
</ html >