0

当我加密 190 字节或更少字节时,以下代码可以正常工作。但如果我尝试 191 或更多,则会引发异常。加密较大数据的正确方法是什么?

async function go(size) {
  var { publicKey, privateKey } = await crypto.subtle.generateKey({
    name: "RSA-OAEP",
    modulusLength: 2048,
    publicExponent: new Uint8Array([1, 0, 1]),
    hash: "SHA-256",
  }, true, ["encrypt", "decrypt"])

  try {
    var res = await window.crypto.subtle.encrypt(
      { name: "RSA-OAEP" },
      publicKey,
      new Uint8Array(size),
    )

    console.log(`${size} - OK: ${res.byteLength}`)
  } catch (ex) {
    console.log(`${size} - ERROR: ${ex.constructor.name} ${ex}`)
  }
}

go(10)    // Ok
go(100)   // Ok
go(190)   // Ok
go(191)   // Error
go(200)   // Error
go(500)   // Error

4

0 回答 0