有没有办法在 WebCrypto API 中获取 RSA 密钥的组件?

Is there a way to get the components of an RSA key in the WebCrypto API?

我可以使用 WebCrypto 创建这样的 rsa 密钥 API (在这里查看真正有用的示例集 https://vibornoff.github.io/webcrypto-examples/index.html

 window.crypto.subtle.generateKey({
         name: "RSASSA-PKCS1-v1_5",
         modulusLength: 2048,
         publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
         hash: {name: "SHA-256"},
        }, 
    true, 
  ["sign", "verify"])

我怎样才能真正找到生成的 public 密钥的模数?

以下代码应为您提供 Uint8Arrays 的模数和指数:

window.crypto.subtle.generateKey({
  name: "RSASSA-PKCS1-v1_5",
  modulusLength: 2048,
  publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
  hash: {
    name: "SHA-256"
  },
}, true, ["sign", "verify"]).then(function(keyPair) {
  window.crypto.subtle.exportKey("jwk", keyPair.publicKey).then(
    function(key) {
      // base64url-decode modulus
      var modulus = base64UrlDecode(key.n);

      // base64url-decode exponent
      var exponent = base64UrlDecode(key.e);

      // modulus and exponent are now Uint8Arrays
    });
});

function base64UrlDecode(str) {
  str = atob(str.replace(/-/g, '+').replace(/_/g, '/'));
  var buffer = new Uint8Array(str.length);
  for(var i = 0; i < str.length; ++i) {
    buffer[i] = str.charCodeAt(i);
  }
  return buffer;
}