Skip to content

Instantly share code, notes, and snippets.

@xl7dev
Created January 29, 2018 15:18
Show Gist options
  • Save xl7dev/2823fd724942baa2447ad7f18385a002 to your computer and use it in GitHub Desktop.
Save xl7dev/2823fd724942baa2447ad7f18385a002 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
import sys
import execjs
def runjs(password):
js = execjs.compile("""
var k = RSAUtils = {}
var o = 2;
var e = 16;
var c = e;
var b = 1 << 16;
var z = b >>> 1;
var l = b * b;
var h = b - 1;
var A = 9999999999999998;
var m;
var g;
var t, a;
var v = BigInt = function (B) {
if (typeof B == "boolean" && B == true) {
this.digits = null
} else {
this.digits = g.slice(0)
}
this.isNeg = false
};
k.setMaxDigits = function (C) {
m = C;
g = new Array(m);
for (var B = 0; B < g.length; B++) {
g[B] = 0
}
t = new v();
a = new v();
a.digits[0] = 1
};
k.setMaxDigits(20);
var q = 15;
k.biFromNumber = function (D) {
var B = new v();
B.isNeg = D < 0;
D = Math.abs(D);
var C = 0;
while (D > 0) {
B.digits[C++] = D & h;
D = Math.floor(D / b)
}
return B
};
var r = k.biFromNumber(1000000000000000);
k.biFromDecimal = function (F) {
var E = F.charAt(0) == "-";
var D = E ? 1 : 0;
var B;
while (D < F.length && F.charAt(D) == "0") {
++D
}
if (D == F.length) {
B = new v()
} else {
var C = F.length - D;
var G = C % q;
if (G == 0) {
G = q
}
B = k.biFromNumber(Number(F.substr(D, G)));
D += G;
while (D < F.length) {
B = k.biAdd(k.biMultiply(B, r), k.biFromNumber(Number(F.substr(D, q))));
D += q
}
B.isNeg = E
}
return B
};
k.biCopy = function (C) {
var B = new v(true);
B.digits = C.digits.slice(0);
B.isNeg = C.isNeg;
return B
};
k.reverseStr = function (D) {
var B = "";
for (var C = D.length - 1; C > -1; --C) {
B += D.charAt(C)
}
return B
};
var x = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
k.biToString = function (D, F) {
var C = new v();
C.digits[0] = F;
var E = k.biDivideModulo(D, C);
var B = x[E[1].digits[0]];
while (k.biCompare(E[0], t) == 1) {
E = k.biDivideModulo(E[0], C);
digit = E[1].digits[0];
B += x[E[1].digits[0]]
}
return (D.isNeg ? "-" : "") + k.reverseStr(B)
};
k.biToDecimal = function (D) {
var C = new v();
C.digits[0] = 10;
var E = k.biDivideModulo(D, C);
var B = String(E[1].digits[0]);
while (k.biCompare(E[0], t) == 1) {
E = k.biDivideModulo(E[0], C);
B += String(E[1].digits[0])
}
return (D.isNeg ? "-" : "") + k.reverseStr(B)
};
var w = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
k.digitToHex = function (D) {
var C = 15;
var B = "";
for (i = 0; i < 4; ++i) {
B += w[D & C];
D >>>= 4
}
return k.reverseStr(B)
};
k.biToHex = function (C) {
var B = "";
var E = k.biHighIndex(C);
for (var D = k.biHighIndex(C); D > -1; --D) {
B += k.digitToHex(C.digits[D])
}
return B
};
k.charToHex = function (I) {
var D = 48;
var C = D + 9;
var E = 97;
var H = E + 25;
var G = 65;
var F = 65 + 25;
var B;
if (I >= D && I <= C) {
B = I - D
} else {
if (I >= G && I <= F) {
B = 10 + I - G
} else {
if (I >= E && I <= H) {
B = 10 + I - E
} else {
B = 0
}
}
}
return B
};
k.hexToDigit = function (E) {
var C = 0;
var B = Math.min(E.length, 4);
for (var D = 0; D < B; ++D) {
C <<= 4;
C |= k.charToHex(E.charCodeAt(D))
}
return C
};
k.biFromHex = function (F) {
var C = new v();
var B = F.length;
for (var E = B, D = 0; E > 0; E -= 4, ++D) {
C.digits[D] = k.hexToDigit(F.substr(Math.max(E - 4, 0), Math.min(E, 4)))
}
return C
};
k.biFromString = function (J, I) {
var B = J.charAt(0) == "-";
var E = B ? 1 : 0;
var K = new v();
var C = new v();
C.digits[0] = 1;
for (var D = J.length - 1; D >= E; D--) {
var F = J.charCodeAt(D);
var G = k.charToHex(F);
var H = k.biMultiplyDigit(C, G);
K = k.biAdd(K, H);
C = k.biMultiplyDigit(C, I)
}
K.isNeg = B;
return K
};
k.biDump = function (B) {
return (B.isNeg ? "-" : "") + B.digits.join(" ")
};
k.biAdd = function (C, G) {
var B;
if (C.isNeg != G.isNeg) {
G.isNeg = !G.isNeg;
B = k.biSubtract(C, G);
G.isNeg = !G.isNeg
} else {
B = new v();
var F = 0;
var E;
for (var D = 0; D < C.digits.length; ++D) {
E = C.digits[D] + G.digits[D] + F;
B.digits[D] = E % b;
F = Number(E >= b)
}
B.isNeg = C.isNeg
}
return B
};
k.biSubtract = function (C, G) {
var B;
if (C.isNeg != G.isNeg) {
G.isNeg = !G.isNeg;
B = k.biAdd(C, G);
G.isNeg = !G.isNeg
} else {
B = new v();
var F, E;
E = 0;
for (var D = 0; D < C.digits.length; ++D) {
F = C.digits[D] - G.digits[D] + E;
B.digits[D] = F % b;
if (B.digits[D] < 0) {
B.digits[D] += b
}
E = 0 - Number(F < 0)
}
if (E == -1) {
E = 0;
for (var D = 0; D < C.digits.length; ++D) {
F = 0 - B.digits[D] + E;
B.digits[D] = F % b;
if (B.digits[D] < 0) {
B.digits[D] += b
}
E = 0 - Number(F < 0)
}
B.isNeg = !C.isNeg
} else {
B.isNeg = C.isNeg
}
}
return B
};
k.biHighIndex = function (C) {
var B = C.digits.length - 1;
while (B > 0 && C.digits[B] == 0) {
--B
}
return B
};
k.biNumBits = function (D) {
var F = k.biHighIndex(D);
var E = D.digits[F];
var C = (F + 1) * c;
var B;
for (B = C; B > C - c; --B) {
if ((E & 32768) != 0) {
break
}
E <<= 1
}
return B
};
k.biMultiply = function (H, G) {
var K = new v();
var F;
var C = k.biHighIndex(H);
var J = k.biHighIndex(G);
var I, B, D;
for (var E = 0; E <= J; ++E) {
F = 0;
D = E;
for (j = 0; j <= C; ++j, ++D) {
B = K.digits[D] + H.digits[j] * G.digits[E] + F;
K.digits[D] = B & h;
F = B >>> e
}
K.digits[E + C + 1] = F
}
K.isNeg = H.isNeg != G.isNeg;
return K
};
k.biMultiplyDigit = function (B, G) {
var F, E, D;
result = new v();
F = k.biHighIndex(B);
E = 0;
for (var C = 0; C <= F; ++C) {
D = result.digits[C] + B.digits[C] * G + E;
result.digits[C] = D & h;
E = D >>> e
}
result.digits[1 + F] = E;
return result
};
k.arrayCopy = function (F, I, D, H, G) {
var B = Math.min(I + G, F.length);
for (var E = I, C = H; E < B; ++E, ++C) {
D[C] = F[E]
}
};
var f = [0, 32768, 49152, 57344, 61440, 63488, 64512, 65024, 65280, 65408, 65472, 65504, 65520, 65528, 65532, 65534, 65535];
k.biShiftLeft = function (C, I) {
var E = Math.floor(I / c);
var B = new v();
k.arrayCopy(C.digits, 0, B.digits, E, B.digits.length - E);
var H = I % c;
var D = c - H;
for (var F = B.digits.length - 1, G = F - 1; F > 0; --F, --G) {
B.digits[F] = ((B.digits[F] << H) & h) | ((B.digits[G] & f[H]) >>> (D))
}
B.digits[0] = ((B.digits[F] << H) & h);
B.isNeg = C.isNeg;
return B
};
var u = [0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535];
k.biShiftRight = function (C, I) {
var D = Math.floor(I / c);
var B = new v();
k.arrayCopy(C.digits, D, B.digits, 0, C.digits.length - D);
var G = I % c;
var H = c - G;
for (var E = 0, F = E + 1; E < B.digits.length - 1; ++E, ++F) {
B.digits[E] = (B.digits[E] >>> G) | ((B.digits[F] & u[G]) << H)
}
B.digits[B.digits.length - 1] >>>= G;
B.isNeg = C.isNeg;
return B
};
k.biMultiplyByRadixPower = function (C, D) {
var B = new v();
k.arrayCopy(C.digits, 0, B.digits, D, B.digits.length - D);
return B
};
k.biDivideByRadixPower = function (C, D) {
var B = new v();
k.arrayCopy(C.digits, D, B.digits, 0, B.digits.length - D);
return B
};
k.biModuloByRadixPower = function (C, D) {
var B = new v();
k.arrayCopy(C.digits, 0, B.digits, 0, D);
return B
};
k.biCompare = function (B, D) {
if (B.isNeg != D.isNeg) {
return 1 - 2 * Number(B.isNeg)
}
for (var C = B.digits.length - 1; C >= 0; --C) {
if (B.digits[C] != D.digits[C]) {
if (B.isNeg) {
return 1 - 2 * Number(B.digits[C] > D.digits[C])
} else {
return 1 - 2 * Number(B.digits[C] < D.digits[C])
}
}
}
return 0
};
k.biDivideModulo = function (G, F) {
var B = k.biNumBits(G);
var E = k.biNumBits(F);
var D = F.isNeg;
var L, K;
if (B < E) {
if (G.isNeg) {
L = k.biCopy(a);
L.isNeg = !F.isNeg;
G.isNeg = false;
F.isNeg = false;
K = biSubtract(F, G);
G.isNeg = true;
F.isNeg = D
} else {
L = new v();
K = k.biCopy(G)
}
return [L, K]
}
L = new v();
K = G;
var I = Math.ceil(E / c) - 1;
var H = 0;
while (F.digits[I] < z) {
F = k.biShiftLeft(F, 1);
++H;
++E;
I = Math.ceil(E / c) - 1
}
K = k.biShiftLeft(K, H);
B += H;
var O = Math.ceil(B / c) - 1;
var T = k.biMultiplyByRadixPower(F, O - I);
while (k.biCompare(K, T) != -1) {
++L.digits[O - I];
K = k.biSubtract(K, T)
}
for (var R = O; R > I; --R) {
var J = (R >= K.digits.length) ? 0 : K.digits[R];
var S = (R - 1 >= K.digits.length) ? 0 : K.digits[R - 1];
var Q = (R - 2 >= K.digits.length) ? 0 : K.digits[R - 2];
var P = (I >= F.digits.length) ? 0 : F.digits[I];
var C = (I - 1 >= F.digits.length) ? 0 : F.digits[I - 1];
if (J == P) {
L.digits[R - I - 1] = h
} else {
L.digits[R - I - 1] = Math.floor((J * b + S) / P)
}
var N = L.digits[R - I - 1] * ((P * b) + C);
var M = (J * l) + ((S * b) + Q);
while (N > M) {
--L.digits[R - I - 1];
N = L.digits[R - I - 1] * ((P * b) | C);
M = (J * b * b) + ((S * b) + Q)
}
T = k.biMultiplyByRadixPower(F, R - I - 1);
K = k.biSubtract(K, k.biMultiplyDigit(T, L.digits[R - I - 1]));
if (K.isNeg) {
K = k.biAdd(K, T);
--L.digits[R - I - 1]
}
}
K = k.biShiftRight(K, H);
L.isNeg = G.isNeg != D;
if (G.isNeg) {
if (D) {
L = k.biAdd(L, a)
} else {
L = k.biSubtract(L, a)
}
F = k.biShiftRight(F, H);
K = k.biSubtract(F, K)
}
if (K.digits[0] == 0 && k.biHighIndex(K) == 0) {
K.isNeg = false
}
return [L, K]
};
k.biDivide = function (B, C) {
return k.biDivideModulo(B, C)[0]
};
k.biModulo = function (B, C) {
return k.biDivideModulo(B, C)[1]
};
k.biMultiplyMod = function (C, D, B) {
return k.biModulo(k.biMultiply(C, D), B)
};
k.biPow = function (C, E) {
var B = a;
var D = C;
while (true) {
if ((E & 1) != 0) {
B = k.biMultiply(B, D)
}
E >>= 1;
if (E == 0) {
break
}
D = k.biMultiply(D, D)
}
return B
};
k.biPowMod = function (D, G, C) {
var B = a;
var E = D;
var F = G;
while (true) {
if ((F.digits[0] & 1) != 0) {
B = k.biMultiplyMod(B, E, C)
}
F = k.biShiftRight(F, 1);
if (F.digits[0] == 0 && k.biHighIndex(F) == 0) {
break
}
E = k.biMultiplyMod(E, E, C)
}
return B
};
BarrettMu = function (B) {
this.modulus = k.biCopy(B);
this.k = k.biHighIndex(this.modulus) + 1;
var C = new v();
C.digits[2 * this.k] = 1;
this.mu = k.biDivide(C, this.modulus);
this.bkplus1 = new v();
this.bkplus1.digits[this.k + 1] = 1;
this.modulo = s;
this.multiplyMod = n;
this.powMod = d
};
function s(J) {
var C = k;
var I = C.biDivideByRadixPower(J, this.k - 1);
var G = C.biMultiply(I, this.mu);
var F = C.biDivideByRadixPower(G, this.k + 1);
var E = C.biModuloByRadixPower(J, this.k + 1);
var K = C.biMultiply(F, this.modulus);
var D = C.biModuloByRadixPower(K, this.k + 1);
var B = C.biSubtract(E, D);
if (B.isNeg) {
B = C.biAdd(B, this.bkplus1)
}
var H = C.biCompare(B, this.modulus) >= 0;
while (H) {
B = C.biSubtract(B, this.modulus);
H = C.biCompare(B, this.modulus) >= 0
}
return B
}
function n(B, D) {
var C = k.biMultiply(B, D);
return this.modulo(C)
}
function d(C, F) {
var B = new v();
B.digits[0] = 1;
var D = C;
var E = F;
while (true) {
if ((E.digits[0] & 1) != 0) {
B = this.multiplyMod(B, D)
}
E = k.biShiftRight(E, 1);
if (E.digits[0] == 0 && k.biHighIndex(E) == 0) {
break
}
D = this.multiplyMod(D, D)
}
return B
}
var y = function (C, E, B) {
var D = k;
this.e = D.biFromHex(C);
this.d = D.biFromHex(E);
this.m = D.biFromHex(B);
this.chunkSize = 2 * D.biHighIndex(this.m);
this.radix = 16;
this.barrett = new BarrettMu(this.m)
};
k.getKeyPair = function (C, D, B) {
return new y(C, D, B)
};
if (typeof twoDigit === "undefined") {
twoDigit = function (B) {
return (B < 10 ? "0" : "") + String(B)
}
}
k.encryptedString = function (I, L) {
var H = [];
var B = L.length;
var F = 0;
while (F < B) {
H[F] = L.charCodeAt(F);
F++
}
while (H.length % I.chunkSize != 0) {
H[F++] = 0
}
var G = H.length;
var M = "";
var E, D, C;
for (F = 0; F < G; F += I.chunkSize) {
C = new v();
E = 0;
for (D = F; D < F + I.chunkSize; ++E) {
C.digits[E] = H[D++];
C.digits[E] += H[D++] << 8
}
var K = I.barrett.powMod(C, I.e);
var J = I.radix == 16 ? k.biToHex(K) : k.biToString(K, I.radix);
M += J + " "
}
return M.substring(0, M.length - 1)
};
k.decryptedString = function (F, G) {
var I = G.split(" ");
var B = "";
var E, D, H;
for (E = 0; E < I.length; ++E) {
var C;
if (F.radix == 16) {
C = k.biFromHex(I[E])
} else {
C = k.biFromString(I[E], F.radix)
}
H = F.barrett.powMod(C, F.d);
for (D = 0; D <= k.biHighIndex(H); ++D) {
B += String.fromCharCode(H.digits[D] & 255, H.digits[D] >> 8)
}
}
if (B.charCodeAt(B.length - 1) == 0) {
B = B.substring(0, B.length - 1)
}
return B
};
k.setMaxDigits(130);
function login(password) {
var a = "00b6fe800bd103ea7d4b8a4f61bc96e7ad6fd8a8a70329458b594bf85d9bf349d4670e61928cd31a604dc2feb2b29fc1ae65fba24170c9e51f50f3429195c70c0036dc2c981e1af9fe9a728779a2c13ed498c9183f3ae29360e8e38f3998846045853a06398694e746ff59159b7a7d215519ef197ac314b7bc74aa5e4643bdf3cb";
var d = "010001";
var c = k.getKeyPair(d, "", a);
var b = k.encryptedString(c, password);
return b
};
""")
return js.call("login", password)
if __name__ == "__main__":
password = sys.argv[1]
print runjs(password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment