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;
    }
    }
  • 相关阅读:
    PHP之简单实现MVC框架
    socket泄露的问题
    gdb 调试多线程
    MMAP和DIRECT IO区别
    三年回首:C基础
    定时器管理:nginx的红黑树和libevent的堆
    strsep和strtok_r替代strtok
    缓存穿透和缓存失效
    mmap为什么比read/write快(兼论buffercache和pagecache)
    B+Tree和MySQL索引分析
  • 原文地址:https://www.cnblogs.com/jakin3130/p/10559425.html
Copyright © 2011-2022 走看看