在模拟登录过程中,有些目标网站的的密码会经过内部js进行加密,所以须对这过程进行模拟。须利用ScriptEngineManager类来执行其js
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test{
public static Invocable invoke = null;
static {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsEngine = manager.getEngineByName("javascript");
String jsFileName = "xxxxx.js"; // 指定md5加密文件
Reader reader;
try {
InputStream in = Test.class.getClassLoader()
.getResourceAsStream(jsFileName);
reader = new InputStreamReader(in);
jsEngine.eval(reader);
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
invoke = (Invocable) jsEngine;
}
public static String getPassEncoding(String pubKey, String serverTime,
String nonce,String password) {
String pass = null;
try {
pass = (String) invoke.invokeFunction("getPassEncoding", new Object[] {
pubKey, serverTime,nonce,password});
} catch (Exception e) {
System.out.println("为登陆密码加密时,出现异常!");
e.printStackTrace();
}
return pass;
}
public static void main(String[] args) {
}
}