Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.giovannirocca.ls;
import java.io.ByteArrayOutputStream;
/**
* Created by igio90 on 17/02/17.
*
* Example:
*
* byte[] body = response.body().bytes(); // The bytes of the body response
* byte[] key = Arrays.copyOfRange(body, 16, 35);
* body = Arrays.copyOfRange(body, 35, body.length);
* String json = decrypt(body, key);
*/
public class Decrypter {
static final byte[] magic1 = new byte[] { 34, 36, 33 };
static final byte[] magic2 = new byte[] { 38, 37, 35 };
static final byte[] magic3 = new byte[] { 36, 35, 37 };
public static String decrypt(final byte[] body, final int key) {
byte b = 0;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
final int n2 = i = body.length;
byte b2 = 0;
while (i > 0) {
final int n3 = i - 1;
final byte b3 = body[i - 1];
final int n4 = (n2 - i + 1) % 10;
if (b3 >= 40 && b3 <= 126) {
if (b3 - key - n4 < 40) {
byteArrayOutputStream.write(b3 - key - n4 + 87);
} else {
byteArrayOutputStream.write(b3 - key - n4);
}
} else {
if (n3 >= 1) {
b2 = body[n3 - 1];
}
if (n3 >= 2) {
b = body[n3 - 2];
}
if (check(b3, b2, b, magic1)) {
byteArrayOutputStream.write(10);
i -= magic1.length - 1;
} else if (check(b3, b2, b, magic2)) {
byteArrayOutputStream.write(13);
i -= magic2.length - 1;
} else if (check(b3, b2, b, magic3)) {
byteArrayOutputStream.write(32);
i -= magic3.length - 1;
} else {
byteArrayOutputStream.write(body[i - 1]);
}
}
--i;
}
try {
final String s = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
byteArrayOutputStream.close();
return s;
} catch (Exception ex) {
return "";
}
}
private static boolean check(final byte b, final byte b2, final byte b3, final byte[] array) {
boolean b4 = true;
switch (array.length) {
default: {
b4 = false;
break;
}
case 3: {
if (b != array[0] || b2 != array[1] || b3 != array[2]) {
b4 = false;
break;
}
break;
}
}
return b4;
}
public static int generateKey(final byte[] array) {
try {
final Byte value = 48;
final int n = array[17] - value;
final int n2 = array[18] - value;
final int n3 = array[11] - value;
final int n4 = array[0] - value + array[1] - value + array[2] - value + array[3] - value + array[5] - value + array[6] - value + array[8] - value + array[9] - value + n3 + array[12] - value + array[14] - value + array[15] - value + n + n2;
int n5;
if (n2 >= n) {
n5 = n4 + n2 - n;
}
else {
n5 = n4 + (n3 + 1) * 3;
}
return n5;
}
catch (Exception ex) {
return 27;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment