使用到包 pyexecjs:pip install pyexecjs
Windows 环境
import execjs
js_file1 = open('js文件1').read()
js_file2 = open('js文件1').read()
tmp = execjs.compile((js_file1 + js_file2))
cmd = 'password_encrypt("", "")'.format('参数1', '参数2')
passwd_js = tmp.eval(cmd)
Linux 环境
提前安装 Node 环境,以 Centos 7.5.1804 为例:yum install nodejs -y
import os
import execjs
os.environ["EXECJS_RUNTIME"] = "Node"
node = execjs.get()
js_file = open("经过修改的JS文件").read()
tmp = node.compile(js_file)
passwd_js = tmp.call('函数名', '参数1', '参数2')
关于 nodejs 中有报错 CryptoJS is not defined 问题,由于对 JS 不太懂,只写处理方式,有兴趣的可查看末尾参考原文。
;(function (root, factory) {
if (typeof exports === "object") {
// CommonJS
module.exports = exports = factory();
}
else if (typeof define === "function" && define.amd) {
// AMD
define([], factory);
}
else {
// Global(browser)
root.CryptoJS = factory();
}
...
改为
var CryptoJS;
;(function (root, factory) {
CryptoJS = factory();
if (typeof exports === "object") {
// CommonJS
module.exports = exports = factory();
}
else if (typeof define === "function" && define.amd) {
// AMD
define([], factory);
}
else {
// Global(browser)
root.CryptoJS = factory();
}
...
参考
cuiqingcai.com/2022114.html