Skip to content

Instantly share code, notes, and snippets.

@killbus
Forked from irever/JDxTEA.js
Created September 8, 2021 15:05
Show Gist options
  • Save killbus/c2bb54554b7716bfff1afac7771a0a4d to your computer and use it in GitHub Desktop.
Save killbus/c2bb54554b7716bfff1afac7771a0a4d to your computer and use it in GitHub Desktop.
JDxTEA #JD #XTEA
//// libDecryptorJni.so TeaEncryptECB ////
function xor(a, b) {
var l = a.length,
t, ret = [];
console.log("l:" + l);
for (var i = 0; i < l; i++) {
var aa = a[i];
console.log(aa)
t = aa ^ b[i];
console.log(t);
ret.push(t);
}
return ret;
}
var delta = 0x9E3779B9;
function encipherArray(array, key) {
var sum = delta,
n = 0x10; //16轮加密
var data = arrayToLongArray(array);
while (n-- > 0) {
data[0] += ((data[1] << 4 & 0xFFFFFFF0) + key[0]) ^ (data[1] + sum) ^ ((data[1] >> 5 & 0x07ffffff) + key[1]);
data[1] += ((data[0] << 4 & 0xFFFFFFF0) + key[2]) ^ (data[0] + sum) ^ ((data[0] >> 5 & 0x07ffffff) + key[3]);
sum += delta;
}
return longArrayToArray(data);
}
function arrayToLongArray(array) {
var result = [];
for (var i = 0, length = array.length; i < length; i += 4) {
result.push((
array[i + 0] << 24 |
array[i + 1] << 16 |
array[i + 2] << 8 |
array[i + 3]));
}
return result;
}
function longArrayToArray(data) {
var result = [];
for (var i = 0, length = data.length; i < length; i++) {
result.push(data[i] >>> 24 & 0xff,
data[i] >>> 16 & 0xff,
data[i] >>> 8 & 0xff,
data[i] & 0xff);
}
return result;
}
function test() {
console.log("============== XTEA of JD Application (iOS and Android) =================");
var v = [1, 9, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 6, 0, 100, 1, 17, 0, 0, 2, 0, 40, 0, 4, 116, 101, 115, 116, 0, 32, 48, 57, 56, 102, 54, 98, 99, 100, 52, 54, 50, 49, 100, 51, 55, 51, 99, 97, 100, 101, 52, 101, 56, 51, 50, 54, 50, 55, 98, 52, 102, 54, 0, 4, 0, 52, 48, 48, 48, 97, 48, 48, 48, 49, 48, 48, 48, 54, 48, 48, 48, 49, 48, 48, 50, 48, 69, 67, 50, 55, 53, 69, 50, 48, 67, 53, 57, 66, 54, 57, 65, 50, 53, 65, 57, 70, 49, 56, 52, 50, 55, 68, 57, 48, 68, 54, 55, 51, 0, 8, 0, 100, 0, 2, 0, 100, 0, 7, 97, 110, 100, 114, 111, 105, 100, 0, 5, 54, 46, 48, 46, 49, 0, 5, 53, 46, 50, 46, 48, 0, 9, 49, 57, 50, 48, 42, 49, 48, 56, 48, 0, 5, 106, 100, 97, 112, 112, 0, 4, 119, 105, 102, 105, 0, 7, 48, 46, 48, 95, 48, 46, 48, 0, 27, 57, 57, 48, 48, 48, 54, 54, 50, 48, 48, 53, 52, 48, 51, 45, 48, 50, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 0, 0, 0, 1, 0, 5, 50, 46, 50, 46, 48, 0, 72, 0, 26, 0, 8, 79, 110, 101, 80, 108, 117, 115, 54, 0, 4, 79, 80, 80, 79, 0, 6, 80, 65, 67, 77, 48, 48, 0, 0];
var rand = 42;
var vl = v.length;
console.log("vl:" + vl);
var r5 = (vl + 10) % 8;
console.log("r5:" + r5);
if (r5)
r5 = 8 - r5;
var firstChar = rand & 0xFFFFFFF8 | r5;
console.log("firstChar(v7):" + firstChar);
var filln = (8 - (vl + 2)) % 8;
filln += 2 + (filln < 0 ? 8 : 0);
var array = [];
//首位
array.push(firstChar);
//填充
for (var j = 0, sum = filln; j < sum; j++) {
array.push(rand);
}
//原文
array = array.concat(v);
//尾部填充的 7 字节的 \0
array = array.concat([0, 0, 0, 0, 0, 0, 0]);
v = array;
var tr = [0, 0, 0, 0, 0, 0, 0, 0],
to = tr,
r = '',
o = tr;
vl = v.length;
var result = [];
for (var i = 0; i < vl; i += 8) {
console.log("++++++++++++++++++++");
var array1 = v.slice(i, i + 8);
console.log("array1:" + array1);
o = xor(array1, tr);
// if (i == 0)
// o = array1;
console.log("o:" + o);
var enc = encipherArray(o, my_key_arr);
console.log("enc:" + enc);
tr = xor(enc, to);
console.log('tr:' + tr);
to = o;
result = result.concat(tr);
// console.log("result:"+result)
r += tr;
}
for (var i = 0, length = result.length; i < length; i++) {
var item = result[i];
if (item > 128) {
result[i] = item - 256;
}
}
console.log("result:" + result);
return result;
}
var my_key_arr = [555819297, 555819297, 555819297, 555819297];
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment