2

我正在尝试从 JAVA6 中的 javascript 引擎库调用 stanford javascript 库加密函数。使用 JAVA6 中的 javascript 引擎库的以下代码,我收到以下错误:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Object] (<Unknown source>#19) in <Unknown source> at line number 19
at com.sun.script.javascript.RhinoScriptEngine.invoke(Unknown Source)
at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(Unknown Source)
at Test.main(Test.java:149)

任何帮助将非常感激。

public static void main(String[] args){
try {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    System.out.println("Available script engines:");
    for (int i = 0; i < factories.size(); i++) {
        ScriptEngineFactory factory = factories.get(i);
        String engine = factory.getEngineName();
        String language = factory.getLanguageName();
        System.out.println("-------------------------------------------");
        System.out.println("Language: " + language);
        System.out.println("Engine: " + engine);
        System.out.println("-------------------------------------------");
    }
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("ECMAScript");
    File script_file = new File("web/js/sjcl.js");
    Reader reader = new FileReader(script_file);
    engine.eval(reader);
    String script = "function decode(encoded_value){"
            + "var decoded_value = sjcl.decrypt('asdf',encoded_value);"
            + "return decoded_value;}"
            + "function encode(plain_text){"
            + "var encoded_value = sjcl.encrypt('asdf', plain_text);"
            + "return encoded_value;}";
    engine.eval(script);
    Invocable invocableEngine = (Invocable) engine;
    String plain_text = "admin";
    String cypher_text = (String) invocableEngine.invokeFunction("encode", plain_text);
    String plain_text_return = (String)invocableEngine.invokeFunction("decode", cypher_text);
    System.out.print("plain_text="+plain_text+"\ncypher_text=" +cypher_text + "\nplain_text_return=" + plain_text_return+"\n");
} catch (Exception e) {
    e.printStackTrace();
}

}

4

2 回答 2

0

To solve this problem, I had to change the sjcl.js to change the "throw new" statements.

An example:

throw new sjcl.exception.invalid("required parameter overridden");

became:

sjcl.exception.invalid.message = ("required parameter overridden");
throw ("required parameter overridden");

I have not done enough tests, ok. I don't know if this change can cause problems in the future.

于 2012-06-19T02:56:57.777 回答
0

我们也遇到了这个问题,我们设法将其追踪到 JRE 版本,RhinoScriptEngine似乎不喜欢在 JRE 6、JRE 7 及更高版本中运行斯坦福脚本,它工作正常。

于 2016-01-27T11:12:37.570 回答