问题
I have the following JavaScript code to implement public key encryption using the Web Cryptography API. It works for Firefox and Chrome but fails for Microsoft Edge. The error I am getting from Edge is "Could not complete the operation due to error 80700011." What have I missed?
<script>
var data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
var crypto = window.crypto || window.msCrypto;
var cryptoSubtle = crypto.subtle;
cryptoSubtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: "SHA-256" },
},
true,
["encrypt", "decrypt"]
).then(function (key) {
console.log(key);
console.log(key.publicKey);
return cryptoSubtle.encrypt(
{
name: "RSA-OAEP"
},
key.publicKey,
data
);
}).then(function (encrypted) {
console.log(new Uint8Array(encrypted));
}).catch(function (err) {
console.error(err);
});
</script>
回答1:
I've found the cause of this issue. I have to add the hash field when invoking the encrypt function:
return cryptoSubtle.encrypt(
{
name: "RSA-OAEP",
hash: { name: "SHA-256" }
},
key.publicKey,
data
);
This does not match the Web Cryptography API Spec but it works.
回答2:
Same problem with crypto.subtle.sign
. Needed to add the hashing algorithm (same issue in Safari)
Replace
crypto.subtle.sign(
{
name: "RSASSA-PKCS1-v1_5"
},
cryptoKey,
digestToSignBuf);
with
crypto.subtle.sign(
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256"
},
cryptoKey,
digestToSignBuf);
来源:https://stackoverflow.com/questions/33043091/public-key-encryption-in-microsoft-edge