簡體   English   中英

無法從 java 程序執行 web-crypto 腳本

[英]Unable to execute web-crypto script from java program

Java 代碼:

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class WebCryptoInvoke {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    if (!(engine instanceof Invocable)) {
      System.out.println("Invoking methods is not supported.");
      return;
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "/home/rajasekhar/Desktop/webcrypto.js";

    engine.eval("load('" + scriptPath + "')");
    Object webCrypto = engine.get("webcrypto");
    Object result = inv.invokeMethod(webCrypto, "generateKeyPair");
    System.out.println(result);
  }
}

JavaScript 代碼

"use strict";
var webcrypto = new Object();

webcrypto.generateKeyPair = function ()
{
 var result = {};

window.crypto.subtle.generateKey(
    {
        name: "RSASSA-PKCS1-v1_5",
        modulusLength: 2048, //can be 1024, 2048, or 4096
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
    },
    true, //whether the key is extractable (i.e. can be used in exportKey)
    ["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key){
    //returns a keypair object
    console.log(key);
    console.log(key.publicKey);
    console.log(key.privateKey);
result[0] = key.publicKey;
result[1] = key.privateKey;
})
.catch(function(err){
    console.error(err);
});
    return result;
};

錯誤:

線程“main”javax.script.ScriptException 中的異常:ReferenceError:“window”未在 jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine. java:470) 在 jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392) 在 jdk.nashorn.api.scripting.NashornScriptEngine.invokeMethod(NashornScriptEngine.java:199) 在 WebCryptoInvoke.main(WebCryptoInvoke.java :20) 原因:/home/rajasekhar/Desktop/webcrypto.js:9 ReferenceError:“窗口”未在 jdk.nashorn 的 jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) 中定義。在 jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) 在 jdk.nashorn.internal.objects.Global 的 internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)。 jdk.nashorn.internal.scripts.Script的 noSuchProperty (Global.java:1441)$Recompilation$2$86$webcrypto.generateKeyPair(/home/rajasekhar/Desktop/webcrypto.js:9) 在 jdk.nashorn.internal.runtime.ScriptFunctionData .invoke(ScriptFunctionData.java:637) 在 jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) 在 jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) 在 jdk。 nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199) 在 jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386) ... 2 更多

tl;博士使用 javafx 2 https://github.com/openjdk/jfx/search?q=documenthttps://stackoverflow.com/a/11266979/11711280

WebEngine webEngine = webView.getEngine();
webEngine.getLoadWorker().stateProperty().addListener(...)
webEngine.loadContent("load('" + scriptPath + "')");

但是:“完整的 Oracle Java Runtime 8 隨 Nashorn 一起提供。” " Oracle Java dev 1.8 自帶捆綁的 Java FX 版本,一般無法覆蓋。要使用 Java FX11,您需要使用 Java 11 運行項目並將系統屬性 java.library.path 設置為該位置外匯框架。”

String scriptPath = "/home/rajasekhar/Desktop/webcrypto.js";
function ScriptWindow(scriptPath){
  return eval(scriptPath);
}.call({window:{}},[...(args=[])]);

我希望靜態可變ScriptEngine范圍

engine.put("window", {});
engine.eval("load('" + scriptPath + "')");

或所有ScriptEngineManager引擎

ScriptContext ctx = new SimpleScriptContext();
Bindings globalBindings = new SimpleBindings();
ctx.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE);
ctx.setAttribute("window", {}, ScriptContext.GLOBAL_SCOPE);

可以通過int聲明(或class實例化)在函數中訪問,但https://stackoverflow.com/a/53561009/11711280似乎您可以設置 web-crypto 可能附加的初始window

public class Document {
};
Document window = new Document();

很難判斷這里是否需要更多可變windowdocument屬性https://github.com/w3c/webcrypto/blob/main/spec/dfn.js

一個腳本試圖訪問一個未在其中定義的全局變量時,nashorn 會在當前使用的 ScriptContext 的綁定中搜索該變量。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM