XXTEA算法标准 —— 目前提供 PHP 、C、Java、Python、JavaScript的版本,其他版本请自行解决。
### PHP
~~~
<?php
/* XXTEA encryption arithmetic library.
* Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.5
* LastModified: Dec 5, 2006
*/
function long2str($v, $w) {
$len = count($v);
$n = ($len - 1) << 2;
if ($w) {
$m = $v[$len - 1];
if (($m < $n - 3) || ($m > $n)) return false;
$n = $m;
}
$s = array();
for ($i = 0; $i < $len; $i++) {
$s[$i] = pack("V", $v[$i]);
}
if ($w) {
return substr(join('', $s), 0, $n);
}
else {
return join('', $s);
}
}
function str2long($s, $w) {
$v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
$v = array_values($v);
if ($w) {
$v[count($v)] = strlen($s);
}
return $v;
}
function int32($n) {
while ($n >= 2147483648) $n -= 4294967296;
while ($n <= -2147483649) $n += 4294967296;
return (int)$n;
}
function xxtea_encrypt($str, $key) {
if ($str == "") {
return "";
}
$v = str2long($str, true);
$k = str2long($key, false);
if (count($k) < 4) {
for ($i = count($k); $i < 4; $i++) {
$k[$i] = 0;
}
}
$n = count($v) - 1;
$z = $v[$n];
$y = $v[0];
$delta = 0x9E3779B9;
$q = floor(6 + 52 / ($n + 1));
$sum = 0;
while (0 < $q--) {
$sum = int32($sum + $delta);
$e = $sum >> 2 & 3;
for ($p = 0; $p < $n; $p++) {
$y = $v[$p + 1];
$mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$z = $v[$p] = int32($v[$p] + $mx);
}
$y = $v[0];
$mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$z = $v[$n] = int32($v[$n] + $mx);
}
return long2str($v, false);
}
function xxtea_decrypt($str, $key) {
if ($str == "") {
return "";
}
$v = str2long($str, false);
$k = str2long($key, false);
if (count($k) < 4) {
for ($i = count($k); $i < 4; $i++) {
$k[$i] = 0;
}
}
$n = count($v) - 1;
$z = $v[$n];
$y = $v[0];
$delta = 0x9E3779B9;
$q = floor(6 + 52 / ($n + 1));
$sum = int32($q * $delta);
while ($sum != 0) {
$e = $sum >> 2 & 3;
for ($p = $n; $p > 0; $p--) {
$z = $v[$p - 1];
$mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$y = $v[$p] = int32($v[$p] - $mx);
}
$z = $v[$n];
$mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$y = $v[0] = int32($v[0] - $mx);
$sum = int32($sum - $delta);
}
return long2str($v, true);
}
~~~
### Python
>官方库 -> https://pypi.python.org/pypi/xtea
### Java
~~~
密匙 : public final static String KEY = "MuFeng";
~~~
~~~
package com.cardvalue.sys.widget;
import java.nio.charset.Charset;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import android.util.Base64;
/*
* XXTEA 加密算法
*/
public class XXTEA {
public static String Encrypt(String data, String key) {
return ToHexString(TEAEncrypt(
ToLongArray(PadRight(data, MIN_LENGTH).getBytes(
Charset.forName("UTF8"))),
ToLongArray(PadRight(key, MIN_LENGTH).getBytes(
Charset.forName("UTF8")))));
}
public static String Decrypt(String data, String key) {
if (data == null || data.length() < MIN_LENGTH) {
return data;
}
byte[] code = ToByteArray(TEADecrypt(
ToLongArray(data),
ToLongArray(PadRight(key, MIN_LENGTH).getBytes(
Charset.forName("UTF8")))));
return new String(code, Charset.forName("UTF8"));
}
private static long[] TEAEncrypt(long[] data, long[] key) {
int n = data.length;
if (n < 1) {
return data;
}
long z = data[data.length - 1], y = data[0], sum = 0, e, p, q;
q = 6 + 52 / n;
while (q-- > 0) {
sum += DELTA;
e = (sum >> 2) & 3;
for (p = 0; p < n - 1; p++) {
y = data[(int) (p + 1)];
z = data[(int) p] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)
^ (sum ^ y) + (key[(int) (p & 3 ^ e)] ^ z);
}
y = data[0];
z = data[n - 1] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)
^ (sum ^ y) + (key[(int) (p & 3 ^ e)] ^ z);
}
return data;
}
private static long[] TEADecrypt(long[] data, long[] key) {
int n = data.length;
if (n < 1) {
return data;
}
long z = data[data.length - 1], y = data[0], sum = 0, e, p, q;
q = 6 + 52 / n;
sum = q * DELTA;
while (sum != 0) {
e = (sum >> 2) & 3;
for (p = n - 1; p > 0; p--) {
z = data[(int) (p - 1)];
y = data[(int) p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)
^ (sum ^ y) + (key[(int) (p & 3 ^ e)] ^ z);
}
z = data[n - 1];
y = data[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y)
+ (key[(int) (p & 3 ^ e)] ^ z);
sum -= DELTA;
}
return data;
}
private static long[] ToLongArray(byte[] data) {
int n = (data.length % 8 == 0 ? 0 : 1) + data.length / 8;
long[] result = new long[n];
for (int i = 0; i < n - 1; i++) {
result[i] = bytes2long(data, i * 8);
}
byte[] buffer = new byte[8];
for (int i = 0, j = (n - 1) * 8; j < data.length; i++, j++) {
buffer[i] = data[j];
}
result[n - 1] = bytes2long(buffer, 0);
return result;
}
private static byte[] ToByteArray(long[] data) {
List<Byte> result = new ArrayList<Byte>();
for (int i = 0; i < data.length; i++) {
byte[] bs = long2bytes(data[i]);
for (int j = 0; j < 8; j++) {
result.add(bs[j]);
}
}
while (result.get(result.size() - 1) == SPECIAL_CHAR) {
result.remove(result.size() - 1);
}
byte[] ret = new byte[result.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = result.get(i);
}
return ret;
}
public static byte[] long2bytes(long num) {
ByteBuffer buffer = ByteBuffer.allocate(8).order(
ByteOrder.LITTLE_ENDIAN);
buffer.putLong(num);
return buffer.array();
}
public static long bytes2long(byte[] b, int index) {
ByteBuffer buffer = ByteBuffer.allocate(8).order(
ByteOrder.LITTLE_ENDIAN);
buffer.put(b, index, 8);
return buffer.getLong(0);
}
private static String ToHexString(long[] data) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length; i++) {
sb.append(PadLeft(Long.toHexString(data[i]), 16));
}
return sb.toString();
}
private static long[] ToLongArray(String data) {
int len = data.length() / 16;
long[] result = new long[len];
for (int i = 0; i < len; i++) {
result[i] = new BigInteger(data.substring(i * 16, i * 16 + 16), 16)
.longValue();
}
return result;
}
private static String PadRight(String source, int length) {
while (source.length() < length) {
source += SPECIAL_CHAR;
}
return source;
}
private static String PadLeft(String source, int length) {
while (source.length() < length) {
source = '0' + source;
}
return source;
}
private static long DELTA = 0x9E3779B9;
private static int MIN_LENGTH = 32;
private static char SPECIAL_CHAR = '\0';
}
~~~
### C
~~~
#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) {
q = 6 + 52/n;
while (q-- > 0) {
sum += DELTA;
e = (sum >> 2) & 3;
for (p=0; p<n-1; p++) y = v[p+1], z = v[p] += MX;
y = v[0];
z = v[n-1] += MX;
}
return 0 ;
} else if (n < -1) {
n = -n;
q = 6 + 52/n;
sum = q*DELTA ;
while (sum != 0) {
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--) z = v[p-1], y = v[p] -= MX;
z = v[n-1];
y = v[0] -= MX;
sum -= DELTA;
}
return 0;
}
return 1;
}
~~~
### JavaScript
~~~
/* XXTEA encryption arithmetic library.
*
* Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.5
* LastModified: Dec 9, 2006
* This library is free. You can redistribute it and/or modify it.
*/
function long2str(v, w) {
var vl = v.length;
var n = (vl - 1) << 2;
if (w) {
var m = v[vl - 1];
if ((m < n - 3) || (m > n)) return null;
n = m;
}
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, n);
}
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);
if (k.length < 4) {
k.length = 4;
}
var n = v.length - 1;
var z = v[n], y = v[0], delta = 0x9E3779B9;
var mx, e, p, q = Math.floor(6 + 52 / (n + 1)), sum = 0;
while (0 < q--) {
sum = sum + delta & 0xffffffff;
e = sum >>> 2 & 3;
for (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);
if (k.length < 4) {
k.length = 4;
}
var n = v.length - 1;
var z = v[n - 1], y = v[0], delta = 0x9E3779B9;
var mx, e, p, q = Math.floor(6 + 52 / (n + 1)), sum = q * delta & 0xffffffff;
while (sum != 0) {
e = sum >>> 2 & 3;
for (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);
}
~~~