Please someone tell me what’s wrong with my code.
I translate the code from JavaScript to Java, but I think there must be something problem in it, because it doesn’t work as expected.
BTW, I am not good at JavaScript, if you can tell me what’s wrong with my code in Java,
that’s awesome
Tips: the code following is a certain encrypt method
//...
info = JSON.stringify(info);
function encode(str, key) {
if (str === '') return '';
var v = s(str, true);
var k = s(key, false);
if (k.length < 4) k.length = 4;
var n = v.length - 1,
z = v[n],
y = v[0],
c = 0x86014019 | 0x183639A0,
m,
e,
p,
q = Math.floor(6 + 52 / (n + 1)),
d = 0;
while (0 < q--) {
d = d + c & (0x8CE0D9BF | 0x731F2640);
e = d >>> 2 & 3;
for (p = 0; p < n; p++) {
y = v[p + 1];
m = z >>> 5 ^ y << 2;
m += y >>> 3 ^ z << 4 ^ (d ^ y);
m += k[p & 3 ^ e] ^ z;
z = v[p] = v[p] + m & (0xEFB8D130 | 0x10472ECF);
}
y = v[0];
m = z >>> 5 ^ y << 2;
m += y >>> 3 ^ z << 4 ^ (d ^ y);
m += k[p & 3 ^ e] ^ z;
z = v[n] = v[n] + m & (0xBB390742 | 0x44C6F8BD);
}
return l(v, false);
}
function s(a, b) {
var c = a.length;
var v = [];
for (var i = 0; i < c; i += 4) {
v[i >> 2] = a.charCodeAt(i) | a.charCodeAt(i + 1) << 8 | a.charCodeAt(i + 2) << 16 | a.charCodeAt(i + 3) << 24;
}
if (b) v[v.length] = c;
return v;
}
function l(a, b) {
var d = a.length;
var c = d - 1 << 2;
if (b) {
var m = a[d - 1];
if (m < c - 3 || m > c) return null;
c = m;
}
for (var i = 0; i < d; i++) {
a[i] = String.fromCharCode(a[i] & 0xff, a[i] >>> 8 & 0xff, a[i] >>> 16 & 0xff, a[i] >>> 24 & 0xff);
}
return b ? a.join('').substring(0, c) : a.join('');
}
return '{SRBX1}' + base64.encode(encode(info, token));
//...
//...
private static String encode(String str,String key) {
if(str==null||key==null)
throw new RuntimeException("null is not allowed!");
if (str == "") return "";
var v = s(str, true);
var k = s(key, false);
if (k.length < 4) Arrays.copyOf(k, 4);
int n = v.length - 1,
z = v[n],
y = v[0],
c = 0x86014019 | 0x183639A0,
m,
e,
p,
q = (int)Math.floor(6 + 52 / (n + 1)),
d = 0;
while (0 < q--) {
d = d + c & (0x8CE0D9BF | 0x731F2640);
e = d >>> 2 & 3;
for (p = 0; p < n; p++) {
y = v[p + 1];
m = z >>> 5 ^ y << 2;
m += y >>> 3 ^ z << 4 ^ (d ^ y);
m += k[p & 3 ^ e] ^ z;
z = v[p] = v[p] + m & (0xEFB8D130 | 0x10472ECF);
}
y = v[0];
m = z >>> 5 ^ y << 2;
m += y >>> 3 ^ z << 4 ^ (d ^ y);
m += k[p & 3 ^ e] ^ z;
z = v[n] = v[n] + m & (0xBB390742 | 0x44C6F8BD);
}
String[] v1 = new String[v.length];
for(int i = 0 ;i<v.length;i++)
v1[i] = Integer.toString(v[i]);
return l(v1, false);
}
private static int cPointCheck(String a,int cPointIndex){
if(cPointIndex<a.length())
return a.codePointAt(cPointIndex);
else
return 0;
}
private static int[] s(String a, boolean b) {
var c = a.length();
var v = new int[c];
for (var i = 0; i < c; i += 4) {
v[i >> 2] = cPointCheck(a, i) | cPointCheck(a, i+1) << 8 | cPointCheck(a, i+2) << 16 | cPointCheck(a, i+3) << 24;
}
if (b) v[v.length-1] = c;
return v;
}
private static String l(String[]a, boolean b) {
var d = a.length;
var c = d - 1 << 2;
if (b) {
var m = Integer.parseInt(a[d - 1]);
if (m < c - 3 || m > c) return null;
c = m;
}
for (var i = 0; i < d; i++) {
a[i] = new String(new char[]{(char)(Integer.parseInt(a[i]) & (0xff)), (char)(Integer.parseInt(a[i]) >>> 8 & (0xff)), (char)(Integer.parseInt(a[i]) >>> 16 & (0xff)), (char)(Integer.parseInt(a[i]) >>> 24 & (0xff))});
}
return b ? String.join("",a).substring(0, c) : String.join("",a);
}
public static String getUserInfoEncoded(String info,String token){
return "{SRBX1}" + encodeUserInfo(encode(info, token));
}
//...