代码之家  ›  专栏  ›  技术社区  ›  Anna B

如何导入ECDH公钥(无法使用指定的密钥用法创建密钥)

  •  0
  • Anna B  · 技术社区  · 6 年前

    我试图用webcrypto运行代码,但似乎无法导入ECDH公钥。我错过了什么?

    我得到这个错误: 无法使用指定的密钥用法创建密钥 .

    浏览器:Google Chrome版本71.0.3578.98(官方版本)(64位)

    (在Firefox上运行良好)。

    window.crypto.subtle
      .generateKey(
        {
          name: 'ECDH',
          namedCurve: 'P-256',
        },
        true,
        ['deriveKey', 'deriveBits']
      )
      .then(function(key) {
        return window.crypto.subtle
          .exportKey('raw', key.publicKey)
          .then(function(ecdhPub) {
            return window.crypto.subtle
              .importKey(
                'raw',
                ecdhPub,
                {
                  name: 'ECDH',
                  namedCurve: 'P-256',
                },
                false,
                ['deriveKey', 'deriveBits']
              )
              .then(function(ecdhPubKey) {
                console.log('DONE !!', ecdhPubKey)
              })
              .catch(function(err) {
                console.log('COULD NOT IMPORT...')
                console.error(err)
              })
          })
          .catch(function(err) {
            console.log('COULD NOT EXPORT...')
            console.error(err)
          })
      })
      .catch(function(err) {
        console.log('COULD NOT GENERATE KEYS...')
        console.error(err)
      })
    0 回复  |  直到 6 年前
    推荐文章