# 下面是python代码
import requests,re,execjs,time,json
from lxml import etree
url = 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%253A%252F%252Fwww.jd.com%252F%253Fcu%253Dtrue%2526utm_source%253Dbaidu-pinzhuan%2526utm_medium%253Dcpc%2526utm_campaign%253Dt_288551095_baidupinzhuan%2526utm_term%253D0f3d30c8dba7459bb52f2eb5eba8ac7d_0_acc17bd41f084abe8822f5d3c8787770&callback=jsonp_031842680543343005'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36","Referer": "https://passport.jd.com/"}
resp = requests.get(url=url,headers=headers)
resp.encoding = 'GBK'
# obj = re.findall('id="pubKey" value="(.*?)"',resp.text)
tree = etree.HTML(resp.text)
pubKey = tree.xpath('//input[@id="pubKey"]/@value')[0]
print(pubKey)
node = execjs.get()
ctx = execjs.compile(open('./jingdong.js',mode = 'r',encoding='utf-8').read())
func_name = 'getpwd("{0}","{1}")'.format('123456',pubKey)
pwd = ctx.eval(func_name)
print(pwd)
#下面是报错代码
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\pythonProject\venv\firstspider\js_decrypt.py", line 136, in <module>
pwd = ctx.eval(func_name)
File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\execjs\_abstract_runtime_context.py", line 27, in eval
return self._eval(source)
File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\execjs\_external_runtime.py", line 78, in _eval
return self.exec_(code)
File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\execjs\_abstract_runtime_context.py", line 18, in exec_
return self._exec_(source)
File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\execjs\_external_runtime.py", line 88, in _exec_
return self._extract_result(output)
File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\execjs\_external_runtime.py", line 167, in _extract_result
raise ProgramError(value)
execjs._exceptions.ProgramError: ReferenceError: JSEncrypt is not defined
Process finished with exit code 1
'''
#下面是JS代码
'''javascript
function u(z) {
if (this.s < 0) {
return "-" + this.negate().toString(z)
}
var L;
if (z == 16) {
L = 4
} else {
if (z == 8) {
L = 3
} else {
if (z == 2) {
L = 1
} else {
if (z == 32) {
L = 5
} else {
if (z == 4) {
L = 2
} else {
return this.toRadix(z)
}
}
}
}
}
var bX = (1 << L) - 1,
b0,
t = false,
bY = "",
bW = this.t;
var bZ = this.DB - (bW * this.DB) % L;
if (bW-->0) {
if (bZ < this.DB && (b0 = this[bW] >> bZ) > 0) {
t = true;
bY = Y(b0)
}
while (bW >= 0) {
if (bZ < L) {
b0 = (this[bW] & ((1 << bZ) - 1)) << (L - bZ);
b0 |= this[--bW] >> (bZ += this.DB - L)
} else {
b0 = (this[bW] >> (bZ -= L)) & bX;
if (bZ <= 0) {
bZ += this.DB; --bW
}
}
if (b0 > 0) {
t = true
}
if (t) {
bY += Y(b0)
}
}
}
return t ? bY: "0"
}
function bC() {
var t = bm();
bf.ZERO.subTo(this, t);
return t
}
function bB() {
return (this.s < 0) ? this.negate() : this
}
function bN(t) {
var L = this.s - t.s;
if (L != 0) {
return L
}
var z = this.t;
L = z - t.t;
if (L != 0) {
return (this.s < 0) ? -L: L
}
while (--z >= 0) {
if ((L = this[z] - t[z]) != 0) {
return L
}
}
return 0
}
function q(z) {
var bW = 1,
L;
if ((L = z >>> 16) != 0) {
z = L;
bW += 16
}
if ((L = z >> 8) != 0) {
z = L;
bW += 8
}
if ((L = z >> 4) != 0) {
z = L;
bW += 4
}
if ((L = z >> 2) != 0) {
z = L;
bW += 2
}
if ((L = z >> 1) != 0) {
z = L;
bW += 1
}
return bW
}
function bt() {
if (this.t <= 0) {
return 0
}
return this.DB * (this.t - 1) + q(this[this.t - 1] ^ (this.s & this.DM))
}
function bv(L, z) {
var t;
for (t = this.t - 1; t >= 0; --t) {
z[t + L] = this[t]
}
for (t = L - 1; t >= 0; --t) {
z[t] = 0
}
z.t = this.t + L;
z.s = this.s
}
function a2(L, z) {
for (var t = L; t < this.t; ++t) {
z[t - L] = this[t]
}
z.t = Math.max(this.t - L, 0);
z.s = this.s
}
function s(b0, bW) {
var z = b0 % this.DB;
var t = this.DB - z;
var bY = (1 << t) - 1;
var bX = Math.floor(b0 / this.DB),
bZ = (this.s << z) & this.DM,
L;
for (L = this.t - 1; L >= 0; --L) {
bW[L + bX + 1] = (this[L] >> t) | bZ;
bZ = (this[L] & bY) << z
}
for (L = bX - 1; L >= 0; --L) {
bW[L] = 0
}
bW[bX] = bZ;
bW.t = this.t + bX + 1;
bW.s = this.s;
bW.clamp()
}
function bT(bZ, bW) {
bW.s = this.s;
var bX = Math.floor(bZ / this.DB);
if (bX >= this.t) {
bW.t = 0;
return
}
var z = bZ % this.DB;
var t = this.DB - z;
var bY = (1 << z) - 1;
bW[0] = this[bX] >> z;
for (var L = bX + 1; L < this.t; ++L) {
bW[L - bX - 1] |= (this[L] & bY) << t;
bW[L - bX] = this[L] >> z
}
if (z > 0) {
bW[this.t - bX - 1] |= (this.s & bY) << t
}
bW.t = this.t - bX;
bW.clamp()
}
function bs(z, bW) {
var L = 0,
bX = 0,
t = Math.min(z.t, this.t);
while (L < t) {
bX += this[L] - z[L];
bW[L++] = bX & this.DM;
bX >>= this.DB
}
if (z.t < this.t) {
bX -= z.s;
while (L < this.t) {
bX += this[L];
bW[L++] = bX & this.DM;
bX >>= this.DB
}
bX += this.s
} else {
bX += this.s;
while (L < z.t) {
bX -= z[L];
bW[L++] = bX & this.DM;
bX >>= this.DB
}
bX -= z.s
}
bW.s = (bX < 0) ? -1 : 0;
if (bX < -1) {
bW[L++] = this.DV + bX
} else {
if (bX > 0) {
bW[L++] = bX
}
}
bW.t = L;
bW.clamp()
}
function bJ(z, bW) {
var t = this.abs(),
bX = z.abs();
var L = t.t;
bW.t = L + bX.t;
while (--L >= 0) {
bW[L] = 0
}
for (L = 0; L < bX.t; ++L) {
bW[L + t.t] = t.am(0, bX[L], bW, L, 0, t.t)
}
bW.s = 0;
bW.clamp();
if (this.s != z.s) {
bf.ZERO.subTo(bW, bW)
}
}
function au(L) {
var t = this.abs();
var z = L.t = 2 * t.t;
while (--z >= 0) {
L[z] = 0
}
for (z = 0; z < t.t - 1; ++z) {
var bW = t.am(z, t[z], L, 2 * z, 0, 1);
if ((L[z + t.t] += t.am(z + 1, 2 * t[z], L, 2 * z + 1, bW, t.t - z - 1)) >= t.DV) {
L[z + t.t] -= t.DV;
L[z + t.t + 1] = 1
}
}
if (L.t > 0) {
L[L.t - 1] += t.am(z, t[z], L, 2 * z, 0, 1)
}
L.s = 0;
L.clamp()
}
function a9(b3, b0, bZ) {
var b9 = b3.abs();
if (b9.t <= 0) {
return
}
var b1 = this.abs();
if (b1.t < b9.t) {
if (b0 != null) {
b0.fromInt(0)
}
if (bZ != null) {
this.copyTo(bZ)
}
return
}
if (bZ == null) {
bZ = bm()
}
var bX = bm(),
z = this.s,
b2 = b3.s;
var b8 = this.DB - q(b9[b9.t - 1]);
if (b8 > 0) {
b9.lShiftTo(b8, bX);
b1.lShiftTo(b8, bZ)
} else {
b9.copyTo(bX);
b1.copyTo(bZ)
}
var b5 = bX.t;
var L = bX[b5 - 1];
if (L == 0) {
return
}
var b4 = L * (1 << this.F1) + ((b5 > 1) ? bX[b5 - 2] >> this.F2: 0);
var cc = this.FV / b4,
cb = (1 << this.F1) / b4,
ca = 1 << this.F2;
var b7 = bZ.t,
b6 = b7 - b5,
bY = (b0 == null) ? bm() : b0;
bX.dlShiftTo(b6, bY);
if (bZ.compareTo(bY) >= 0) {
bZ[bZ.t++] = 1;
bZ.subTo(bY, bZ)
}
bf.ONE.dlShiftTo(b5, bY);
bY.subTo(bX, bX);
while (bX.t < b5) {
bX[bX.t++] = 0
}
while (--b6 >= 0) {
var bW = (bZ[--b7] == L) ? this.DM: Math.floor(bZ[b7] * cc + (bZ[b7 - 1] + ca) * cb);
if ((bZ[b7] += bX.am(0, bW, bZ, b6, 0, b5)) < bW) {
bX.dlShiftTo(b6, bY);
bZ.subTo(bY, bZ);
while (bZ[b7] < --bW) {
bZ.subTo(bY, bZ)
}
}
}
if (b0 != null) {
bZ.drShiftTo(b5, b0);
if (z != b2) {
bf.ZERO.subTo(b0, b0)
}
}
bZ.t = b5;
bZ.clamp();
if (b8 > 0) {
bZ.rShiftTo(b8, bZ)
}
if (z < 0) {
bf.ZERO.subTo(bZ, bZ)
}
}
function bh(t) {
var z = bm();
this.abs().divRemTo(t, null, z);
if (this.s < 0 && z.compareTo(bf.ZERO) > 0) {
t.subTo(z, z)
}
return z
}
function aT(t) {
this.m = t
}
function aI(t) {
if (t.s < 0 || t.compareTo(this.m) >= 0) {
return t.mod(this.m)
} else {
return t
}
}
function c(t) {
return t
}
function V(t) {
t.divRemTo(this.m, null, t)
}
function p(t, L, z) {
t.multiplyTo(L, z);
this.reduce(z)
}
function aF(t, z) {
t.squareTo(z);
this.reduce(z)
}
aT.prototype.convert = aI;
aT.prototype.revert = c;
aT.prototype.reduce = V;
aT.prototype.mulTo = p;
aT.prototype.sqrTo = aF;
function ab() {
if (this.t < 1) {
return 0
}
var t = this[0];
if ((t & 1) == 0) {
return 0
}
var z = t & 3;
z = (z * (2 - (t & 15) * z)) & 15;
z = (z * (2 - (t & 255) * z)) & 255;
z = (z * (2 - (((t & 65535) * z) & 65535))) & 65535;
z = (z * (2 - t * z % this.DV)) % this.DV;
return (z > 0) ? this.DV - z: -z
}
function K(t) {
this.m = t;
this.mp = t.invDigit();
this.mpl = this.mp & 32767;
this.mph = this.mp >> 15;
this.um = (1 << (t.DB - 15)) - 1;
this.mt2 = 2 * t.t
}
function by(t) {
var z = bm();
t.abs().dlShiftTo(this.m.t, z);
z.divRemTo(this.m, null, z);
if (t.s < 0 && z.compareTo(bf.ZERO) > 0) {
this.m.subTo(z, z)
}
return z
}
function bl(t) {
var z = bm();
t.copyTo(z);
this.reduce(z);
return z
}
function bV(t) {
while (t.t <= this.mt2) {
t[t.t++] = 0
}
for (var L = 0; L < this.m.t; ++L) {
var z = t[L] & 32767;
var bW = (z * this.mpl + (((z * this.mph + (t[L] >> 15) * this.mpl) & this.um) << 15)) & t.DM;
z = L + this.m.t;
t[z] += this.m.am(0, bW, t, L, 0, this.m.t);
while (t[z] >= t.DV) {
t[z] -= t.DV;
t[++z]++
}
}
t.clamp();
t.drShiftTo(this.m.t, t);
if (t.compareTo(this.m) >= 0) {
t.subTo(this.m, t)
}
}
//省略部分代码
A.prototype.getPrivateBaseKey = function() {
var z = {
array: [new KJUR.asn1.DERInteger({
"int": 0
}), new KJUR.asn1.DERInteger({
bigint: this.n
}), new KJUR.asn1.DERInteger({
"int": this.e
}), new KJUR.asn1.DERInteger({
bigint: this.d
}), new KJUR.asn1.DERInteger({
bigint: this.p
}), new KJUR.asn1.DERInteger({
bigint: this.q
}), new KJUR.asn1.DERInteger({
bigint: this.dmp1
}), new KJUR.asn1.DERInteger({
bigint: this.dmq1
}), new KJUR.asn1.DERInteger({
bigint: this.coeff
})]
};
var t = new KJUR.asn1.DERSequence(z);
return t.getEncodedHex()
};
A.prototype.getPrivateBaseKeyB64 = function() {
return ae(this.getPrivateBaseKey())
};
A.prototype.getPublicBaseKey = function() {
var L = {
array: [new KJUR.asn1.DERObjectIdentifier({
oid: "1.2.840.113549.1.1.1"
}), new KJUR.asn1.DERNull()]
};
var t = new KJUR.asn1.DERSequence(L);
L = {
array: [new KJUR.asn1.DERInteger({
bigint: this.n
}), new KJUR.asn1.DERInteger({
"int": this.e
})]
};
var bX = new KJUR.asn1.DERSequence(L);
L = {
hex: "00" + bX.getEncodedHex()
};
var bW = new KJUR.asn1.DERBitString(L);
L = {
array: [t, bW]
};
var z = new KJUR.asn1.DERSequence(L);
return z.getEncodedHex()
};
A.prototype.getPublicBaseKeyB64 = function() {
return ae(this.getPublicBaseKey())
};
A.prototype.wordwrap = function(L, t) {
t = t || 64;
if (!L) {
return L
}
var z = "(.{1," + t + "})( +|$\n?)|(.{1," + t + "})";
return L.match(RegExp(z, "g")).join("\n")
};
A.prototype.getPrivateKey = function() {
var t = "-----BEGIN RSA PRIVATE KEY-----\n";
t += this.wordwrap(this.getPrivateBaseKeyB64()) + "\n";
t += "-----END RSA PRIVATE KEY-----";
return t
};
A.prototype.getPublicKey = function() {
var t = "-----BEGIN PUBLIC KEY-----\n";
t += this.wordwrap(this.getPublicBaseKeyB64()) + "\n";
t += "-----END PUBLIC KEY-----";
return t
};
A.prototype.hasPublicKeyProperty = function(t) {
t = t || {};
return (t.hasOwnProperty("n") && t.hasOwnProperty("e"))
};
A.prototype.hasPrivateKeyProperty = function(t) {
t = t || {};
return (t.hasOwnProperty("n") && t.hasOwnProperty("e") && t.hasOwnProperty("d") && t.hasOwnProperty("p") && t.hasOwnProperty("q") && t.hasOwnProperty("dmp1") && t.hasOwnProperty("dmq1") && t.hasOwnProperty("coeff"))
};
A.prototype.parsePropertiesFrom = function(t) {
this.n = t.n;
this.e = t.e;
if (t.hasOwnProperty("d")) {
this.d = t.d;
this.p = t.p;
this.q = t.q;
this.dmp1 = t.dmp1;
this.dmq1 = t.dmq1;
this.coeff = t.coeff
}
};
var bx = function(t) {
A.call(this);
if (t) {
if (typeof t === "string") {
this.parseKey(t)
} else {
if (this.hasPrivateKeyProperty(t) || this.hasPublicKeyProperty(t)) {
this.parsePropertiesFrom(t)
}
}
}
};
bx.prototype = new A();
bx.prototype.constructor = bx;
var a3 = function(t) {
t = t || {};
this.default_key_size = parseInt(t.default_key_size) || 1024;
this.default_public_exponent = t.default_public_exponent || "010001";
this.log = t.log || false;
this.key = null
};
a3.prototype.setKey = function(t) {
if (this.log && this.key) {
console.warn("A key was already set, overriding existing.")
}
this.key = new bx(t)
};
a3.prototype.setPrivateKey = function(t) {
this.setKey(t)
};
a3.prototype.setPublicKey = function(t) {
this.setKey(t)
};
a3.prototype.decrypt = function(t) {
try {
return this.getKey().decrypt(aW(t))
} catch(z) {
return false
}
};
a3.prototype.encrypt = function(t) {
try {
return ae(this.getKey().encrypt(t))
} catch(z) {
return false
}
};
a3.prototype.getKey = function(t) {
if (!this.key) {
this.key = new bx();
if (t && {}.toString.call(t) === "[object Function]") {
this.key.generateAsync(this.default_key_size, this.default_public_exponent, t);
return
}
this.key.generate(this.default_key_size, this.default_public_exponent)
}
return this.key
};
a3.prototype.getPrivateKey = function() {
return this.getKey().getPrivateKey()
};
a3.prototype.getPrivateKeyB64 = function() {
return this.getKey().getPrivateBaseKeyB64()
};
a3.prototype.getPublicKey = function() {
return this.getKey().getPublicKey()
};
a3.prototype.getPublicKeyB64 = function() {
return this.getKey().getPublicBaseKeyB64()
};
a3.version = "2.3.1";
ap.JSEncrypt = a3
});'''
//这下面的函数是我自己改写的
function getpwd(password,pubKey) {
// var pubKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB';
//if (!password || !pubKey || !SysConfig.encryptInfo) {
//return password;}
var encrypt = new JSEncrypt();
encrypt.setPublicKey(pubKey);
return encrypt.encrypt(password);
}