Javascript 将 ECDH 密钥转换为 ECDSA 密钥

Posted

技术标签:

【中文标题】Javascript 将 ECDH 密钥转换为 ECDSA 密钥【英文标题】:Javascript convert ECDH keys into ECDSA keys 【发布时间】:2019-08-16 10:04:12 【问题描述】:

我以这种方式生成 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

我该怎么做?

【问题讨论】:

你解释一下你最终想要达到的目标? 我会为 ECDSA 重新使用生成的 ECDH 密钥 【参考方案1】:
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 密钥 );

【讨论】:

以上是关于Javascript 将 ECDH 密钥转换为 ECDSA 密钥的主要内容,如果未能解决你的问题,请参考以下文章

Nodejs Crypto ECDH PublicKey Hex as X.509

java的RSA签名验签怎么转化成c

使用 javascript 将用户类型转换为大写

如何访问 OpenSSL 的 EVP_PKEY 结构中的原始 ECDH 公钥、私钥和参数?

在javascript中转换为e+的脚本

密钥协商机制