从HTML文件中取出JS加密需要的参数,并调用js内的加密算法

背景,爬虫程序需要模拟登陆,账号密码是经js加密的,加密所需的参数需要从html页面中取出

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ScriptEngineTest {
public static void main(String[] args) throws IOException, ScriptException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String jsFileName = "src/main/resources/static/ss.js";
String htmlFileName = "src/main/resources/static/url.html";
Pattern pattern = Pattern.compile(""10001", "", "(.*?)"");
String htmldatas = readfile(htmlFileName);
String key = getkey(pattern, htmldatas);
System.out.println("Key=" + key);
FileReader reader = new FileReader(jsFileName);
engine.eval(reader);
if (engine instanceof Invocable) {
Invocable invoke = (Invocable) engine;
String c = (String) invoke.invokeFunction("find", "10001", key, "111111");
System.out.println("加密后:" + c);
}
reader.close();
}


public static String readfile(String filePath) {
File file = new File(filePath);
InputStream input = null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
try {
for (int n; (n = input.read(bytes)) != -1; ) {
buffer.append(new String(bytes, 0, n, "UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}

public static String getkey(Pattern pattern, String htmldatas) {
Matcher matcher = pattern.matcher(htmldatas);
String htmldata = "";
String key = "";
if (matcher.find()) {
htmldata = matcher.group();
System.out.println("htmldata=" + htmldata);
String ss[] = htmldata.split(",");
key = ss[2].replace(""","").trim();
}
return key;
}
}
原文地址:https://www.cnblogs.com/jakin3130/p/10559425.html