[Android] JS method call java

Because the school has recently switched to a new educational system, want to do a simulated landing function, login ID and password found there is a js script to encrypt

JS compiled a method of execution in java

 

Zhiqiang Senate account password encryption method

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

We first need to create an interface, an abstract interface methods to the js function name and consistency

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

ScriptEngineManager then used to load the main function 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

Since there is no Andrews ScriptEngineManager, after the introduction of the jar package can not be used (embarrassed)

Therefore, the use of J2V8 this third-party library to load

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

Js files in the file folder assets

Then call in an Activity

public String encodeNumber()
    {
        The Result String = "no" ;
        IS the InputStream = null ;    // Get the user name and password encryption js code 
        the 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);
            }
            Runtime V8 = V8.createV8Runtime ();       // Use J2V8 run js code and coding result returned 
            Final String encodeName = runtime.executeStringScript (sb.toString ()
                     + "encodeInp ( 'Account'); \ n-" );
             Final String = runtime.executeStringScript encodepwd (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;
    }

 

 

 

Guess you like

Origin www.cnblogs.com/robotpaul/p/11165783.html