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

Posted 末末无闻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从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;
}
}

以上是关于从HTML文件中取出JS加密需要的参数,并调用js内的加密算法的主要内容,如果未能解决你的问题,请参考以下文章

按钮使用onclick调用js函数参数传递问题

Js怎么调用外部Js

html调用js文件里面的参数

HTML5三种对密码加密的方法

怎么在html文件中调用js文件

Js怎么调用外部Js