XXTEA加密算法的各种程序实现整理

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

引用XXTEA文章说明资料:
“... XXTEA 算法很安全,而且非常快速,非常适合应用于 Web 开发中。

但目前似乎很少有人将该算法用于实际开发中。

甚至国内尚无介绍该算法的文章(至少在 Google 上搜索不到这方面的中文文章,关于密码学算法的书中也未见提及)。

我在 Google 上搜索到了几个国外的 XXTEA 算法的实现(见参考文献)...”。

JavaScript实现(下载文件)
//=================================;
/* xxtea.js
*
* Author: Ma Bingyao <andot@>
* Copyright:
* Version: 1.2
* LastModified: 2006-05-02
* This library is free. You can redistribute it and/or modify it.
* /?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);
}
Java实现(下载文件)
//=================================;
/* xxtea.java
*
* Author: Ma Bingyao < andot@ >
* Copyright:
* Version: 1.0
* LastModified: 2006-05-11
* This library is free. You can redistribute it and/or modify it.
* /?p=169
*/
public class xxtea {
/**
* Encrypt data with key.
*
* @param data
* @param key
* @return
*/
public static byte [] encrypt ( byte [] data , byte [] key ) {
if ( data . length == 0 ) {
return data ;
}
return toByteArray ( encrypt ( toIntArray ( data , true ) , toIntArray ( key , false )) , false ) ;
}
/**
* Decrypt data with key.
*
* @param data
* @param key
* @return
*/
public static byte [] decrypt ( byte [] data , byte [] key ) {
if ( data . length == 0 ) {
return data ;
}
return toByteArray ( decrypt ( toIntArray ( data , false ) , toIntArray ( key , false )) , true ) ;
}
/**
* Encrypt data with key.
*
* @param v
* @param k
* @return
*/
public static int [] encrypt ( int [] v , int [] k ) {
int n = v . length - 1 ;
if ( n < 1 ) {
return v ;
}
if ( k . length < 4 ) {
int [] key = new int [ 4 ] ;
System . arraycopy ( k , 0 , key , 0 , k . length ) ;
k = key ;
}
int z = v [ n ] , y = v [ 0 ] , delta = 0x9E3779B9 , sum = 0 , e ;
int p , q = 6 + 52 / ( n + 1 ) ;
while ( q -- > 0 ) {
sum = sum + delta ;
e = sum >>> 2 & 3 ;
for ( p = 0 ; p < n ; p ++ ) {
y = v [ p + 1 ] ;
z = v [ p ] += ( z >>> 5 ^ y << 2 ) + ( y >>> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z ) ;
}
y = v [ 0 ] ;
z = v [ n ] += ( z >>> 5 ^ y << 2 ) + ( y >>> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z ) ;
}
return v ;
}
/**
* Decrypt data with key.
*
* @param v
* @param k
* @return
*/
public static int [] decrypt ( int [] v , int [] k ) {
int n = v . length - 1 ;
if ( n < 1 ) {
return v ;
}
if ( k . length < 4 ) {
int [] key = new int [ 4 ] ;
System . arraycopy ( k , 0 , key , 0 , k . length ) ;
k = key ;
}
int z = v [ n ] , y = v [ 0 ] , delta = 0x9E3779B9 , sum , e ;
int p , q = 6 + 52 / ( n + 1 ) ;
sum = q * delta ;
while ( sum != 0 ) {
e = sum >>> 2 & 3 ;
for ( p = n ; p > 0 ; p -- ) {
z = v [ p - 1 ] ;
y = v [ p ] -= ( z >>> 5 ^ y << 2 ) + ( y >>> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z ) ;
}
z = v [ n ] ;
y = v [ 0 ] -= ( z >>> 5 ^ y << 2 ) + ( y >>> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z ) ;
sum = sum - delta ;
}
return v ;
}
/**
* Convert byte array to int array.
*
* @param data
* @param includeLength
* @return
*/
private static int [] toIntArray ( byte [] data , boolean includeLength ) { int n = ((( data . length & 3 ) == 0 ) ? ( data . length >>> 2 )
: (( data . length >>> 2 ) + 1 )) ;
int [] result ;
if ( includeLength ) {
result = new int [ n + 1 ] ;
result [ n ] = data . length ;
} else {
result = new int [ n ] ;
}
n = data . length ;
for ( int i = 0 ; i < n ; i ++ ) {
result [ i >>> 2 ] |= ( 0x000000ff & data [ i ]) << (( i & 3 ) << 3 ) ; }
return result ;
}
/**
* Convert int array to byte array.
*
* @param data
* @param includeLength
* @return
*/
private static byte [] toByteArray ( int [] data , boolean includeLength ) { int n ;
if ( includeLength ) {
n = data [ data . length - 1 ] ;
} else {
n = data . length << 2 ;
}
byte [] result = new byte [ n ] ;
for ( int i = 0 ; i < n ; i ++ ) {
result [ i ] = ( byte ) ( data [ i >>> 2 ] >>> (( i & 3 ) << 3 )) ; }
return result ;
}
}
//=================================;
C#实现(下载文件)
//=================================;
/* xxtea.cs
*
* Author: Ma Bingyao < andot@ >
* Copyright:
* Version: 1.1
* LastModified: 2006-05-05
* This library is free. You can redistribute it and/or modify it.
* /?p=163
*/
using System ;
class XXTEA
{
public static Byte [] Encrypt ( Byte [] Data , Byte [] Key )
{
if ( Data . Length == 0 )
{
return Data ;
}
return ToByteArray ( Encrypt ( ToUInt32Array ( Data , true ) , ToUInt32Array ( Key , false )) , false ) ;
}
public static Byte [] Decrypt ( Byte [] Data , Byte [] Key )
{
if ( Data . Length == 0 )
{
return Data ;
}
return ToByteArray ( Decrypt ( ToUInt32Array ( Data , false ) , ToUInt32Array ( Key , false )) , true ) ;
}
public static UInt32 [] Encrypt ( UInt32 [] v , UInt32 [] k )
{
Int32 n = v . Length - 1 ;
if ( n < 1 )
{
return v ;
}
if ( k . Length < 4 )
{
UInt32 [] Key = new UInt32 [ 4 ] ;
k . CopyTo ( Key , 0 ) ;
k = Key ;
}
UInt32 z = v [ n ] , y = v [ 0 ] , delta = 0x9E3779B9 , sum = 0 , e ;
Int32 p , q = 6 + 52 / ( n + 1 ) ;
while ( q -- > 0 )
{
sum = unchecked ( sum + delta ) ;
e = sum >> 2 & 3 ;
for ( p = 0 ; p < n ; p ++ )
{
y = v [ p + 1 ] ;
z = unchecked ( v [ p ] += ( z >> 5 ^ y << 2 ) + ( y >> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z )) ;
}
y = v [ 0 ] ;
z = unchecked ( v [ n ] += ( z >> 5 ^ y << 2 ) + ( y >> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z )) ;
}
return v ;
}
public static UInt32 [] Decrypt ( UInt32 [] v , UInt32 [] k )
{
Int32 n = v . Length - 1 ;
if ( n < 1 )
{
return v ;
}
if ( k . Length < 4 )
{
UInt32 [] Key = new UInt32 [ 4 ] ;
k . CopyTo ( Key , 0 ) ;
k = Key ;
}
UInt32 z = v [ n ] , y = v [ 0 ] , delta = 0x9E3779B9 , sum , e ;
Int32 p , q = 6 + 52 / ( n + 1 ) ;
sum = unchecked (( UInt32 )( q * delta )) ;
while ( sum != 0 )
{
e = sum >> 2 & 3 ;
for ( p = n ; p > 0 ; p -- )
{
z = v [ p - 1 ] ;
y = unchecked ( v [ p ] -= ( z >> 5 ^ y << 2 ) + ( y >> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z )) ;
}
z = v [ n ] ;
y = unchecked ( v [ 0 ] -= ( z >> 5 ^ y << 2 ) + ( y >> 3 ^ z << 4 ) ^ ( sum ^ y ) + ( k [ p & 3 ^ e ] ^ z )) ;
sum = unchecked ( sum - delta ) ;
}
return v ;
}
private static UInt32 [] ToUInt32Array ( Byte [] Data , Boolean IncludeLength )
{
Int32 n = ((( Data . Length & 3 ) == 0 ) ? ( Data . Length >> 2 ) : (( Data . Length >> 2 ) + 1 )) ;
UInt32 [] Result ;
if ( IncludeLength )
{
Result = new UInt32 [ n + 1 ] ;
Result [ n ] = ( UInt32 ) Data . Length ;
}
else
{
Result = new UInt32 [ n ] ;
}
n = Data . Length ;
for ( Int32 i = 0 ; i < n ; i ++ )
{
Result [ i >> 2 ] |= ( UInt32 ) Data [ i ] << (( i & 3 ) << 3 ) ;
}
return Result ;
}
private static Byte [] ToByteArray ( UInt32 [] Data , Boolean IncludeLength )
{
Int32 n ;
if ( IncludeLength )
{
n = ( Int32 ) Data [ Data . Length - 1 ] ;
}
else
{
n = Data . Length << 2 ;
}
Byte [] Result = new Byte [ n ] ;
for ( Int32 i = 0 ; i < n ; i ++ )
{
Result [ i ] = ( Byte )( Data [ i >> 2 ] >> (( i & 3 ) << 3 )) ;
}
return Result ;
}
}
注意:如果需要在JavaScript 中加密解密带有汉字的信息,在加密时,需要先将带加密信息用utf16to8 进行转换,解密时,需要将解密后的内容再用utf8to16 还原。

如果要在PHP 和JavaScript 之间传递带有汉字的加密信息,原信息需要用UTF-8 字符集。

(utf16to8 和utf8to16 请参见utf.js。

)。

相关文档
最新文档