【Android】java中调用JS的方法

时间:2022-01-24 06:44:08

最近因为学校换了新的教务系统,想做一个模拟登陆功能,发现登陆的账号和密码有一个js脚本来进行加密

整理了一下java中执行JS的方法

智强教务 账号 密码 加密方法

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encodeInp(input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64
} else if (isNaN(chr3)) {
enc4 = 64
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = ""
} while (i < input.length);
return output
}

Java

首先需要创建一个接口,接口中的抽象方法要和js中函数名保持一致

public interface Methods {
public String encodeInp(String input);
}

然后在主函数中使用ScriptEngineManager来加载js

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
FileReader fileReader=new FileReader("F:\\JAVAProject\\src\\conwork.js");//js路径
engine.eval(fileReader);
if (engine instanceof Invocable) {
Invocable invocable = (Invocable) engine;
Methods executeMethod = invocable.getInterface(Methods.class);
System.out.println(executeMethod.encodeInp("account"));
System.out.println(executeMethod.encodeInp("password"));
}

Android

由于安卓中没有ScriptEngineManager ,引入jar包后也无法使用(囧)

故使用J2V8这个第三方库来进行加载

implementation 'com.eclipsesource.j2v8:j2v8:4.5.0@aar'

将js文件放在assets文件夹下

然后在Activity中调用

public String encodeNumber()
{
String result="没有";
InputStream is= null; //获取用户名与密码加密的js代码
try {
is = getAssets().open("conwork.js");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
V8 runtime = V8.createV8Runtime(); //使用J2V8运行js代码并将编码结果返回
final String encodename = runtime.executeStringScript(sb.toString()
+ "encodeInp('account');\n");
final String encodepwd=runtime.executeStringScript(sb.toString()+"encodeInp(password');\n");
runtime.release();
result=encodename+"%%%"+encodepwd;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}