zoukankan      html  css  js  c++  java
  • 从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;
    }
    }
  • 相关阅读:
    钉钉 LDAP
    OpenLDAP 密码策略与审计控制
    Active Directory LDAP DingDing
    Linux kill 命令 java
    Memory Analyzer 与 Java VM 版本支持问题
    java.lang.Thread.State
    稻盛和夫 活法 人生公式
    [领导力/管理]一句话说带团队
    把某个公司git项目迁移到gitee的步骤
    Protocol Buffers  |  Google Developers
  • 原文地址:https://www.cnblogs.com/jakin3130/p/10559425.html
Copyright © 2011-2022 走看看