2

我可以使用 WebCrypto API 创建一个像这样的 rsa 密钥(请参阅此处真正有用的示例集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"])

我如何才能真正找到生成的公钥的模数?

4

1 回答 1

4

以下代码应为您提供 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;
}
于 2015-11-11T01:16:40.520 回答