Menu

[r494]: / phprpc_3.0 / java / XXTEA.java  Maximize  Restore  History

Download this file

200 lines (177 with data), 5.8 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| XXTEA.java |
| |
| Release 3.0.1 |
| 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: 3.0.1
* LastModified: Jan 12, 2008
* This library is free. You can redistribute it and/or modify it.
*/
package org.phprpc.util;
final public class XXTEA {
private static final int delta = 0x9E3779B9;
private XXTEA() {}
final private static int MX(int sum, int y, int z, int p, int e, int[] k) {
return (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
}
/**
* Encrypt data with key.
*
* @param data
* @param key
* @return
*/
synchronized final 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
*/
synchronized final 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
*/
final private 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], 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] += MX(sum, y, z, p, e, k);
}
y = v[0];
z = v[n] += MX(sum, y, z, p, e, k);
}
return v;
}
/**
* Decrypt data with key.
*
* @param v
* @param k
* @return
*/
final private 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], 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] -= MX(sum, y, z, p, e, k);
}
z = v[n];
y = v[0] -= MX(sum, y, z, p, e, k);
sum = sum - delta;
}
return v;
}
/**
* Convert byte array to int array.
*
* @param data
* @param includeLength
* @return
*/
final 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
*/
final private static byte[] toByteArray(int[] data, boolean includeLength) {
int n = data.length << 2;
if (includeLength) {
int m = data[data.length - 1];
if (m > n) {
return null;
}
else {
n = m;
}
}
byte[] result = new byte[n];
for (int i = 0; i < n; i++) {
result[i] = (byte) ((data[i >>> 2] >>> ((i & 3) << 3)) & 0xff);
}
return result;
}
}
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.