Menu

[r443]: / phprpc_3.0 / ajs / xxtea.js  Maximize  Restore  History

Download this file

137 lines (127 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| xxtea.js |
| |
| Release 3.0.0 |
| Copyright (c) 2005-2008 by Team-PHPRPC |
| |
| WebSite: http://www.phprpc.org/ |
| http://www.phprpc.net/ |
| http://www.phprpc.com/ |
| http://sourceforge.net/projects/php-rpc/ |
| |
| Authors: Ma Bingyao <andot@ujn.edu.cn> |
| |
| This file may be distributed and/or modified under the |
| terms of the GNU Lesser General Public License (LGPL) |
| version 3.0 as published by the Free Software Foundation |
| and appearing in the included file LICENSE. |
| |
\**********************************************************/
/* XXTEA encryption arithmetic library.
*
* Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.6
* LastModified: May 6, 2007
* This library is free. You can redistribute it and/or modify it.
*/
// static class XXTEA
var XXTEA = new function() {
// private static member delta
var delta = 0x9E3779B9;
// private static method longArrayToString
function longArrayToString(data, includeLength) {
var length = data.length;
var n = (length - 1) << 2;
if (includeLength) {
var m = data[length - 1];
if ((m < n - 3) || (m > n)) return null;
n = m;
}
for (var i = 0; i < length; i++) {
data[i] = String.fromCharCode(
data[i] & 0xff,
data[i] >>> 8 & 0xff,
data[i] >>> 16 & 0xff,
data[i] >>> 24 & 0xff
);
}
if (includeLength) {
return data.join('').substring(0, n);
}
else {
return data.join('');
}
}
// private static method stringToLongArray
function stringToLongArray(string, includeLength) {
var length = string.length;
var result = [];
for (var i = 0; i < length; i += 4) {
result[i >> 2] = string.charCodeAt(i) |
string.charCodeAt(i + 1) << 8 |
string.charCodeAt(i + 2) << 16 |
string.charCodeAt(i + 3) << 24;
}
if (includeLength) {
result[result.length] = length;
}
return result;
}
// public static method encrypt
this.encrypt = function(string, key) {
if (string == "") {
return "";
}
var v = stringToLongArray(string, true);
var k = stringToLongArray(key, false);
if (k.length < 4) {
k.length = 4;
}
var n = v.length - 1;
var z = v[n], y = v[0];
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 longArrayToString(v, false);
}
// public static method decrypt
this.decrypt = function(string, key) {
if (string == "") {
return "";
}
var v = stringToLongArray(string, false);
var k = stringToLongArray(key, false);
if (k.length < 4) {
k.length = 4;
}
var n = v.length - 1;
var z = v[n - 1], y = v[0];
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 longArrayToString(v, true);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.