0

我以这种方式生成 ECDH 密钥

let _this = this;
window.crypto.subtle.generateKey(
        {
            name: "ECDH",
            namedCurve: "P-256", // the curve name
        },
        true, // <== Here if you want it to be exportable !!
        ["deriveKey", "deriveBits"] // usage
    )
    .then(key => {
        _this.keys = key;
        // export
        return window.crypto.subtle.exportKey(
            "raw", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
            _this.keys.publicKey
        )
        .then(rawPublicKey => {
            _this.publicKey = rawPublicKey;
            return rawPublicKey;
        })
    })

这样我就有了密码和原始(x,y 坐标)公钥。
我将使用密钥将其用于 ECDSA

我怎样才能做到这一点?

4

1 回答 1

0
window.crypto.subtle.generateKey({
                name: "ECDSA",
                namedCurve: curve, //can be "P-256", "P-384", or "P-521"
            },
            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) {

        publicKey = key.publicKey;
        privateKey = key.privateKey;
        // For Demo Purpos Only Exported in JWK format
        window.crypto.subtle.exportKey("jwk", key.publicKey).then(
            function(keydata) {
                publicKeyhold = keydata;
                publicKeyJson = JSON.stringify(publicKeyhold);
                document.getElementById("ecdsapublic").value = publicKeyJson;
            }
        );

        window.crypto.subtle.exportKey("jwk", key.privateKey).then(
            function(keydata) {
                privateKeyhold = keydata;
                privateKeyJson = JSON.stringify(privateKeyhold);
                document.getElementById("ecdsaprivate").value = privateKeyJson;

如您所见,您可以使用全局方法生成 ECDSA 密钥,它们会有所不同,您不能使用 ECDH 密钥});

于 2019-08-16T10:30:18.013 回答