从JAVA6中的javascript引擎库调用Stanford javascript库加密函数的问题

时间:2012-06-15 01:10:14

标签: javascript encryption sjcl

我试图从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();
}

}

2 个答案:

答案 0 :(得分:0)

要解决这个问题,我必须更改sjcl.js以更改“throw new”语句。

一个例子:

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

成为:

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

我还没有做足够的测试,好吧。我不知道这种变化是否会在将来引发问题。

答案 1 :(得分:0)

我们也有这个问题,我们设法将其追溯到JRE版本,RhinoScriptEngine似乎不喜欢在JRE 6,JRE 7及以上版本中运行斯坦福脚本,它运行正常。< / p>